/
enigmaMachine.py
278 lines (189 loc) · 7.76 KB
/
enigmaMachine.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import random
class plugBoard:
def __init__(self):
self.connections = []
#Add cable/wire to the plugboard
def addWire(self, letterA, letterB):
#Convert letterA to char
letterA = ord(letterA)
if letterA > 96:
letterA -= 97
else:
letterA -= 65
#Convert letterA to char
letterB = ord(letterB)
if letterB > 96:
letterB -= 97
else:
letterB -= 65
self.connections.append([letterA, letterB])
#Get letter on other side of the cable
def getLetter(self, charLetter):
#Loop through every connection to find matching letter
for i in self.connections:
if i[0] == charLetter:
return i[1]
elif i[1] == charLetter:
return i[0]
#If no letter found, return inputLetter
return charLetter;
class reflectorWheel:
def __init__(self, settings):
self.rotorWires = []
self.numOfSymbols = 26
self.readSettings(settings)
#Reads in a settings string
def readSettings(self, settingsStr):
pos = 0;
for c in settingsStr:
symbolValue = ord(c)
if symbolValue > 96:
symbolValue -= 97
else:
symbolValue -= 65
self.rotorWires.append(symbolValue)
pos += 1
#Get position on other side
def getConnection(self, symbolNumber):
return self.rotorWires[symbolNumber]
class rotorWheel:
def __init__(self, settings = "", turnOverSymbol = "a"):
self.rotorWires = []
self.numOfSymbols = 26
self.turnOverSymbol = turnOverSymbol
if settings == "":
self.scrambleConnections()
else:
self.readSettings(settings)
#Reads in a settings string
def readSettings(self, settingsStr):
pos = 0;
for c in settingsStr:
symbolValue = ord(c)
if symbolValue > 96:
symbolValue -= 97
else:
symbolValue -= 65
self.rotorWires.append(symbolValue)
pos += 1
#Scramble connection
def scrambleConnections(self):
for i in range(self.numOfSymbols):
foundUnique = 0
while not foundUnique:
foundUnique = 1
randomMatch = random.randint(0, self.numOfSymbols)
for wire in self.rotorWires:
if wire == randomMatch:
foundUnique = 0
self.rotorWires.append(randomMatch)
#Rotate list
def rotate(self):
self.rotorWires = [self.rotorWires[-1]] + self.rotorWires[:-1]
#Get position on other side, 0 left direction 1 right direction
def getConnection(self, symbolNumber, direction):
if direction == 1:
return self.rotorWires[symbolNumber]
else:
pos = 0
for wire in self.rotorWires:
if wire == symbolNumber:
return pos
pos += 1
class rotorColumn:
def __init__(self):
self.rotorWheels = []
self.enteredLetters = 0
def sendThroughSymbol(self, symbolValue, direction):
currentValue = symbolValue
if direction == 1:
for rotorWheel in self.rotorWheels:
currentValue = rotorWheel.getConnection(currentValue, direction)
else:
for rotorWheel in reversed(self.rotorWheels):
currentValue = rotorWheel.getConnection(currentValue, 0)
return currentValue
#Rotate mechanism
def turnOver(self):
#Rotate first wheel, rightMost
self.rotorWheels[0].rotate()
currentPos = 0
#Turn first wheel and check if any reaches turnOverSymbol
for rotorWheel in self.rotorWheels:
#If the wheel reached turnOverSymbol, rotate wheel to right
if rotorWheel.rotorWires[0] == rotorWheel.turnOverSymbol:
#If rotorWheel exists on the left
if currentPos < len(self.rotorWheels):
self.rotorWheels[currentPos + 1].rotate()
currentPos += 1
#Add rotorWheel to column
def addWheel(self, rotorWheel):
self.rotorWheels.append(rotorWheel)
class enigmaMachine:
def __init__(self):
self.rotorColumn = rotorColumn()
self.plugBoard = plugBoard()
self.reflector = None
def encryptStr(self, message):
encryptedMessage = ""
for c in message:
symbolValue = ord(c) - 97
newSymbol = self.getSymbol(symbolValue)
character = chr(newSymbol + 97)
encryptedMessage += character
return encryptedMessage
def decryptStr(self, message):
decryptedMessage = ""
for c in message:
symbolValue = ord(c) - 97
newSymbol = self.getSymbol(symbolValue)
character = chr(newSymbol + 97)
decryptedMessage += character
return decryptedMessage
#Send through letter
def getSymbol(self, symbolVal):
#Through plugboard
nextSymbol = self.plugBoard.getLetter(symbolVal)
#Through rotorColumn
nextSymbol = self.rotorColumn.sendThroughSymbol(symbolVal, 1)
#Through reflector
nextSymbol = self.reflector.getConnection(nextSymbol)
#Back through rotorColumn
nextSymbol = self.rotorColumn.sendThroughSymbol(nextSymbol, 0)
#Back through plugboard
nextSymbol = self.plugBoard.getLetter(nextSymbol)
#Rotate wheels
self.rotorColumn.turnOver()
return nextSymbol
#Add rotorWheel to column
def plugBoardAdd(self, letterA, letterB):
self.plugBoard.addWire(letterA, letterB)
#Add rotorWheel to column
def addWheel(self, settings = "", turnOverSymbol = "A"):
tempRotorWheel = rotorWheel(settings, turnOverSymbol)
self.rotorColumn.addWheel(tempRotorWheel)
#Add reflectorWheel to the enigma machine
def addReflector(self, settings = ""):
self.reflector = reflectorWheel(settings)
#Create Machine A
enigmaMachineA = enigmaMachine()
#Add rotorWheel
enigmaMachineA.addWheel("JGDQOXUSCAMIFRVTPNEWKBLZYH", "Q")
enigmaMachineA.addWheel("NTZPSFBOKMWRCJDIVLAEYUXHGQ", "E")
enigmaMachineA.addWheel("JVIUBHTCDYAKEQZPOSGXNRMWFL", "V")
#Add reflector
enigmaMachineA.addReflector("QYHOGNECVPUZTFDJAXWMKISRBL")
#Create Machine B
enigmaMachineB = enigmaMachine()
#Add rotorWheel
enigmaMachineB.addWheel("JGDQOXUSCAMIFRVTPNEWKBLZYH", "Q")
enigmaMachineB.addWheel("NTZPSFBOKMWRCJDIVLAEYUXHGQ", "E")
enigmaMachineB.addWheel("JVIUBHTCDYAKEQZPOSGXNRMWFL", "V")
#Add reflector
enigmaMachineB.addReflector("QYHOGNECVPUZTFDJAXWMKISRBL")
message = "iloveyou"
print("Message: " + message)
encryptedMessage = enigmaMachineA.encryptStr(message)
print("Encrypted Message: " + encryptedMessage)
decryptedMessage = enigmaMachineB.decryptStr(encryptedMessage)
print("Decrypted Message: " + decryptedMessage)