-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.py
More file actions
79 lines (59 loc) · 1.99 KB
/
logic.py
File metadata and controls
79 lines (59 loc) · 1.99 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
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.exceptions import InvalidTag
from cryptography.hazmat.backends import default_backend
from pathlib import Path
import secrets
#encryption
def encrypt_file(file_path, password):
with open(file_path, 'rb') as file:
data = file.read()
#generating salt
salt = secrets.token_bytes(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=200000,
backend=default_backend()
)
key = kdf.derive(password.encode())
#encrypt with AES-GCM
aesgcm = AESGCM(key)
nonce = secrets.token_bytes(12)
ciphertext = aesgcm.encrypt(nonce, data, None)
file_path = Path(file_path)
extension = file_path.suffix.encode()
output_path = file_path.with_suffix(".efy")
with open(output_path, 'wb') as file:
file.write(salt + nonce + bytes([len(extension)]) + extension + ciphertext)
return True
def decrypt_file(file_path, password):
path = Path(file_path)
with open(path, 'rb') as file:
blob = file.read()
salt = blob[:16] # 16 bytes
nonce = blob[16:28] # next 12 bytes
ext_len = blob[28] # 1 byte -> length of extension
extension = blob[29:29+ext_len].decode() # extension string
ciphertext = blob[29+ext_len:]
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=200_000,
backend=default_backend()
)
key = kdf.derive(password.encode())
aesgcm = AESGCM(key)
try:
decrypted_data = aesgcm.decrypt(nonce, ciphertext, None)
except InvalidTag:
# Raised if password is wrong or ciphertext is tampered
return False
out_path = path.with_suffix(extension)
with open(out_path, 'wb') as file:
file.write(decrypted_data)
return True