-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.py
31 lines (25 loc) · 849 Bytes
/
encrypt.py
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
from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
def encrypt(ID):
# Encrypting the file with the Fernet key
print("This is your unique key please save it: "+str(key.decode("utf-8")))
f = Fernet(key)
filename = ID
with open(filename, 'rb') as file:
file_data = file.read()
encrypted_data = f.encrypt(file_data)
with open(filename, 'wb') as file:
file.write(encrypted_data)
# Writing the Fernet key to a file(key.txt)
s = str(key.decode("utf-8"))
with open("key.txt", "w") as f:
f.write(s)
def decrypt(key, ID):
f = Fernet(key)
filename = ID
with open(filename, 'rb') as file:
encrypted_data = file.read()
decrtpted_data = f.decrypt(encrypted_data)
with open(filename, "wb") as file:
file.write(decrtpted_data)