-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
104 lines (73 loc) · 3.5 KB
/
Copy pathgui.py
File metadata and controls
104 lines (73 loc) · 3.5 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
import tkinter as tk
from tkinter import filedialog, messagebox
from crypto import encrypt_file, decrypt_file, get_file_hash, write_log
class App:
def __init__(self,root):
root.title("Secure File Encryption")
root.geometry("420x320")
root.resizable(False, False)
self.selected_file = tk.StringVar()
self.password = tk.StringVar()
#File selection row
tk.Label(root, text="Selected file:", anchor="w").pack(
fill="x", padx=20, pady=(20,0))
file_frame = tk.Frame(root)
file_frame.pack(fill="x", padx=20, pady=4)
tk.Entry(file_frame, textvariable=self.selected_file,
state="readonly").pack(side="left", fill="x", expand=True)
tk.Button(file_frame, text="Browse",
command=self.browse_file).pack(side="right", padx=(6, 0))
#Password row
tk.Label(root, text="Password:", anchor="w").pack(
fill="x", padx=20, pady=(12, 0))
tk.Entry(root, textvariable=self.password,
show="*").pack(fill="x", padx=20, pady=4)
#Buttons
btn_frame = tk.Frame(root)
btn_frame.pack(pady=20)
tk.Button(btn_frame, text="Encrypt", width=14, bg="#185FA5", fg="white", command=self.encrypt).pack(side="left", padx=8)
tk.Button(btn_frame, text="Decrypt", width=14, bg="#0F6E56", fg="white", command=self.decrypt).pack(side="left", padx=8)
#Status label at the bottom
self.status = tk.StringVar()
tk.Label(root, textvariable=self.status, fg="gray", wraplength=380).pack(pady=(0,16))
def browse_file(self):
path = filedialog.askopenfilename(
title="Select a file",
filetypes=[("All files", "*.*")]
)
if path:
self.selected_file.set(path)
self.status.set("")
def encrypt(self):
src = self.selected_file.get()
pwd = self.password.get()
if not src or not pwd:
messagebox.showwarning("Missing input", "Please select a file and enter a password.")
return
output = src + ".enc"
try:
original_hash = get_file_hash(src)
duration = encrypt_file(src, output, pwd)
write_log("ENCRYPTED", src, duration)
self.status.set(f"Encrypted in {duration}s | SHA-256: {original_hash[:20]}...")
messagebox.showinfo("Success", f"File encrypted in {duration}s\nSHA-256: {original_hash[:20]}...")
except Exception as e:
messagebox.showerror("Error", str(e))
def decrypt(self):
src = self.selected_file.get()
pwd = self.password.get()
if not src or not pwd:
messagebox.showwarning("Missing input", "Please select a file and enter a password.")
return
if not src.endswith(".enc"):
messagebox.showwarning("Wrong file", "Please select a .enc file to decrypt.")
return
output = src.replace(".enc", ".decrypted")
try:
duration = decrypt_file(src, output, pwd)
restored_hash = get_file_hash(output)
write_log("DECRYPTED", src, duration)
self.status.set(f"Decrypted in {duration}s |SHA-256: {restored_hash[:20]}...")
messagebox.showinfo("Success", f"Decrypted in {duration}s\nIntegrity hash: {restored_hash[:20]}...")
except ValueError as e:
messagebox.showerror("Failed", str(e))