-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python - Enigma GUI.txt
146 lines (120 loc) · 5.05 KB
/
Python - Enigma GUI.txt
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
import tkinter as tk
class EnigmaMachine:
def __init__(self):
# Initialize the rotors and reflector
self.rotor1 = Rotor('EKMFLGDQVZNTOWYHXUSPAIBRCJ', 'Q')
self.rotor2 = Rotor('AJDKSIRUXBLHWTMCQGZNPYFVOE', 'E')
self.rotor3 = Rotor('BDFHJLCPRTXVZNYEIWGAKMUSQO', 'V')
self.reflector = Reflector('YRUHQSLDPXNGOKMIEBFZCWVJAT')
def encrypt(self, plaintext):
# Iterate through each character in the plaintext
ciphertext = ''
for char in plaintext:
# Encode the character using the rotors and reflector
char = self.rotor1.encode(char)
char = self.rotor2.encode(char)
char = self.rotor3.encode(char)
char = self.reflector.encode(char)
char = self.rotor3.encode(char, reverse=True)
char = self.rotor2.encode(char, reverse=True)
char = self.rotor1.encode(char, reverse=True)
# Add the encoded character to the ciphertext
ciphertext += char
return ciphertext
class Rotor:
def __init__(self, mapping, offset):
self.mapping = mapping
self.offset = ord(offset) - ord('A')
def encode(self, char, reverse=False):
if reverse:
# Look up the character in the mapping and get its index
char_index = self.mapping.index(char)
# Subtract the offset to get the original index
char_index -= self.offset
# Wrap around if necessary
char_index %= 26
# Look up the character at the original index in the mapping
char = self.mapping[char_index]
else:
# Add the offset to the character's index
char_index = ord(char) - ord('A') + self.offset
# Wrap around if necessary
char_index %= 26
# Look up the character at the modified index in the mapping
char = self.mapping[char_index]
return char
class Reflector:
def __init__(self, mapping):
self.mapping = mapping
def encode(self, char):
# Look up the character in the mapping and get its index
char_index = self.mapping.index(char)
# Look up the character at the opposite index in the mapping
char = self.mapping[25 - char_index]
return char
class EnigmaGUI(tk.Tk):
def __init__(self):
super().__init__()
self.title("Enigma Machine")
# Create the Enigma machine
self.machine
V2:
import tkinter as tk
class EnigmaMachine:
def __init__(self):
# Initialize the rotors and reflector
self.rotor1 = Rotor('EKMFLGDQVZNTOWYHXUSPAIBRCJ', 'Q')
self.rotor2 = Rotor('AJDKSIRUXBLHWTMCQGZNPYFVOE', 'E')
self.rotor3 = Rotor('BDFHJLCPRTXVZNYEIWGAKMUSQO', 'V')
self.reflector = Reflector('YRUHQSLDPXNGOKMIEBFZCWVJAT')
def encrypt(self, plaintext):
# Iterate through each character in the plaintext
ciphertext = ''
for char in plaintext:
# Encode the character using the rotors and reflector
char = self.rotor1.encode(char)
char = self.rotor2.encode(char)
char = self.rotor3.encode(char)
char = self.reflector.encode(char)
char = self.rotor3.encode(char, reverse=True)
char = self.rotor2.encode(char, reverse=True)
char = self.rotor1.encode(char, reverse=True)
# Add the encoded character to the ciphertext
ciphertext += char
return ciphertext
class Rotor:
def __init__(self, mapping, offset):
self.mapping = mapping
self.offset = ord(offset) - ord('A')
def encode(self, char, reverse=False):
if reverse:
# Look up the character in the mapping and get its index
char_index = self.mapping.index(char)
# Subtract the offset to get the original index
char_index -= self.offset
# Wrap around if necessary
char_index %= 26
# Look up the character at the original index in the mapping
char = self.mapping[char_index]
else:
# Add the offset to the character's index
char_index = ord(char) - ord('A') + self.offset
# Wrap around if necessary
char_index %= 26
# Look up the character at the modified index in the mapping
char = self.mapping[char_index]
return char
class Reflector:
def __init__(self, mapping):
self.mapping = mapping
def encode(self, char):
# Look up the character in the mapping and get its index
char_index = self.mapping.index(char)
# Look up the character at the opposite index in the mapping
char = self.mapping[25 - char_index]
return char
class EnigmaGUI(tk.Tk):
def __init__(self):
super().__init__()
self.title("Enigma Machine")
# Create the