-
Notifications
You must be signed in to change notification settings - Fork 0
/
enigma.py
54 lines (36 loc) · 1019 Bytes
/
enigma.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
'''
Github.com/razyar
Github.com/jadijadi
'''
import pickle
alphabet = 'abcdefghijklmnopqrstuvwxyz'
f = open('./rotor_state.rotor', 'rb')
rotor1, rotor2, rotor3 = pickle.load(f)
f.close()
print("Your settings:\n\trotor1: %s\n\trotor2: %s\n\trotor3: %s" % (rotor1, rotor2, rotor3))
def reflector(char):
return alphabet[len(alphabet)-alphabet.find(char)-1]
def enigma_one_char(char):
char1 = rotor1[alphabet.find(char)]
char2 = rotor2[alphabet.find(char1)]
char3 = rotor3[alphabet.find(char2)]
reflected = reflector(char3)
char3 = alphabet[rotor3.find(reflected)]
char2 = alphabet[rotor2.find(char3)]
char1 = alphabet[rotor1.find(char2)]
return char1
def rotate_rotors():
global rotor1, rotor2, rotor3
rotor1 = rotor1[1:] + rotor1[0]
if state % 26:
rotor2 = rotor2[1:] + rotor2[0]
if state % (26*26):
rotor3 = rotor3 [1:] + rotor3[0]
plain = 'anything'
cipher = ''
state = 0
for char in plain:
state += 1
cipher += enigma_one_char(char)
rotate_rotors()
print '\n[out]: %s' % cipher