- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathenigma_machine.py
59 lines (53 loc) · 1.63 KB
/
enigma_machine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
alphabets= [chr(i) foriinrange(32, 126)]
gear_one=list(range(len(alphabets)))
gear_two=list(range(len(alphabets)))
gear_three=list(range(len(alphabets)))
reflector=list(reversed(range(len(alphabets))))
code= []
gear_one_pos=gear_two_pos=gear_three_pos=0
defrotator():
globalgear_one_pos
globalgear_two_pos
globalgear_three_pos
i=gear_one[0]
gear_one.append(i)
delgear_one[0]
gear_one_pos+=1
ifgear_one_pos%len(alphabets) ==0:
i=gear_two[0]
gear_two.append(i)
delgear_two[0]
gear_two_pos+=1
ifgear_two_pos%len(alphabets) ==0:
i=gear_three[0]
gear_three.append(i)
delgear_three[0]
gear_three_pos+=1
defengine(input_character):
target=alphabets.index(input_character)
target=gear_one[target]
target=gear_two[target]
target=gear_three[target]
target=reflector[target]
target=gear_three.index(target)
target=gear_two.index(target)
target=gear_one.index(target)
code.append(alphabets[target])
rotator()
if__name__=="__main__":
decode=list(input("Type your message:\n"))
whileTrue:
try:
token=int(input("Please set token:(must be only digits)\n"))
break
exceptExceptionaserror:
print(error)
for_inrange(token):
rotator()
forjindecode:
engine(j)
print("\n"+"".join(code))
print(
f"\nYour Token is {token} please write it down.\nIf you want to decode "
"this message again you should input same digits as token!"
)