-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptUI.py
338 lines (263 loc) · 14.5 KB
/
cryptUI.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 os
import tkinter.messagebox
from tkinter import *
from tkinter import ttk
import sumoGame
from PIL import ImageTk, Image
from cryptography.fernet import Fernet, InvalidToken
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
resource_path('venv/UI/Sumocrypt.png')
resource_path('venv/UI/tizzycrypt.ico')
class Crypt:
def make_key(self): # Makes the encryption key and writes it to a file
self.key_make = Fernet.generate_key()
key_file = open('key.key', 'wb')
key_file.write(self.key_make)
key_file.close()
key_file = open('key.key', 'rb')
self.key = key_file.read()
key_file.close()
tkinter.messagebox.showinfo('Done', "Key generated. Your key is: {} ".format(self.key) + "You will find the "
"file located in "
"the project "
"directory "
"as 'key.key' ")
def get_key(self): # Reads the .key file and stores the key as a variable
key_file = open('key.key', 'rb')
self.key = key_file.read()
key_file.close()
class Tkinter_GUI:
def __init__(self, master): # Defines beginning frames of the program
self.master = master
self.f1 = Frame(master, width=500, height=600)
self.f1.pack()
self.f2 = Frame(master, width=500, height=600)
self.f2.pack()
# Defines image to be used as background
path = "venv/UI/Sumocrypt.png"
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(self.f1, image=img)
panel.photo = img
panel.grid(column=0, row=1, )
welcome = Label(self.f1, font=("Calibri", 16, "bold"), borderwidth=25,
text='Welcome to SumoCrypt, an all in one base64 encrypter and decrypter.')
welcome.grid(column=0, row=0)
state = Label(self.f1, text='Please Select an Option.', font=('Calibri', 14, 'bold'), borderwidth=25)
state.grid(column=0, row=2)
gt_gen_key_btn = Button(self.f2, font=('Calibri', 16, 'bold'), text='Generate Key',
command=self.gt_gen_key)
gt_gen_key_btn.grid(column=0, row=1, sticky=W)
gt_encrypt_btn = Button(self.f2, text='Encrypt Files', font=('Calibri', 16, 'bold'), command=self.gt_crypt)
gt_encrypt_btn.grid(column=1, row=1)
gt_decrypt_btn = Button(self.f2, text='Decrypt Files', font=('Calibri', 16, 'bold'), command=self.gt_decrypt)
gt_decrypt_btn.grid(column=2, row=1)
gt_chat_btn = Button(self.f2, text='Chat', font=('Calibri', 16, 'bold'), command=self.runGame)
gt_chat_btn.grid(column=3, row=1)
def gt_gen_key(self):
self.var = 0
self.f1.pack_forget()
self.f2.pack_forget()
self.f_key = Frame(self.master, width=500, height=600)
self.f_key.pack()
crypt = Crypt() # Calls the previous class "Crypt" and allows the functions to be used.
key_head = Label(self.f_key, text='Press the "Generate Key" to create a random key.',
font=('Calibri', 16, 'bold'), borderwidth=100)
key_head.grid(row=0, column=0, columnspan=2)
key_btn = Button(self.f_key, text='Generate Key', font=('Calibri', 14, 'bold'), command=crypt.make_key)
key_btn.grid(row=1, column=1)
k_menu_btn = Button(self.f_key, text='Return to Menu', font=('Calibri', 14, 'bold'), command=self.rtr_menu)
k_menu_btn.grid(row=1, column=0)
def gt_crypt(self):
self.var = 1
self.tizzy = 0
self.f1.pack_forget()
self.f2.pack_forget()
self.f_crypt = Frame(self.master, width=500, height=600)
self.f_crypt.pack()
crypt_head = Label(self.f_crypt, text='Please enter in the text you wish to encrypt and chose your key.',
font=('Calibri', 16, 'bold'), borderwidth=25)
crypt_head.grid(row=0, column=0, columnspan=4)
lbl_ent_msg = Label(self.f_crypt, text='Text to be encrypted:', font=('Calibri', 14))
lbl_ent_msg.grid(column=1, row=1)
self.ent_msg = Entry(self.f_crypt, width=20)
self.ent_msg.grid(column=2, row=1, pady=50)
lbl_ent_key = Label(self.f_crypt, text='Enter custom key:', font=('Calibri', 14))
lbl_ent_key.grid(column=1, row=2)
self.ent_key = Entry(self.f_crypt, width=20)
self.ent_key.grid(column=2, row=2, pady=50)
crypt_btn_lc = Button(self.f_crypt, text='Encrypt with local key', font=('Calibri', 14, 'bold'),
command=self.crypt_msg_func_lc)
crypt_btn_lc.grid(row=3, column=2)
self.crypt_btn_cs = Button(self.f_crypt, text='Encrypt with custom key', font=('Calibri', 14, 'bold'),
command=self.crypt_msg_func_cs)
self.crypt_btn_cs.grid(row=3, column=4)
cry_menu_btn = Button(self.f_crypt, text='Return to Menu', font=('Calibri', 14, 'bold'), command=self.rtr_menu)
cry_menu_btn.grid(row=3, column=0)
self.chk_cs = ttk.Checkbutton(self.f_crypt, command=self.key_chk_btn_func)
self.chk_cs.grid(row=2, column=0)
self.chk_cs.state(['!alternate'])
self.crypt_btn_cs.configure(state='disabled')
self.ent_key.configure(state='disabled')
def gt_decrypt(self):
self.tizzy = 1
self.var = 2
self.f1.pack_forget()
self.f2.pack_forget()
self.f_decrypt = Frame(self.master, width=500, height=600)
self.f_decrypt.pack()
de_head_1 = Label(self.f_decrypt, text='Please choose what file you wish to decrypt.', font=('Calibri',
16, 'bold'),
borderwidth=50)
de_head_1.grid(row=0, column=0, columnspan=4)
de_lbl_ent_key = Label(self.f_decrypt, text='Enter encrypted text:', font=('Calibri', 14))
de_lbl_ent_key.grid(column=1, row=1)
de_lbl_ent_crypt = Label(self.f_decrypt, text='Enter in key:', font=('Calibri', 14))
de_lbl_ent_crypt.grid(column=1, row=2)
self.de_ent_key = Entry(self.f_decrypt, width=20)
self.de_ent_key.grid(column=2, row=2, pady=50)
self.ent_crypt = Entry(self.f_decrypt, width=20)
self.ent_crypt.grid(column=2, row=1, pady=50)
decrypt_btn_lc = Button(self.f_decrypt, text='Decrypt saved file with local key', font=('Calibri', 14, 'bold'),
command=self.decrypt_msg_func_lc)
decrypt_btn_lc.grid(row=3, column=2)
self.decrypt_btn_cs = Button(self.f_decrypt, text='Decrypt inputted text with custom key',
font=('Calibri', 14, 'bold'),
command=self.decrypt_msg_func_cs)
self.decrypt_btn_cs.grid(row=3, column=4)
de_menu_btn = Button(self.f_decrypt, text='Return to Menu', font=('Calibri', 14, 'bold'), command=self.rtr_menu)
de_menu_btn.grid(row=3, column=0)
self.chk_cs = ttk.Checkbutton(self.f_decrypt, command=self.key_chk_btn_func)
self.chk_cs.grid(row=1, column=0)
self.chk_cs.state(['!alternate'])
self.decrypt_btn_cs.configure(state='disabled')
self.de_ent_key.configure(state='disabled')
self.ent_crypt.configure(state='disabled')
def crypt_msg_func_lc(self):
crypt = Crypt() # Calls the previous class "Crypt" and allows the functions to be used.
crypt.get_key()
message = str(self.ent_msg.get())
crypt.encode_msg = message.encode()
crypt.f = Fernet(crypt.key)
crypt.encrypted = crypt.f.encrypt(crypt.encode_msg)
msg_file = open('crypt.msg', 'wb')
msg_file.write(crypt.encrypted)
msg_file.close()
self.ent_msg.configure(state='disabled')
tkinter.messagebox.showinfo('Done', 'Your file is encrypted and is protected by the sheer mass of the Sumo '
'Man. Please note, if you overwrite your key by generating a new one, '
'your encrypted files will be lost forever.')
def crypt_msg_func_cs(self):
try:
crypt = Crypt() # Calls the previous class "Crypt" and allows the functions to be used.
key = str(self.ent_key.get())
message = str(self.ent_msg.get())
crypt.encode_msg = message.encode()
crypt.f = Fernet(key)
crypt.encrypted = crypt.f.encrypt(crypt.encode_msg)
self.ent_msg.configure(state='disabled')
tkinter.messagebox.showinfo('Done', 'Your file is encrypted and is protected by the sheer mass of the Sumo '
'Man. Please note, if you overwrite your key by generating a new one, '
'your encrypted files will be lost forever.')
except ValueError:
tkinter.messagebox.showwarning('Hey!', 'Please use the right format, encryption key must be 32 url-safe '
'base64-encoded bytes.')
def rtr_menu(self):
if self.var == 0:
self.f_key.pack_forget()
elif self.var == 1:
self.f_crypt.pack_forget()
elif self.var == 2:
self.f_decrypt.pack_forget()
self.f1.pack()
self.f2.pack()
def decrypt_msg_func_lc(self): # Opens the encrypted message and stores it in a variable and decrypts it
try:
file = open('key.key', 'rb')
key = file.read()
file.close()
with open('crypt.msg', 'rb') as f:
data = f.read()
fernet = Fernet(key)
encrypted = fernet.decrypt(data)
final = encrypted.decode()
tkinter.messagebox.showinfo('Done', 'Here is your decrypted message: {}'.format(str(final)))
except InvalidToken:
tkinter.messagebox.showerror('Error', 'Error: File is impossible to decrypt, the key does not match. Was '
'the previous key overwritten?')
def decrypt_msg_func_cs(self): # Opens the encrypted message and stores it in a variable and decrypts it
try:
key = str(self.de_ent_key.get())
message = str(self.ent_crypt.get())
fernet = Fernet(key)
encrypted = fernet.decrypt(message)
final = encrypted.decode()
tkinter.messagebox.showinfo('Done', 'Here is your decrypted message: {}'.format(str(final)))
except InvalidToken:
tkinter.messagebox.showerror('Error', 'Error: File is impossible to decrypt, the key does not match. Was '
'the previous key overwritten?')
except ValueError:
tkinter.messagebox.showwarning('Hey!', 'Please use the right format, encryption key must be 32 url-safe '
'base64-encoded bytes.')
except TypeError:
try:
key = str(self.de_ent_key.get()).encode()
message = str(self.ent_crypt.get()).encode()
fernet = Fernet(key)
encrypted = fernet.decrypt(message)
final = encrypted.decode()
tkinter.messagebox.showinfo('Done', 'Here is your decrypted message: {}'.format(str(final)))
except TypeError:
try:
key = str(self.de_ent_key.get()).encode()
message = str(self.ent_crypt.get())
fernet = Fernet(key)
encrypted = fernet.decrypt(message)
final = encrypted.decode()
tkinter.messagebox.showinfo('Done', 'Here is your decrypted message: {}'.format(str(final)))
except TypeError:
try:
key = str(self.de_ent_key.get())
message = str(self.ent_crypt.get()).encode()
fernet = Fernet(key)
encrypted = fernet.decrypt(message)
final = encrypted.decode()
tkinter.messagebox.showinfo('Done', 'Here is your decrypted message: {}'.format(str(final)))
except TypeError:
tkinter.messagebox.showwarning('Hey!', 'Please use the right format, encryption key and '
'message must be in bytes format. Make sure you keep '
'the " b " before the key and message. In all '
"actuality, you shouldn't be seeing this message. If"
'you are, then you screwed up something terribly. '
'Congrats.')
def key_chk_btn_func(self):
if self.tizzy == 0:
if self.chk_cs.instate(['selected']):
self.crypt_btn_cs.configure(state='normal')
self.ent_key.configure(state='normal')
else:
self.crypt_btn_cs.configure(state='disabled')
self.ent_key.configure(state='disabled')
if self.tizzy == 1:
if self.chk_cs.instate(['selected']):
self.decrypt_btn_cs.configure(state='normal')
self.de_ent_key.configure(state='normal')
self.ent_crypt.configure(state='normal')
else:
self.decrypt_btn_cs.configure(state='disabled')
self.de_ent_key.configure(state='disabled')
self.ent_crypt.configure(state='disabled')
def runGame(self):
self.master.destroy()
sumoGame.main()
def main(): # Packs the GUI class into a main function and sets program basics
program = Tk()
program.title('SUMOCORE © 2020 SUMO')
program.geometry('1350x850')
program.iconbitmap('venv/UI/tizzycrypt.ico')
app = Tkinter_GUI(program)
program.mainloop()
if __name__ == '__main__': # Makes sure the module is being ran directly instead of being imported
main()