import base64 import sys from cryptography.fernet import Fernet from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC def decrypt(salt, password, data): kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt.encode("ascii"), iterations=480000) key = base64.urlsafe_b64encode(kdf.derive(password.encode('ascii'))) f = Fernet(key) return f.decrypt(data.encode("ascii")).decode("ascii") def encrypt(salt, password, data): kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt.encode("ascii"), iterations=480000) key = base64.urlsafe_b64encode(kdf.derive(password.encode('ascii'))) f = Fernet(key) return f.encrypt(data.encode("ascii")).decode("ascii")