-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenigma.py
144 lines (117 loc) · 2.96 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
144
'''
This code ciphers and dechipers messages through Enigma.
- Current version runs files with an explicit structure. More information on example file.
- To be added:
--print message in more readable form
---Pengu1ne
'''
import time
alphabet = 'abcdefghijklmnopqrstuvwxyz'
''' Read the file line by line and turn it lower case '''
with open('test_iii.txt', 'r') as file:
lines = file.readlines()
'''
Constants
'''
shift = int(lines[1])
msg = lines[5]
l = len(msg)-1
msg = msg[:l]
crypt = lines[0]
crypt = crypt[:6]
add_shift = []
for a in range(l):
add_shift.append(shift+a)
'''
The shift. Includes both encoding and decoding, thus, this part is called for
different purposes later.
'''
def shift_letters(message):
indexes = []
new_msg = []
if crypt == 'decode':
for i in message:
indexes.append(alphabet.find(i))
for j in range(l):
new_msg.append(alphabet[(indexes[j]-add_shift[j])%26])
else:
for i in message:
indexes.append(alphabet.find(i))
for j in range(l):
new_msg.append(alphabet[(indexes[j]+add_shift[j])%26])
return new_msg
'''
Encoding i.e. running the message through rotors. The function calls previous
function for shifted letters.
'''
def rotors_en():
r = 1
shifted = shift_letters(msg)
indexes = []
rotored = []
while r < 4:
r +=1
tobe_rotored = []
'''
The indexes are determined.
'''
if r == 2:
indexes = []
for i in shifted:
indexes.append(alphabet.index(i))
else:
indexes = []
for i in rotored:
indexes.append(alphabet.index(i))
'''
Looping through rotors.
'''
for j in indexes:
tobe_rotored.append(lines[r][j])
rotored = tobe_rotored
return rotored
'''
The decoding i.e. going rotors backwards.
'''
def rotor_de():
r = 5
indexes = []
rotored = []
while r > 2:
r -=1
tobe_rotored = []
'''
Determining indexes
'''
if r == 4:
for i in msg:
indexes.append(lines[r].index(i))
else:
indexes = []
for i in rotored:
indexes.append(lines[r].find(i))
'''
Going back through rotors
'''
for j in indexes:
tobe_rotored.append(alphabet[j])
rotored = tobe_rotored
return rotored
'''
Encode or decode
'''
def read_msg():
if crypt == 'decode':
print('Decoding...')
time.sleep(1)
print('The decoded message is:')
print(shift_letters(rotor_de()))
elif crypt == 'encode':
print('Encoding...')
time.sleep(1)
print('The encoded message is:')
print(rotors_en())
''' Run the code - and the right answer for the test runs'''
read_msg()
#print(rotor_de())
#print(shifted_letters(rotor_de()))