-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecryptor.py
More file actions
35 lines (25 loc) · 983 Bytes
/
decryptor.py
File metadata and controls
35 lines (25 loc) · 983 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
import os
from crypto_utils import derive_key, MAGIC, SALT_SIZE, IV_SIZE
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def decrypt_file(path: str, password: str):
with open(path, "rb") as f:
content = f.read()
if not content.startswith(MAGIC):
return
salt_start = len(MAGIC)
salt = content[salt_start : salt_start + SALT_SIZE]
iv_start = salt_start + SALT_SIZE
iv = content[iv_start : iv_start + IV_SIZE]
ciphertext = content[iv_start + IV_SIZE :]
key = derive_key(password, salt)
aes = AESGCM(key)
decrypted = aes.decrypt(iv, ciphertext, None)
original_path = path.replace(".bapu", "")
with open(original_path, "wb") as f:
f.write(decrypted)
os.remove(path)
def decrypt_folder(folder: str, password: str):
for root, _, files in os.walk(folder):
for file in files:
if file.endswith(".bapu"):
decrypt_file(os.path.join(root, file), password)