-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (71 loc) · 2.74 KB
/
Copy pathmain.py
File metadata and controls
103 lines (71 loc) · 2.74 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
import os
from cryptography.fernet import Fernet
def create_key():
key = Fernet.generate_key()
with open("top_secret.key", "wb") as key_file:
key_file.write(key)
print("\n[+] Success: 'top_secret.key' has been created. Do not lose it!")
def encrypt_files(folder_path):
if not os.path.exists("top_secret.key"):
print("\n[!] Error: 'top_secret.key' not found! Create a key first.")
return
with open("top_secret.key", "rb") as key_file:
key = key_file.read()
f = Fernet(key)
for file_name in os.listdir(folder_path):
file_full_path = os.path.join(folder_path, file_name)
if os.path.isfile(file_full_path) and file_name != "top_secret.key":
with open(file_full_path, "rb") as file:
raw_data = file.read()
encrypted_data = f.encrypt(raw_data)
with open(file_full_path, "wb") as file:
file.write(encrypted_data)
print(f"[*] Encrypted: {file_name}")
def decrypt_files(folder_path):
try:
with open("top_secret.key", "rb") as key_file:
key = key_file.read()
except FileNotFoundError:
print("\n[!] Error: 'top_secret.key' file not found.")
return
f = Fernet(key)
for file_name in os.listdir(folder_path):
file_full_path = os.path.join(folder_path, file_name)
if os.path.isfile(file_full_path) and file_name != "top_secret.key":
with open(file_full_path, "rb") as file:
locked_data = file.read()
try:
decrypted_data = f.decrypt(locked_data)
with open(file_full_path, "wb") as file:
file.write(decrypted_data)
print(f"[*] Decrypted: {file_name}")
except Exception as e:
print(f"[!] Error decrypting {file_name}: {e}")
while True:
print("\n" + "=" * 30)
print(" FILE ENCRYPTION CENTER")
print("=" * 30)
print("1 - Create New Key")
print("2 - Encrypt Folder")
print("3 - Decrypt Folder")
print("Q - Quit Program")
choice = input("\nPlease enter your choice: ").strip().lower()
if choice == "1":
create_key()
elif choice == "2":
target = input("Enter the folder name to encrypt: ")
if os.path.exists(target):
encrypt_files(target)
else:
print("[!] Folder not found.")
elif choice == "3":
target = input("Enter the folder name to decrypt: ")
if os.path.exists(target):
decrypt_files(target)
else:
print("[!] Folder not found.")
elif choice == "q":
print("Closing program... Stay safe!")
break
else:
print("Invalid choice! Please try again.")