-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenigma.py
143 lines (115 loc) · 4.42 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import random
import pickle
class enigma:
def __init__(self):
self.alphabet_en = "abcdefghijklmnopqrstuvwxyz"
self.alphabet_en_big = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
self.alphabet_sign = ".:;’~`?,!@#$%^&*()[]{}'<>/\-_=+ 0123456789۰۱۲۳۴۵۶۷۸۹"
self.alphabet_fa = "اأإةآبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی"
self.alphabets = self.alphabet_en + self.alphabet_fa + self.alphabet_sign + self.alphabet_en_big
self.plugboard = {}
self.count_rotors = ''
self.rotors = []
self.state_rotor = 0
self.state_rotate = 0
def reflector(self,character):
"""
this is method for reflected characters
this is work change character
a <=> z
b <=> y
"""
return self.alphabets[len(self.alphabets) - self.alphabets.find(character) - 1]
def rotor_plugboard_generator(self,count,dictionary):
"""
this method for generate rotors by count input
"""
if dictionary != None:
self.plugboard.update(dictionary) # this just plugboard
rotors = []
for i in range(count):
i = list(self.alphabets)
random.shuffle(i)
i = ''.join(i)
rotors.append(i)
with open('rotor_state.enigma','wb') as file:
pickle.dump((rotors,self.plugboard),file)
file.close()
# just Testing for read file
def read_test(self):
with open('rotor_state.enigma','rb') as file:
items = pickle.load(file)
file.close()
self.rotors = items[0]
self.plugboard.update(items[1])
def master_mind(self,x):
"""
this is important method for enigma encode & decode ..
"""
#print(f'rotors = > {self.rotors}')
#print(f'plugboards = > {self.plugboard}')
character = ''
#print(character,'\n')
"""
This Here befor rotate started
plugboard checked
if x in plugboard:
x = plugboard[x]
"""
for key, value in self.plugboard.items():
if x == key:
x = value
elif x == value:
x = key
character = self.rotors[0][self.alphabets.find(x)]
#print(f'character of rotor[0] = {character}')
for i in range(1, len(self.rotors)):
character = self.rotors[i][self.alphabets.find(character)]
#print(f'character of rotor[{i}] = {character}')
character = self.reflector(character)
#print(f'character reflector = {character}')
for j in range(len(self.rotors),0,-1):
character = self.alphabets[self.rotors[j-1].find(character)]
#print(f'character of rotor[{j-1}] = {character}')
#print(f'i = {i}, len rotors = {len(self.rotors)}')
for key, value in self.plugboard.items():
if character == key:
character = value
elif character == value:
character = key
return character
def rotate_rotors(self):
"""
this method for rotate rotors
"""
self.rotors[0] = self.rotors[0][1:] + self.rotors[0][0]
self.state_rotate += 1
if self.state_rotate % (len(self.alphabets) * (self.state_rotor + 1)) == 0:
self.state_rotate = 0
self.state_rotor += 1
if self.state_rotor >= len(self.rotors):
self.state_rotor = 0
self.rotors[self.state_rotor] = self.rotors[self.state_rotor][1:] + self.rotors[self.state_rotor][0]
def run(self,plain):
"""
This Method for Run enigma machine
submit settings and options for start
"""
cipher = ''
self.read_test()
for c in plain:
#print(self.state_rotor)
cipher += self.master_mind(c)
self.rotate_rotors()
file = open('./cipher.txt','w')
file.write(cipher)
file.close()
def setup(self,count_rotor,plugboard):
"""
THIS METHOD FOR SETUP ENIGMA MACHINE &
SAVE FILE FOR ENIGMA
"""
self.rotor_plugboard_generator(count_rotor,plugboard)
self.read_test()
print('rotors = {}'.format(self.rotors))
print('plugboards = {}'.format(self.plugboard))