decrypt_str

EncryptHelper.decrypt_str(data: Union[str, bytes], key: Union[str, bytes] = None)str[source]

Decrypts data previously encrypted using encrypt_str() with the same Fernet compatible key, and returns the decrypted version as a string.

The key cannot just be a random “password”, it must be a 32-byte key encoded with URL Safe base64. Use the method generate_key() to create a Fernet compatible encryption key.

Under the hood, Fernet uses AES-128 CBC to encrypt the data, with PKCS7 padding and HMAC_SHA256 authentication.

If the key parameter isn’t passed, or is empty (None / “”), then it will attempt to fall back to encrypt_key - if that’s also empty, EncryptKeyMissing will be raised.

Parameters
  • data (str) – The base64 encoded data to be decrypted, in the form of either a str or bytes.

  • key (str) – A Fernet encryption key (base64) for decryption, if blank, will fall back to encrypt_key

Raises
  • EncryptKeyMissing – Either no key was passed, or something is wrong with the key.

  • EncryptionError – Something went wrong while attempting to decrypt the data

Return str decrypted_data

The decrypted data as a string