forked from Gravitar64/A-beautiful-code-in-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Teil_44_Enigma.py
112 lines (95 loc) · 2.95 KB
/
Teil_44_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
from collections import deque
def str2num(zeichenkette):
return deque([ord(c)-65 for c in zeichenkette])
walzen_r = ['EKMFLGDQVZNTOWYHXUSPAIBRCJ', # I
'AJDKSIRUXBLHWTMCQGZNPYFVOE', # II
'BDFHJLCPRTXVZNYEIWGAKMUSQO', # III
'ESOVPZJAYQUIRHXLNFTGKDCMWB', # IV
'VZBRGITYUPSDNHLXAWMJQOFECK', # V
'JPGVOUMFYQBENHZRDKASXLICTW', # VI
'NZJHGRCXMYSWBOUFAIVLPEKQDT', # VII
'FKQHTLXOCBJSPDZRAMEWNIUYGV'] # VIII
walzen_r = [str2num(zeile) for zeile in walzen_r]
walzen_l = deque(range(26))
UKWs = ['EJMZALYXVBWFCRQUONTSPIKHGD', # UKW A
'YRUHQSLDPXNGOKMIEBFZCWVJAT', # UKW B
'FVPJIAOYEDRZXWGCTKUQSBNMHL'] # UKW C
UKWs = [str2num(zeile) for zeile in UKWs]
kerbenKat = "Q E V J Z ZM ZM ZM"
kerbenKat = [str2num(zeile) for zeile in kerbenKat.split()]
class Walze():
def __init__(self, nr, w_pos, r_pos):
self.w_pos = w_pos
self.r_pos = r_pos
self.verdr_r = walzen_r[nr-1].copy()
self.verdr_l = walzen_l.copy()
self.kerben = kerbenKat[nr-1]
self.setup()
def setup(self):
offset = self.r_pos-self.w_pos
self.verdr_l.rotate(offset)
self.verdr_r.rotate(offset)
self.kerben = [(k-self.r_pos) % 26 for k in self.kerben]
def show(self):
for nr in self.verdr_l:
print(chr(nr+65),end='')
print()
for nr in self.verdr_r:
print(chr(nr+65),end='')
print()
for nr in self.kerben:
print(chr(nr+65),end='')
print()
def click(self):
self.verdr_l.rotate(-1)
self.verdr_r.rotate(-1)
def schaltung(self):
return self.verdr_l[0] in self.kerben
class Enigma():
def __init__(self):
self.walzen = []
self.ukw = []
self.steckerbr = {}
def setup(self, nr_ukw, nr_walzen, w_pos, r_pos, paare_steckerbr):
for i, nr in enumerate(nr_walzen):
wpos = ord(w_pos[i])-65
rpos = r_pos[i]-1
self.walzen.append(Walze(nr, wpos, rpos))
self.ukw = UKWs[nr_ukw-1]
for b1,b2 in paare_steckerbr.split():
self.steckerbr[ord(b1)-65] = ord(b2)-65
self.steckerbr[ord(b2)-65] = ord(b1)-65
def rotiere(self):
links, mitte, rechts = self.walzen
if mitte.schaltung():
mitte.click()
links.click()
elif rechts.schaltung():
mitte.click()
rechts.click()
def umwandeln(e, text):
u_text = ""
text = text.upper()
for c in text:
c = ord(c)-65
if c < 0 or c> 26: continue
e.rotiere()
c = e.steckerbr.get(c,c)
for w in reversed(e.walzen):
c = w.verdr_r[c]
c = w.verdr_l.index(c)
c = e.ukw[c]
for w in e.walzen:
c = w.verdr_l[c]
c = w.verdr_r.index(c)
c = e.steckerbr.get(c,c)
u_text += chr(c+65)
return u_text
enigma = Enigma()
enigma.setup(2, [2,4,5], "BLA", [2,21,12], "AV BS CG DL FU HZ IN KM OW RX")
print('##### ENIGMA SIMULATOR #####')
text = input('Texteingabe: ')
u_text = umwandeln(enigma, text)
u_text = u_text.replace('X', ' ')
u_text = u_text.replace('Q', 'CH')
print(u_text)