-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotor.py
191 lines (164 loc) · 5.4 KB
/
rotor.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
ROTOR_DICT = {
'I': {
'name': 'I',
'output': 'EKMFLGDQVZNTOWYHXUSPAIBRCJ',
'notch': 'Q'
},
'II': {
'name': 'II',
'output': 'AJDKSIRUXBLHWTMCQGZNPYFVOE',
'notch': 'E'
},
'III': {
'name': 'III',
'output': 'BDFHJLCPRTXVZNYEIWGAKMUSQO',
'notch': 'V'
},
'IV': {
'name': 'IV',
'output': 'ESOVPZJAYQUIRHXLNFTGKDCMWB',
'notch': 'J'
},
'V': {
'name': 'V',
'output': 'VZBRGITYUPSDNHLXAWMJQOFECK',
'notch': 'Z'
},
'VI': {
'name': 'VI',
'output': 'JPGVOUMFYQBENHZRDKASXLICTW',
'notch': 'ZM'
},
'VII': {
'name': 'VII',
'output': 'NZJHGRCXMYSWBOUFAIVLPEKQDT',
'notch': 'ZM'
},
'VIII': {
'name': 'VII',
'output': 'FKQHTLXOCBJSPDZRAMEWNIUYGV',
'notch': 'ZM'
}
}
ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def get_rotor_choices():
return list(ROTOR_DICT.keys())
class Rotor:
"""A class to represnt a single rotor in the Engima machine.
Attributes
----------
name: str
The name of the rotor.
output: str
The permutation of the alphabet representing the scrambling.
notch: str
The letter which after stepping past will cause the next rotor to turnover.
printing_enabled: bool
Determines whether to print the scrambling route to the terminal.
pos: int
The index of the alphabet representing the current letter of the rotor.
ring_setting: int
The number of letters to offset the scrambling by.
Methods
-------
forward(input)
Returns the index in the alphabet of the letter after scrambling right to left.
backward(input)
Returns the index in the alphabet of the letter after scrambling right to left.
current_letter_setting()
Returns the letter the rotor is currently on.
step()
Steps the rotor one step.
"""
def __init__(self, model, printing, start_letter, ring_setting):
"""
Parameters
----------
model: str
The name of the rotor.
printing: bool
Determines whether to print the scrambling route to the terminal.
start_letter: str
The letter to start the rotor on.
ring_setting: int
The number of letters to offset the scrambling by.
"""
self.name = ROTOR_DICT[model]['name']
self.output = ROTOR_DICT[model]['output']
self.notch = ROTOR_DICT[model]['notch']
self.printing_enabled = printing
self.pos = ALPHABET.find(start_letter)
# Creating the new output alphabet given the ring settings
self.ring_setting = ring_setting+1
if (ring_setting != 0):
dot_pos = self.output.find('A')
new_wiring = ''
for char in self.output:
new_wiring += ALPHABET[(ALPHABET.find(char)+ring_setting) % 26]
new_dot_pos = (dot_pos + ring_setting) % 26
while not new_wiring[new_dot_pos] == ALPHABET[ring_setting]:
new_wiring = new_wiring[-1:] + new_wiring[:-1]
self.output = new_wiring
def forward(self, input):
"""Scrambles the letter in the forward direction.
Forward is determined to be the electrical signal travelling towards the reflector in the physical machine.
Parameters
----------
input: int
The index in the alphabet of the input letter.
Returns
-------
output: int
The index in the alphabet of the scrambled letter.
"""
output = (input+self.pos) % 26
cipherletter = self.output[output]
if(self.printing_enabled):
print('->', cipherletter, '(forward through rotor {})'.format(self.name))
output = (ALPHABET.find(cipherletter)-self.pos) % 26
return output
def backward(self, input):
"""Scrambles the letter in the backward direction.
Backward is determined to be the electrical signal travelling away from the reflector in the physical machine.
Parameters
----------
input: int
The index in the alphabet of the input letter.
Returns
-------
output: int
The index in the alphabet of the scrambled letter.
"""
output = (input+self.pos) % 26
output = self.output.find(ALPHABET[output])
cipherletter = ALPHABET[output]
if(self.printing_enabled):
print('->', cipherletter,
'(backwards through rotor {})'.format(self.name))
output = (output-self.pos) % 26
return output
def current_letter_setting(self):
"""Finds the current letter the rotor is on.
This is equivalent to the outward showing letter on the front display on the physical machine.
Parameters
----------
None
Returns
-------
ALPHABET[self.pos]: str
The current letter the rotor is on.
"""
return ALPHABET[self.pos]
def step(self):
"""Steps the rotor.
Also prints the name of the rotor if printing_enabled is True.
Parameters
----------
None
Returns
-------
None
"""
if(self.printing_enabled):
print("Stepping rotor", self.name)
self.pos = (self.pos+1) % 26