-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
338 lines (319 loc) · 12.3 KB
/
gui.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import tkinter as tk
from tkinter import filedialog, ttk, messagebox
import tkinter.scrolledtext as scrolled_text
from png_image import PNGImage
from block_cipher import ElectronicCodeBook, Counter
from PIL import Image, ImageTk
from rsa import MyRSA, PyRSA
class GUI:
BlockCiphers = {
"ElectronicCodeBook": ElectronicCodeBook,
"Counter": Counter,
}
RsaKeySize = {
"1024": 1024,
"2048": 2048,
"4096": 4096,
}
def __init__(self):
# main window #
self.window = tk.Tk()
self.window.geometry("1345x800")
self.window.configure(background="black")
# current image #
self.png = PNGImage("data/dices.png")
self.rsa = None
image = self.get_photo_image()
# block cipher
self.block_cipher = tk.StringVar()
# rsa key_size
self.rsa_size = tk.StringVar()
# rsa selection
self.rsa_selection = tk.IntVar()
# widgets #
# label with file path
self.file_label = tk.Label(
self.window,
text=f"File path: {self.png.image_path}",
font=("Helvetica", "12", "bold"),
bg="black",
fg="white"
)
# label with Block Cipher
self.bc_label = tk.Label(
self.window,
text="Block Cipher:",
font=("Helvetica", "14", "bold"),
bg="black",
fg="white",
)
# label with RSA key size
self.rsa_size_label = tk.Label(
self.window,
text="Key Size:",
font=("Helvetica", "14", "bold"),
bg="black",
fg="white",
)
# label with modulus
self.mod_key_label = tk.Label(
self.window,
text="Modulus:",
font=("Helvetica", "14", "bold"),
bg="black",
fg="white",
)
# label with public key exponent
self.pub_key_exp = tk.Label(
self.window,
text="Public Exponent:",
font=("Helvetica", "14", "bold"),
bg="black",
fg="white",
)
# label with private key exponent
self.pvt_key_exp = tk.Label(
self.window,
text="Private Exponent:",
font=("Helvetica", "14", "bold"),
bg="black",
fg="white",
)
# entry with modulus
self.mod_entry = tk.Entry(
font=("Helvetica", "12", "bold"),
)
# entry with public key exponent
self.pub_key_entry = tk.Entry(
font=("Helvetica", "12", "bold"),
)
# entry with private key exponent
self.pvt_key_entry = tk.Entry(
font=("Helvetica", "12", "bold"),
)
# button for browsing files
self.file_button = tk.Button(
self.window,
text="Browse files",
bg="black",
fg="white",
command=self.browse_files
)
# button to anonymize images
self.anonymize_button = tk.Button(
self.window,
text="Anonymize",
bg="black",
fg="white",
command=self.anonymize_image
)
# button for showing image, chunks (hidden)
self.image_chunks_button = tk.Button(
self.window,
text="Image/Chunks",
bg="black",
fg="white",
command=self.display_image_chunks
)
# button for showing fft
self.fft_button = tk.Button(
self.window,
text="FFT",
bg="black",
fg="white",
command=self.get_fft
)
# button for encrypting image
self.encrypt_button = tk.Button(
self.window,
text="Encrypt",
bg="black",
fg="white",
command=self.encrypt
)
# button for decrypting image
self.decrypt_button = tk.Button(
self.window,
text="Decrypt",
bg="black",
fg="white",
command=self.decrypt
)
# button for generating rsa keys
self.rsa_gen_button = tk.Button(
self.window,
text="Generate Keys",
bg="black",
fg="white",
command=self.generate_keys,
)
# button for saving images to out.png
self.save_button = tk.Button(
self.window,
text="Save",
bg="black",
fg="white",
command=self.save_image,
)
# block cipher combo_box
self.block_cipher_cbox = ttk.Combobox(
self.window,
values=list(self.BlockCiphers.keys()),
state="readonly",
font=("Helvetica", "12", "bold"),
textvariable=self.block_cipher,
)
self.block_cipher_cbox.set(list(self.BlockCiphers.keys())[0])
# rsa key size combo_box
self.rsa_size_cbox = ttk.Combobox(
self.window,
values=list(self.RsaKeySize.keys()),
state="readonly",
font=("Helvetica", "12", "bold"),
textvariable=self.rsa_size,
)
self.rsa_size_cbox.set(list(self.RsaKeySize.keys())[0])
# rsa selection radio buttons
self.rsa_radio_buttons = [
tk.Radiobutton(self.window, text="MyRSA", variable=self.rsa_selection, value=1, font=("Helvetica", "10", "bold")),
tk.Radiobutton(self.window, text="PyRSA", variable=self.rsa_selection, value=2, font=("Helvetica", "10", "bold")),
]
for button in self.rsa_radio_buttons:
button.configure(background="black", foreground="azure3", activebackground="black", activeforeground="white")
# displayed image
self.image = tk.Label(
self.window,
image=image
)
self.image.image = image
# fft magnitude image (hidden)
self.fft_mag_image = tk.Label(self.window)
# fft magnitude image (hidden)
self.fft_phase_image = tk.Label(self.window)
# text with parsed PNG chunks
self.text_scroll = scrolled_text.ScrolledText(
self.window,
bg="black",
fg="white"
)
self.text_scroll.insert(tk.INSERT, self.png.to_string())
self.text_scroll.configure(state="disabled")
# grid #
self.file_label.grid(row=4, column=0, columnspan=4, sticky="W", padx=10, pady=10)
self.bc_label.grid(row=2, column=1, padx=10, pady=10)
self.rsa_size_label.grid(row=1, column=1, padx=10, pady=10)
self.mod_key_label.grid(row=0, column=4, padx=10, pady=10)
self.pub_key_exp.grid(row=1, column=4, padx=10, pady=10)
self.pvt_key_exp.grid(row=2, column=4, padx=10, pady=10)
self.mod_entry.grid(row=0, column=5, padx=10, pady=10, sticky="NSEW")
self.pub_key_entry.grid(row=1, column=5, padx=10, pady=10, sticky="NSEW")
self.pvt_key_entry.grid(row=2, column=5, padx=10, pady=10, sticky="NSEW")
self.file_button.grid(row=0, column=0, padx=10, pady=10, sticky="NSEW")
self.anonymize_button.grid(row=1, column=0, padx=10, pady=10, sticky="NSEW")
self.rsa_radio_buttons[0].grid(row=0, column=1, padx=10, pady=10, sticky="W")
self.rsa_radio_buttons[1].grid(row=0, column=1, padx=10, pady=10, sticky="E")
self.fft_button.grid(row=2, column=0, padx=10, pady=10, sticky="NSEW")
self.image_chunks_button.grid(row=2, column=0, padx=10, pady=10, sticky="NSEW")
self.encrypt_button.grid(row=0, column=2, padx=10, pady=10, sticky="NSEW")
self.decrypt_button.grid(row=0, column=3, padx=10, pady=10, sticky="NSEW")
self.save_button.grid(row=2, column=3, padx=10, pady=10, sticky="NSEW")
self.rsa_gen_button.grid(row=1, column=3, padx=10, pady=10, sticky="NSEW")
self.rsa_size_cbox.grid(row=1, column=2, padx=10, pady=10, sticky="NSEW")
self.block_cipher_cbox.grid(row=2, column=2, padx=10, pady=10, sticky="NSEW")
self.image.grid(row=3, column=0, columnspan=4, padx=10)
self.text_scroll.grid(row=3, column=4, columnspan=2, padx=10, sticky="NSEW")
self.window.columnconfigure(0, weight=1)
self.window.columnconfigure(1, weight=1)
self.window.columnconfigure(2, weight=1)
self.window.columnconfigure(3, weight=1)
self.window.columnconfigure(4, weight=1)
self.window.columnconfigure(5, weight=3)
# main loop #
self.window.mainloop()
def get_photo_image(self):
image = self.png.get_image()
image.thumbnail((640, 720), Image.ANTIALIAS)
return ImageTk.PhotoImage(image)
def anonymize_image(self):
self.png.anonymize("data/out.png") # self.png.image_path
self.png = PNGImage("data/out.png") # self.png.image_path
self.update_scroll_text()
def update_image(self):
image = self.get_photo_image()
self.image.configure(image=image)
self.image.image = image
def update_scroll_text(self):
self.text_scroll.configure(state="normal")
self.text_scroll.delete(1.0, tk.END)
self.text_scroll.insert(tk.INSERT, self.png.to_string())
self.text_scroll.configure(state="disabled")
def get_fft(self):
magnitude, phase = self.png.fft()
magnitude.thumbnail((640, 720), Image.ANTIALIAS)
phase.thumbnail((640, 720), Image.ANTIALIAS)
magnitude = ImageTk.PhotoImage(magnitude)
phase = ImageTk.PhotoImage(phase)
self.fft_mag_image.configure(image=magnitude)
self.fft_phase_image.configure(image=phase)
self.fft_mag_image.image = magnitude
self.fft_phase_image.image = phase
self.display_fft()
def display_fft(self):
self.image.grid_forget()
self.text_scroll.grid_forget()
self.fft_button.grid_forget()
self.fft_mag_image.grid(row=3, column=0, columnspan=4, padx=10)
self.fft_phase_image.grid(row=3, column=4, columnspan=2, padx=10)
self.image_chunks_button.grid(row=2, column=0, padx=10, pady=10, sticky="NSEW")
def display_image_chunks(self):
self.fft_mag_image.grid_forget()
self.fft_phase_image.grid_forget()
self.image_chunks_button.grid_forget()
self.image.grid(row=3, column=0, columnspan=4, padx=10)
self.text_scroll.grid(row=3, column=4, columnspan=2, padx=10, sticky="NSEW")
self.fft_button.grid(row=2, column=0, padx=10, pady=10, sticky="NSEW")
def browse_files(self):
filename = tk.filedialog.askopenfilename(title="Select file", filetypes=[("image files", ".png")])
if filename == "":
return
self.png = PNGImage(filename)
self.update_image()
self.update_scroll_text()
self.file_label.configure(text=f"File path: {filename}")
self.display_image_chunks()
def encrypt(self):
if not self.rsa:
return messagebox.showinfo('Error', 'Keys not generated')
if not self._update_keys():
return
self.png.encrypt(rsa=self.rsa, cipher_block=self.BlockCiphers[self.block_cipher.get()])
self.update_image()
def decrypt(self):
if not self.rsa:
return messagebox.showinfo('Error', 'Keys not generated')
if not self._update_keys():
return
self.png.decrypt(rsa=self.rsa, cipher_block=self.BlockCiphers[self.block_cipher.get()])
self.update_image()
def _update_keys(self):
try:
self.rsa.set_keys(int(self.mod_entry.get()), int(self.pub_key_entry.get()), int(self.pvt_key_entry.get()))
except ValueError:
messagebox.showinfo('Error', 'Some key is not an integer')
return False
return True
def generate_keys(self):
num_bits = self.RsaKeySize[self.rsa_size.get()]
match self.rsa_selection.get():
case 1:
self.rsa = MyRSA(num_bits)
case 2:
self.rsa = PyRSA(num_bits)
case _:
return messagebox.showinfo('Error', 'RSA not selected')
self.rsa.set_keys(*self.rsa.generate_keys())
self.mod_entry.insert(0, str(self.rsa.mod))
self.pub_key_entry.insert(0, str(self.rsa.pub_exp))
self.pvt_key_entry.insert(0, str(self.rsa.pvt_exp))
def save_image(self):
self.png.save_image("data/out.png")