Skip to content

Commit 9532492

Browse files
shahabmohammadiAnupKumarPanwar
authored andcommitted
added enigma machine algorithm (#932)
1 parent 4ff2a9d commit 9532492

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Diff for: hashes/enigma_machine.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from __future__ import print_function
2+
3+
alphabets = [chr(i) for i in range(32, 126)]
4+
gear_one = [i for i in range(len(alphabets))]
5+
gear_two = [i for i in range(len(alphabets))]
6+
gear_three = [i for i in range(len(alphabets))]
7+
reflector = [i for i in reversed(range(len(alphabets)))]
8+
code = []
9+
gear_one_pos = gear_two_pos = gear_three_pos = 0
10+
11+
12+
def rotator():
13+
global gear_one_pos
14+
global gear_two_pos
15+
global gear_three_pos
16+
i = gear_one[0]
17+
gear_one.append(i)
18+
del gear_one[0]
19+
gear_one_pos += 1
20+
if gear_one_pos % int(len(alphabets)) == 0:
21+
i = gear_two[0]
22+
gear_two.append(i)
23+
del gear_two[0]
24+
gear_two_pos += 1
25+
if gear_two_pos % int(len(alphabets)) == 0:
26+
i = gear_three[0]
27+
gear_three.append(i)
28+
del gear_three[0]
29+
gear_three_pos += 1
30+
31+
32+
def engine(input_character):
33+
target = alphabets.index(input_character)
34+
target = gear_one[target]
35+
target = gear_two[target]
36+
target = gear_three[target]
37+
target = reflector[target]
38+
target = gear_three.index(target)
39+
target = gear_two.index(target)
40+
target = gear_one.index(target)
41+
code.append(alphabets[target])
42+
rotator()
43+
44+
45+
if __name__ == '__main__':
46+
decode = input("Type your message:\n")
47+
decode = list(decode)
48+
while True:
49+
try:
50+
token = int(input("Please set token:(must be only digits)\n"))
51+
break
52+
except Exception as error:
53+
print(error)
54+
for i in range(token):
55+
rotator()
56+
for i in decode:
57+
engine(i)
58+
print("\n" + "".join(code))
59+
print(
60+
f"\nYour Token is {token} please write it down.\nIf you want to decode "
61+
f"this message again you should input same digits as token!")

0 commit comments

Comments
 (0)