-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt.py
More file actions
31 lines (21 loc) · 711 Bytes
/
crypt.py
File metadata and controls
31 lines (21 loc) · 711 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
import cryptography
from cryptography.fernet import Fernet
def encrypt_(original):
key = Fernet.generate_key()
fernet = Fernet(key)
encrypted = fernet.encrypt(original.encode("utf-8"))
with open('data', 'wb') as encrypted_file:
encrypted_file.write(key+encrypted)
def decrypt_():
with open('data', 'rb') as datafile:
key = datafile.read(44)
data = datafile.read()
try:
fernet = Fernet(key)
except ValueError:
print("Corrupted key")
return "[]"
try:
return fernet.decrypt(data).decode("utf-8")
except (cryptography.fernet.InvalidToken, TypeError):
print("Invalid Token")