-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (60 loc) · 2.19 KB
/
Copy pathmain.py
File metadata and controls
74 lines (60 loc) · 2.19 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
import tkinter as tk
from tkinter import filedialog, messagebox
from cryptography.fernet import Fernet
import os
# ===== Key Management =====
KEY_FILE = "secret.key"
def generate_key():
key = Fernet.generate_key()
with open(KEY_FILE, "wb") as key_file:
key_file.write(key)
def load_key():
if not os.path.exists(KEY_FILE):
generate_key()
with open(KEY_FILE, "rb") as key_file:
return key_file.read()
# ===== File Encryption =====
def encrypt_file():
file_path = filedialog.askopenfilename(title="Select File to Encrypt")
if not file_path:
return
try:
key = load_key()
fernet = Fernet(key)
with open(file_path, "rb") as f:
data = f.read()
encrypted = fernet.encrypt(data)
encrypted_path = file_path + ".enc"
with open(encrypted_path, "wb") as f:
f.write(encrypted)
messagebox.showinfo("Success", f"File encrypted:\n{encrypted_path}")
except Exception as e:
messagebox.showerror("Error", f"Encryption failed:\n{e}")
# ===== File Decryption =====
def decrypt_file():
file_path = filedialog.askopenfilename(title="Select File to Decrypt", filetypes=[("Encrypted files", "*.enc")])
if not file_path:
return
try:
key = load_key()
fernet = Fernet(key)
with open(file_path, "rb") as f:
encrypted = f.read()
decrypted = fernet.decrypt(encrypted)
save_path = filedialog.asksaveasfilename(title="Save Decrypted File As")
if not save_path:
return
with open(save_path, "wb") as f:
f.write(decrypted)
messagebox.showinfo("Success", f"File decrypted:\n{save_path}")
except Exception as e:
messagebox.showerror("Error", f"Decryption failed:\n{e}")
# ===== GUI Setup =====
root = tk.Tk()
root.title("File Encryptor & Decryptor")
root.geometry("400x200")
root.resizable(False, False)
tk.Label(root, text="Secure File Encryption & Decryption", font=("Arial", 14)).pack(pady=20)
tk.Button(root, text="Encrypt File", width=20, command=encrypt_file).pack(pady=10)
tk.Button(root, text="Decrypt File", width=20, command=decrypt_file).pack(pady=10)
root.mainloop()