import base64 from cryptography.fernet import Fernet, InvalidToken def encrypt_string(string, key): """ Encrypts a string with the specified key. The key must be 32 raw bytes. """ f = Fernet(base64.urlsafe_b64encode(key)) return f.encrypt(string) def decrypt_string(string, key, ttl=None): """ Decrypts an encrypted string with the specified key. The key must be 32 raw bytes. """ f = Fernet(base64.urlsafe_b64encode(key)) try: return f.decrypt(string, ttl=ttl) except InvalidToken: return None except TypeError: return None