-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.py
More file actions
25 lines (20 loc) · 631 Bytes
/
Copy pathencryption.py
File metadata and controls
25 lines (20 loc) · 631 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
from cryptography.fernet import Fernet
import os
KEY_FILE = "key.key"
def generate_key():
key = Fernet.generate_key()
with open(KEY_FILE, "wb") as key_file:
key_file.write(key)
def load_key():
if not os.path.exists(KEY_FILE):
generate_key()
with open(KEY_FILE, "rb") as key_file:
return key_file.read()
def encrypt_password(password):
key = load_key()
fernet = Fernet(key)
return fernet.encrypt(password.encode()).decode()
def decrypt_password(encrypted_password):
key = load_key()
fernet = Fernet(key)
return fernet.decrypt(encrypted_password.encode()).decode()