-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolder_crypto.py
More file actions
37 lines (26 loc) · 992 Bytes
/
folder_crypto.py
File metadata and controls
37 lines (26 loc) · 992 Bytes
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
import os
def encrypt_folder(folder_path, fernet):
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith(".enc"):
continue
path = os.path.join(root, file)
with open(path, "rb") as f:
data = f.read()
encrypted = fernet.encrypt(data)
with open(path + ".enc", "wb") as f:
f.write(encrypted)
os.remove(path)
def decrypt_folder(folder_path, fernet):
for root, _, files in os.walk(folder_path):
for file in files:
if not file.endswith(".enc"):
continue
path = os.path.join(root, file)
with open(path, "rb") as f:
encrypted = f.read()
decrypted = fernet.decrypt(encrypted)
original_path = path[:-4] # remove .enc
with open(original_path, "wb") as f:
f.write(decrypted)
os.remove(path)