-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
338 lines (291 loc) · 11 KB
/
main.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
#!/usr/bin/env python
"""Simple GUI for Easy Crypt."""
from __future__ import (
print_function,
division,
unicode_literals
)
import os
import sys
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import tkFileDialog as filedialog
except ImportError:
from tkinter import filedialog
try:
import tkMessageBox as messagebox
except ImportError:
from tkinter import messagebox
# from tkinter import ttk
from eccrypt import __version__
from eccrypt.encrypt import encrypt
from eccrypt.decrypt import decrypt
from eccrypt.generate import generate
# Solarized colorscheme
base03 = "#002b36"
base02 = "#073642"
base01 = "#586e75"
base00 = "#657b83"
base0 = "#839496"
base1 = "#93a1a1"
base2 = "#eee8d5"
base3 = "#fdf6e3"
yellow = "#b58900"
orange = "#cb4b16"
red = "#dc322f"
magenta = "#d33682"
violet = "#6c71c4"
blue = "#268bd2"
cyan = "#2aa198"
green = "#859900"
class EasyButtons:
"""Easy Buttons."""
def __init__(self, master):
"""."""
frame = tk.Frame(master)
frame.pack()
# frame.configure(background=base03)
self.text_file_paths = None
self.encrypted_file_paths = None
self.public_key_file_path = None
self.private_key_file_path = None
# Radiobuttons
self.v = tk.StringVar()
self.v.set("PUB")
self.radiobutton_public_key = tk.Radiobutton(
frame,
width=20,
text="Encrypt",
variable=self.v,
value="PUB",
# bg=base03,
command=lambda: self.enable_button_public_key(None))
self.radiobutton_public_key.grid(column=1, row=1, padx=20, pady=20)
self.radiobutton_private_key = tk.Radiobutton(
frame,
width=20,
text="Decrypt",
variable=self.v,
value="PRI",
# bg=base03,
command=lambda: self.enable_button_private_key(None))
self.radiobutton_private_key.grid(column=2, row=1, padx=20, pady=20)
# Buttons
self.button_open_text_file = tk.Button(
frame,
width=20,
text="Open a text file",
relief=tk.GROOVE,
# bg=base01,
command=self.open_text_file)
# self.button_open_text_file.pack(side=tk.LEFT)
self.button_open_text_file.grid(column=1, row=2, padx=5, pady=5)
self.button_open_encrypted_file = tk.Button(
frame,
width=20,
state=tk.DISABLED, # Use "disabled" in `ttk`
text="Open an encrypted file",
relief=tk.GROOVE,
# bg=base01,
command=self.open_encrypted_file)
self.button_open_encrypted_file.grid(column=2, row=2, padx=5, pady=5)
# TODO: Move this button to the `File` menu as `Import public key`.
self.button_public_key = tk.Button(
frame,
width=20,
text="Open your public key",
relief=tk.GROOVE,
# bg=base01,
command=self.open_public_key_file)
self.button_public_key.grid(column=1, row=3, padx=5, pady=5)
# TODO: Move this button to the `File` menu as `Import private key`.
self.button_private_key = tk.Button(
frame,
width=20,
state=tk.DISABLED, # Use "disabled" in `ttk`
text="Open your private key",
relief=tk.GROOVE,
# bg=base01,
command=self.open_private_key_file)
self.button_private_key.grid(column=2, row=3, padx=5, pady=5)
self.button_save_encrypted_file = tk.Button(
frame,
width=20,
text="Save the encrypted file",
relief=tk.GROOVE,
# bg=base01,
command=self.save_encrypted_file)
self.button_save_encrypted_file.grid(column=1, row=4, padx=20, pady=20)
self.button_save_decrypted_file = tk.Button(
frame,
width=20,
text="Save the decrypted file",
relief=tk.GROOVE,
# bg=base01,
state=tk.DISABLED, # Use "disabled" in `ttk`
command=self.save_decrypted_file)
self.button_save_decrypted_file.grid(column=2, row=4, padx=20, pady=20)
self.status = tk.Label(
root,
text="Start by opening a text file.",
bd=1,
relief=tk.SUNKEN,
anchor=tk.W)
self.status.pack(side=tk.BOTTOM, fill=tk.X)
def enable_button_public_key(self, event):
"""Enable encryption buttons."""
# NOTE: Use `state` attribute with `ttk` buttons
# self.button_public_key.state(["!disabled"])
# self.button_private_key.state(["disabled"])
# self.button_open_text_file.state(["!disabled"])
# self.button_open_encrypted_file.state(["disabled"])
# self.button_save_encrypted_file.state(["!disabled"])
# self.button_save_decrypted_file.state(["disabled"])
# NOTE: Use `state` key with `tk` buttons
self.button_public_key['state'] = tk.NORMAL
self.button_private_key['state'] = tk.DISABLED
self.button_open_text_file['state'] = tk.NORMAL
self.button_open_encrypted_file['state'] = tk.DISABLED
self.button_save_encrypted_file['state'] = tk.NORMAL
self.button_save_decrypted_file['state'] = tk.DISABLED
self.status['text'] = "Start by opening a text file."
def enable_button_private_key(self, event):
"""Enable decryption buttons."""
# NOTE: Use `state` attribute with `ttk` buttons
# self.button_public_key.state(["disabled"])
# self.button_private_key.state(["!disabled"])
# self.button_open_text_file.state(["disabled"])
# self.button_open_encrypted_file.state(["!disabled"])
# self.button_save_encrypted_file.state(["disabled"])
# self.button_save_decrypted_file.state(["!disabled"])
# NOTE: Use `state` key with `tk` buttons
self.button_public_key['state'] = tk.DISABLED
self.button_private_key['state'] = tk.NORMAL
self.button_open_text_file['state'] = tk.DISABLED
self.button_open_encrypted_file['state'] = tk.NORMAL
self.button_save_encrypted_file['state'] = tk.DISABLED
self.button_save_decrypted_file['state'] = tk.NORMAL
self.status['text'] = "Start by opening an encrypted file."
def open_text_file(self):
"""Open text file."""
self.text_file_path = filedialog.askopenfilename()
print(self.text_file_path)
if self.text_file_path:
self.status['text'] = "Now you can open the public key."
return self.text_file_path
def open_encrypted_file(self):
"""Open encrypted file."""
self.encrypted_file_path = filedialog.askopenfilename()
print(self.encrypted_file_path)
if self.encrypted_file_path:
self.status['text'] = "Now you can open the private key."
return self.encrypted_file_path
def open_public_key_file(self):
"""Open public key file."""
self.public_key_file_path = filedialog.askopenfilename()
print(self.public_key_file_path)
if self.public_key_file_path:
self.status['text'] = "Now you can save the encrypted file."
return self.public_key_file_path
def open_private_key_file(self):
"""Open private key file."""
self.private_key_file_path = filedialog.askopenfilename()
print(self.private_key_file_path)
if self.private_key_file_path:
self.status['text'] = "Now you can save the decrypted file."
return self.private_key_file_path
def save_encrypted_file(self):
"""Save encrypted file."""
filename = filedialog.asksaveasfilename(
defaultextension=".bin",
filetypes=(('Binary files', '.bin'), ('All files', '.*')))
if not filename:
return
try:
encrypt(
self.text_file_path,
rsa_public_key=self.public_key_file_path,
output=[filename]) # Convert `output` argument, must be a list
self.status['text'] = "File encrypted and saved."
except NameError as e:
messagebox.showerror(e.__class__.__name__, e)
except TypeError as e:
msg = "Please, select a text file and a valid public key."
print(msg, e)
messagebox.showerror(e.__class__.__name__, msg)
except IndexError as e:
messagebox.showerror(e.__class__.__name__, e)
def save_decrypted_file(self):
"""Save decrypted file."""
filename = filedialog.asksaveasfilename(
defaultextension=".txt",
filetypes=(('Text files', '.txt'), ('All files', '.*')))
if not filename:
return
try:
decrypt(
self.encrypted_file_path,
rsa_private_key=self.private_key_file_path,
output=[filename]) # Convert `output` argument, must be a list
self.status['text'] = "File decrypted and saved."
except TypeError as e:
msg = "Please, select an encrypted file and a valid private key."
print(msg, e)
messagebox.showerror(e.__class__.__name__, msg)
except NameError as e:
messagebox.showerror(e.__class__.__name__, e)
except ValueError as e:
messagebox.showerror(e.__class__.__name__, e)
root = tk.Tk()
b = EasyButtons(root)
# Menu bar
def save_configuration():
"""Save configuration from menu."""
messagebox.showwarning(
"Save", "This function is not yet available.")
def generate_key_pair():
"""Generate RSA key pair."""
filename = filedialog.asksaveasfilename()
if not filename:
return
try:
generate(filename)
b.status['text'] = "RSA key pair generated."
except:
messagebox.showerror(sys.exc_info()[0], sys.exc_info()[1])
def show_about():
"""Show about message box."""
messagebox.showinfo(
"About", "Easy Crypt v" + __version__ + "\nArch. Pierpaolo Rasicci\n" +
"https://github.com/i5ar/eccrypt")
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Generate key pair", command=generate_key_pair)
filemenu.add_command(label="Save", command=save_configuration)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=show_about)
menubar.add_cascade(label="Help", menu=helpmenu)
root.config(menu=menubar)
# Title
root.title("Easy Crypt")
# Icon
if "nt" == os.name:
icon_path = os.path.join("images", "eccrypt.ico")
else:
icon_path = os.path.join("images", "eccrypt.gif")
img = tk.PhotoImage(file=icon_path)
root.tk.call('wm', 'iconphoto', root._w, img)
# Eventually
# if "nt" == os.name:
# icon_path = os.path.join("images", "eccrypt.ico")
# else:
# icon_path = "@" + os.path.join("images", "eccrypt.xbm")
# root.wm_iconbitmap(bitmap=icon_path)
# Main loop
root.mainloop()