-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainapp.py
More file actions
232 lines (186 loc) · 7.26 KB
/
mainapp.py
File metadata and controls
232 lines (186 loc) · 7.26 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
import os
import threading
import customtkinter as ctk
from tkinter import filedialog, messagebox
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.backends import default_backend
# ================= CONFIG =================
ITERATIONS = 200_000
KEY_LENGTH = 32
BACKEND = default_backend()
# ================= CRYPTO =================
def derive_key(password: str, salt: bytes) -> bytes:
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=KEY_LENGTH,
salt=salt,
iterations=ITERATIONS,
backend=BACKEND
)
return kdf.derive(password.encode())
def encrypt_file(file_path, password):
with open(file_path, "rb") as f:
data = f.read()
salt = os.urandom(16)
nonce = os.urandom(12)
key = derive_key(password, salt)
aes = AESGCM(key)
encrypted = aes.encrypt(nonce, data, None)
out = file_path + ".enc"
with open(out, "wb") as f:
f.write(salt + nonce + encrypted)
return out
def decrypt_file(file_path, password):
with open(file_path, "rb") as f:
raw = f.read()
salt, nonce, cipher = raw[:16], raw[16:28], raw[28:]
key = derive_key(password, salt)
aes = AESGCM(key)
data = aes.decrypt(nonce, cipher, None)
out = file_path.replace(".enc", "")
with open(out, "wb") as f:
f.write(data)
return out
# ================= PASSWORD METER (REAL FIX) =================
def password_meter(password: str):
length_score = min(len(password) / 16, 1.0)
variety = 0
if any(c.islower() for c in password): variety += 1
if any(c.isupper() for c in password): variety += 1
if any(c.isdigit() for c in password): variety += 1
if any(c in "!@#$%^&*()_+-=" for c in password): variety += 1
variety_score = variety / 4
value = min((length_score * 0.6) + (variety_score * 0.4), 1.0)
if value < 0.3:
return "Weak", "red", value
elif value < 0.55:
return "Medium", "yellow", value
elif value < 0.8:
return "Strong", "orange", value
else:
return "Very Strong", "green", value
# ================= GUI =================
ctk.set_appearance_mode("Dark")
ctk.set_default_color_theme("blue")
class SecureApp(ctk.CTk):
def __init__(self):
super().__init__()
self.title("Secure File Encryption Tool")
self.geometry("1000x600")
self.sidebar = ctk.CTkFrame(self, width=200)
self.sidebar.pack(side="left", fill="y")
self.container = ctk.CTkFrame(self)
self.container.pack(side="right", expand=True, fill="both")
self.create_sidebar()
self.show_encrypt()
def create_sidebar(self):
for text, cmd in [
("Encrypt File", self.show_encrypt),
("Decrypt File", self.show_decrypt),
("Settings", self.show_settings),
("About", self.show_about),
]:
ctk.CTkButton(self.sidebar, text=text, command=cmd).pack(
pady=8, padx=10, fill="x"
)
def clear(self):
for w in self.container.winfo_children():
w.destroy()
# ================= PASSWORD UI (FIXED) =================
def password_ui(self, parent):
pwd = ctk.CTkEntry(parent, show="*", width=300)
pwd.pack(pady=6)
show = ctk.BooleanVar()
ctk.CTkCheckBox(
parent,
text="Show Password 👁️",
variable=show,
command=lambda: pwd.configure(show="" if show.get() else "*")
).pack()
label = ctk.CTkLabel(parent, text="Password Strength:")
label.pack(pady=(8, 2))
bar = ctk.CTkProgressBar(parent, width=320)
bar.pack(pady=4)
bar.set(0)
def update(event=None):
strength, color, value = password_meter(pwd.get())
label.configure(text=f"Password Strength: {strength}", text_color=color)
bar.set(value)
pwd.bind("<KeyRelease>", update)
return pwd
# ================= ENCRYPT =================
def show_encrypt(self):
self.clear()
f = ctk.CTkFrame(self.container)
f.pack(expand=True, fill="both", padx=20, pady=20)
ctk.CTkLabel(f, text="Encrypt File", font=("Arial", 24)).pack(pady=10)
file_entry = ctk.CTkEntry(f, width=420)
file_entry.pack(pady=5)
ctk.CTkButton(
f,
text="Browse File",
command=lambda: file_entry.insert(0, filedialog.askopenfilename())
).pack()
password_entry = self.password_ui(f)
def start():
strength, _, _ = password_meter(password_entry.get())
if strength in ["Weak", "Medium"]:
messagebox.showwarning("Weak Password", "Use Strong or Very Strong password")
return
def task():
try:
out = encrypt_file(file_entry.get(), password_entry.get())
messagebox.showinfo("Success", f"Encrypted:\n{out}")
except Exception as e:
messagebox.showerror("Error", str(e))
threading.Thread(target=task).start()
ctk.CTkButton(f, text="Encrypt", command=start).pack(pady=15)
# ================= DECRYPT =================
def show_decrypt(self):
self.clear()
f = ctk.CTkFrame(self.container)
f.pack(expand=True, fill="both", padx=20, pady=20)
ctk.CTkLabel(f, text="Decrypt File", font=("Arial", 24)).pack(pady=10)
file_entry = ctk.CTkEntry(f, width=420)
file_entry.pack(pady=5)
ctk.CTkButton(
f,
text="Browse Encrypted File",
command=lambda: file_entry.insert(
0, filedialog.askopenfilename(filetypes=[("ENC", "*.enc")])
)
).pack()
password_entry = self.password_ui(f)
def start():
try:
out = decrypt_file(file_entry.get(), password_entry.get())
messagebox.showinfo("Success", f"Decrypted:\n{out}")
except Exception as e:
messagebox.showerror("Error", str(e))
ctk.CTkButton(f, text="Decrypt", command=start).pack(pady=15)
def show_settings(self):
self.clear()
f = ctk.CTkFrame(self.container)
f.pack(expand=True, fill="both")
ctk.CTkButton(
f,
text="Toggle Dark / Light Mode",
command=lambda: ctk.set_appearance_mode(
"Light" if ctk.get_appearance_mode() == "Dark" else "Dark"
)
).pack(pady=40)
def show_about(self):
self.clear()
f = ctk.CTkFrame(self.container)
f.pack(expand=True, fill="both")
ctk.CTkLabel(
f,
text="Secure File Encryption Tool\nAES-256-GCM\nPBKDF2\nProfessional Password Meter",
font=("Arial", 16),
justify="center"
).pack(expand=True)
# ================= RUN =================
if __name__ == "__main__":
SecureApp().mainloop()