-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_crypt.py
More file actions
285 lines (221 loc) · 11.7 KB
/
file_crypt.py
File metadata and controls
285 lines (221 loc) · 11.7 KB
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
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from cryptography.fernet import Fernet
import base64
import os
class CryptApp:
def __init__(self, root):
self.root = root
self.root.title("Wil-go File Crypt")
self.root.geometry("600x450")
# Configure styles
self.style = ttk.Style()
self.style.theme_use('clam')
# Configure colors
self.style.configure('TFrame', background='#f0f0f0')
self.style.configure('TLabel', background='#f0f0f0', font=('Segoe UI', 10))
self.style.configure('TButton', font=('Segoe UI', 10), padding=5)
self.style.configure('TEntry', font=('Segoe UI', 10), padding=5)
self.style.configure('TNotebook', background='#f0f0f0')
self.style.configure('TNotebook.Tab', font=('Segoe UI', 10, 'bold'), padding=[10,5])
# Custom button styles
self.style.map('Primary.TButton',
foreground=[('active', 'white'), ('!disabled', 'white')],
background=[('active', '#0069d9'), ('!disabled', '#007bff')])
self.style.configure('Primary.TButton', font=('Segoe UI', 10, 'bold'))
self.setup_ui()
def setup_ui(self):
# Main container
self.notebook = ttk.Notebook(self.root)
# Encryption Tab
self.encrypt_frame = ttk.Frame(self.notebook)
self.create_encrypt_ui()
# Decryption Tab
self.decrypt_frame = ttk.Frame(self.notebook)
self.create_decrypt_ui()
self.notebook.add(self.encrypt_frame, text="Encrypt File")
self.notebook.add(self.decrypt_frame, text="Decrypt File")
self.notebook.pack(expand=True, fill='both')
def create_encrypt_ui(self):
# Encryption UI components
ttk.Label(self.encrypt_frame, text="Select File to Encrypt:").pack(pady=(10,5))
file_frame = ttk.Frame(self.encrypt_frame)
file_frame.pack(fill='x', padx=20, pady=5)
self.encrypt_file_btn = ttk.Button(file_frame, text="Browse", style='Primary.TButton', command=self.select_encrypt_file)
self.encrypt_file_btn.pack(side='left', padx=(0,10))
self.encrypt_file_info = ttk.Label(file_frame, text="No file selected", style='TLabel')
self.encrypt_file_info.pack(side='left')
# File handling options
options_frame = ttk.Frame(self.encrypt_frame)
options_frame.pack(fill='x', padx=20, pady=(5,0))
ttk.Label(options_frame, text="After encryption:").pack(side='left')
self.file_handling_var = tk.StringVar(value='remove')
ttk.Radiobutton(options_frame, text="Remove original file",
variable=self.file_handling_var, value='remove').pack(side='left', padx=10)
ttk.Radiobutton(options_frame, text="Keep original file",
variable=self.file_handling_var, value='keep').pack(side='left')
ttk.Label(self.encrypt_frame, text="Encryption Key:").pack(pady=(10,5))
key_frame = ttk.Frame(self.encrypt_frame)
key_frame.pack(fill='x', padx=20, pady=5)
self.key_entry = ttk.Entry(key_frame, width=50)
self.key_entry.pack(side='left', fill='x', expand=True, padx=(0,10))
self.gen_key_btn = ttk.Button(key_frame, text="Generate", style='Primary.TButton', command=self.generate_key)
self.gen_key_btn.pack(side='left')
action_frame = ttk.Frame(self.encrypt_frame)
action_frame.pack(pady=(20,10))
self.encrypt_btn = ttk.Button(action_frame, text="Encrypt File", style='Primary.TButton', command=self.encrypt_file)
self.encrypt_btn.pack(pady=10, ipadx=20, ipady=5)
def create_decrypt_ui(self):
# Decryption UI components
ttk.Label(self.decrypt_frame, text="Select File to Decrypt:").pack(pady=(10,5))
file_frame = ttk.Frame(self.decrypt_frame)
file_frame.pack(fill='x', padx=20, pady=5)
self.decrypt_file_btn = ttk.Button(file_frame, text="Browse", style='Primary.TButton', command=self.select_decrypt_file)
self.decrypt_file_btn.pack(side='left', padx=(0,10))
self.decrypt_file_info = ttk.Label(file_frame, text="No file selected", style='TLabel')
self.decrypt_file_info.pack(side='left')
# File handling options
options_frame = ttk.Frame(self.decrypt_frame)
options_frame.pack(fill='x', padx=20, pady=(5,0))
ttk.Label(options_frame, text="After decryption:").pack(side='left')
self.decrypt_handling_var = tk.StringVar(value='remove')
ttk.Radiobutton(options_frame, text="Remove encrypted file",
variable=self.decrypt_handling_var, value='remove').pack(side='left', padx=10)
ttk.Radiobutton(options_frame, text="Keep encrypted file",
variable=self.decrypt_handling_var, value='keep').pack(side='left')
ttk.Label(self.decrypt_frame, text="Decryption Key:").pack(pady=(10,5))
key_frame = ttk.Frame(self.decrypt_frame)
key_frame.pack(fill='x', padx=20, pady=5)
self.decrypt_key_entry = ttk.Entry(key_frame, width=50)
self.decrypt_key_entry.pack(fill='x', expand=True)
action_frame = ttk.Frame(self.decrypt_frame)
action_frame.pack(pady=(20,10))
self.decrypt_btn = ttk.Button(action_frame, text="Decrypt File", style='Primary.TButton', command=self.decrypt_file)
self.decrypt_btn.pack(pady=10, ipadx=20, ipady=5)
def generate_key(self):
key = Fernet.generate_key()
self.key_entry.delete(0, tk.END)
self.key_entry.insert(0, key.decode())
def select_encrypt_file(self):
file_path = filedialog.askopenfilename(title="Select File to Encrypt")
if file_path:
self.encrypt_path = file_path
self.encrypt_file_info.config(text=os.path.basename(file_path))
def select_decrypt_file(self):
file_path = filedialog.askopenfilename(title="Select File to Decrypt")
if file_path:
self.decrypt_path = file_path
self.decrypt_file_info.config(text=os.path.basename(file_path))
def format_file_size(self, size):
# Convert bytes to human-readable format
for unit in ['B', 'KB', 'MB', 'GB']:
if size < 1024:
return f"{size:.2f} {unit}"
size /= 1024
return f"{size:.2f} GB"
def encrypt_file(self):
try:
if not hasattr(self, 'encrypt_path') or not self.encrypt_path:
messagebox.showerror('Error', 'Please select a file to encrypt first')
return
original_path = self.encrypt_path
key = self.key_entry.get().strip()
if not key:
messagebox.showerror('Error', 'Please enter an encryption key or generate one')
return
try:
key = key.encode()
fernet = Fernet(key)
except Exception:
messagebox.showerror('Error', 'Invalid encryption key format. Please use a valid Fernet key')
return
try:
with open(self.encrypt_path, 'rb') as f:
file_data = f.read()
except Exception:
messagebox.showerror('Error', 'Could not read the selected file. Please check file permissions')
return
encrypted = fernet.encrypt(file_data)
save_path = filedialog.asksaveasfilename(
title="Save Encrypted File",
defaultextension=".enc",
filetypes=[("Encrypted Files", "*.enc")]
)
if not save_path:
return
try:
with open(save_path, 'wb') as f:
f.write(encrypted)
# Handle original file based on user selection
if self.file_handling_var.get() == 'remove':
try:
os.remove(original_path)
messagebox.showinfo('Success',
'File encrypted successfully! Original file has been securely removed.')
except Exception as e:
messagebox.showwarning('Warning',
f'File encrypted but could not remove original: {str(e)}')
else:
messagebox.showinfo('Success',
'File encrypted successfully! Original file has been preserved.')
except Exception:
messagebox.showerror('Error', 'Failed to save encrypted file. Check if you have write permissions')
except Exception as e:
messagebox.showerror('Error', f'Encryption failed: {str(e)}')
def decrypt_file(self):
try:
if not hasattr(self, 'decrypt_path') or not self.decrypt_path:
messagebox.showerror('Error', 'Please select an encrypted file first')
return
original_path = self.decrypt_path
key = self.decrypt_key_entry.get().strip()
if not key:
messagebox.showerror('Error', 'Please enter the decryption key')
return
try:
key = key.encode()
fernet = Fernet(key)
except Exception:
messagebox.showerror('Error', 'Invalid decryption key format. Please use a valid Fernet key')
return
try:
with open(self.decrypt_path, 'rb') as f:
encrypted_data = f.read()
except Exception:
messagebox.showerror('Error', 'Could not read the encrypted file. It may be corrupted')
return
try:
decrypted = fernet.decrypt(encrypted_data)
except Exception:
messagebox.showerror('Error', 'Decryption failed. The key may be incorrect or the file is corrupted')
return
save_path = filedialog.asksaveasfilename(
title="Save Decrypted File",
defaultextension="",
filetypes=[("All Files", "*.*")]
)
if not save_path:
return
try:
with open(save_path, 'wb') as f:
f.write(decrypted)
# Handle encrypted file based on user selection
if self.decrypt_handling_var.get() == 'remove':
try:
os.remove(original_path)
messagebox.showinfo('Success',
'File decrypted successfully! Encrypted file has been securely removed.')
except Exception as e:
messagebox.showwarning('Warning',
f'File decrypted but could not remove encrypted file: {str(e)}')
else:
messagebox.showinfo('Success',
'File decrypted successfully! Encrypted file has been preserved.')
except Exception:
messagebox.showerror('Error', 'Failed to save decrypted file. Check if you have write permissions')
except Exception as e:
messagebox.showerror('Error', f'An unexpected error occurred: {str(e)}')
if __name__ == "__main__":
root = tk.Tk()
app = CryptApp(root)
root.mainloop()