-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.py
241 lines (217 loc) · 8.8 KB
/
kernel.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
import tkinter as tk
import re
from bisect import bisect_left
from gui import GUI
# TODO
# Codestyle
# Documentation
# WHITESPACES???
# Case sensitivity???
class Kernel(GUI):
def __init__(self, master=None):
super().__init__(master)
self.prime_list = [
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97
]
self.key_pattern = re.compile(
r"(^\d+[,]{1} \d+ [:]{1} \d+[,]{1} \d+$)|(^\d+[,]{1} \d+$)")
self.encrypt_button.configure(
command=lambda: self.process(operation="encrypt"))
self.decrypt_button.configure(
command=lambda: self.process(operation="decrypt"))
self.student_number.trace("rw", self.get_prime)
self.surname.trace("rw", self.insert_a1z26)
self.generated_keys.trace("w", self.export_keys)
self.use_generated_keys_state.trace("w", self.export_keys)
self.a1z26_sum.trace("rw", self.get_q)
self.p.trace("rw", self.get_n)
self.q.trace("rw", self.get_n)
self.n.trace("rw", self.get_phi)
self.phi.trace("rw", self.get_e)
self.e.trace("rw", self.get_d)
self.d.trace("rw", self.get_keys)
def get_prime(self, *args):
try:
if 0 < self.student_number.get() <= 25:
self.p_field.configure(foreground="black")
self.p.set(self.prime_list[-self.student_number.get()])
else:
self.p_field.configure(foreground="gray70")
self.p.set("Invalid student number")
except tk.TclError as exception:
if str(exception)[-2:] == "\"\"":
self.p_field.configure(foreground="gray70")
self.p.set("Enter student number")
else:
self.p_field.configure(foreground="gray70")
self.p.set("Invalid student number")
def a1z26_encrypt(self, symbol):
if 8 <= ord(symbol) - 1071 < 33:
return (ord(symbol) - 1070)
elif ord(symbol) - 1071 <= 6:
return (ord(symbol) - 1071)
else:
return 7
def a1z26_decrypt(self, symbol):
if 8 <= symbol < 33:
return chr(symbol + 1070)
elif symbol <= 6:
return chr(symbol + 1071)
else:
return "ё"
def insert_a1z26(self, *args):
a1z26_list = []
if len(self.surname.get()) > 0:
self.a1z26_field.configure(foreground="black")
if re.fullmatch(r"[а-яёА-ЯЁ]*", self.surname.get()):
for letter in self.surname.get().lower():
a1z26_list.append(self.a1z26_encrypt(letter))
self.a1z26.set(a1z26_list)
self.a1z26_sum.set(sum(a1z26_list))
else:
self.a1z26_sum.set("")
self.a1z26_field.configure(foreground="gray70")
self.a1z26.set("Invalid surname")
else:
self.a1z26_sum.set("")
self.a1z26_field.configure(foreground="gray70")
self.a1z26.set("Enter surname")
def find_closest(self, number):
pos = bisect_left(self.prime_list, number)
if pos == 0:
return self.prime_list[0]
if pos == len(self.prime_list):
return self.prime_list[-1]
before = self.prime_list[pos - 1]
after = self.prime_list[pos]
if after - number < number - before:
return after
else:
return before
def get_q(self, *args):
try:
while self.a1z26_sum.get() > 100:
self.a1z26_sum.set(self.a1z26_sum.get() - 100)
self.q_field.configure(foreground="black")
self.q.set(self.find_closest(self.a1z26_sum.get()))
except tk.TclError:
if len(self.surname.get()) > 0:
self.q_field.configure(foreground="gray70")
self.q.set("Invalid surname")
else:
self.q_field.configure(foreground="gray70")
self.q.set("Enter surname")
def get_n(self, *args):
try:
self.n.set(self.p.get() * self.q.get())
self.n_field.configure(foreground="black")
except tk.TclError:
self.n_field.configure(foreground="gray70")
self.n.set("Invalid p or q")
def get_phi(self, *args):
try:
self.phi.set((self.p.get() - 1) * (self.q.get() - 1))
self.euler_func_field.configure(foreground="black")
except tk.TclError:
self.euler_func_field.configure(foreground="gray70")
self.phi.set("Invalid n")
def egcd(self, a, b):
if a == 0:
return b, 0, 1
else:
g, y, x = self.egcd(b % a, a)
return g, x - (b // a) * y, y
def modinv(self, a, m):
g, x, y = self.egcd(a, m)
if g != 1:
return "Modular inverse doesn`t exist"
else:
return x % m
def get_e(self, *args):
try:
for e in range(2, self.phi.get()):
if self.egcd(self.phi.get(), e)[0] == 1:
self.e_field.configure(foreground="black")
self.e.set(e)
break
except tk.TclError:
self.e_field.configure(foreground="gray70")
self.e.set("Invalid phi")
def get_d(self, *args):
try:
self.d.set(self.modinv(self.e.get(), self.phi.get()))
self.d_field.configure(foreground="black")
except tk.TclError:
self.d_field.configure(foreground="gray70")
self.d.set("Invalid e")
def get_keys(self, *args):
try:
self.generated_keys.set(
f"{self.e.get()}, {self.n.get()} : {self.d.get()}, {self.n.get()}"
)
self.generated_keys_field.configure(foreground="black")
except tk.TclError:
self.generated_keys_field.configure(foreground="gray70")
self.generated_keys.set("Invalid e, d or n")
def export_keys(self, *args):
if self.use_generated_keys_state.get():
if self.generated_keys.get() == "Invalid e, d or n":
self.key_field.configure(foreground="gray70")
self.key_field.delete(0, tk.END)
self.key_field.insert(0, self.generated_keys.get())
self.key_field.configure(state="readonly")
else:
if self.key_field.cget("state") == "readonly":
self.key_field.configure(state="normal")
self.key_field.configure(foreground="black")
self.key_field.delete(0, tk.END)
self.key_field.insert(0, self.generated_keys.get())
self.key_field.configure(state="readonly")
elif self.key_field.get() != self.key_field.label:
if self.key_field.cget("state") == "readonly":
self.key_field.configure(state="normal")
self.key_field.configure(foreground="gray70")
self.key_field.delete(0, tk.END)
self.key_field.insert(0, self.key_field.label)
def check_keys(self):
if re.fullmatch(self.key_pattern, self.key_field.get()):
keys = [
int(i) for i in re.split(",| |:", self.key_field.get())
if i.isdigit()
]
return keys
else:
self.messagebox.showerror("Error", "Wrong Key")
def process_block(self, keys, block):
return pow(block, keys[0], keys[1])
def process(self, operation=None):
if self.source_field.cget("foreground") == "black":
keys = self.check_keys()
if keys:
if operation == "encrypt":
encrypted = []
for block in self.source_field.get().lower():
encrypted.append(
self.process_block(keys[:2],
self.a1z26_encrypt(block)))
self.result.set(encrypted)
self.result_field.configure(foreground="black")
else:
if len(keys) > 2:
keys = keys[2:]
decrypted = []
source = [int(i) for i in self.source.get().split()]
for block in source:
decrypted.append(
self.a1z26_decrypt(
self.process_block(keys[:2], block)))
print(self.process_block(keys[:2], block))
self.result.set("".join(decrypted))
self.result_field.configure(foreground="black")
else:
self.messagebox.showerror("Error", "No source")
if __name__ == "__main__":
root = tk.Tk()
kernel = Kernel(master=root)
kernel.mainloop()