-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt_file.py
More file actions
41 lines (30 loc) · 950 Bytes
/
decrypt_file.py
File metadata and controls
41 lines (30 loc) · 950 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
38
39
40
41
#%%
import os
from base64 import b64decode
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.serialization import load_der_private_key
abs_path = os.path.dirname(__file__)
""" Load Private Key """
pem_file = open(f"{abs_path}\\keys\\rsa.pem")
der_data = b64decode('\n'.join(pem_file.read().splitlines()[1:-1]))
pem_file.close()
pem_key = load_der_private_key(der_data, password=None)
""" Decrypt File """
file = open(f"{abs_path}\\files\\file.bin", "rb")
data = file.read()
file.close()
decrypted_data = pem_key.decrypt(
data,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
file = open(f"{abs_path}\\files\\file.txt", "wb")
file.truncate()
file.write(decrypted_data)
file.close()
""" Remove Encrypted File """
os.remove(f"{abs_path}\\files\\file.bin")