0

I am getting Fernet Decryption error error at line(f = Fernet(private_key)) .Please help on this.

Error:ValueError: Fernet key must be 32 url-safe base64-encoded bytes.

    from cryptography.fernet import Fernet
    import base64

    config_path = os.path.join(str(Utils.get_project_root()), 'key.pem')
    with open(config_path, "rb") as file_handle:
        private_key = file_handle.read()
    decoded_msg = base64.urlsafe_b64decode(encoded_text)
    f = Fernet(private_key)
    decrypted_msg = f.decrypt(decoded_msg)
    decrypted_msg.decode()
Sarvendra Singh
  • 109
  • 1
  • 1
  • 9
  • 1
    As the error message already states, the key must be 32 bytes long and Base64url encoded. Is it? With regard to the extension .pem maybe not, you should post a sample key. – Topaco Aug 31 '21 at 06:34
  • In addition to above mentioned comment, `base64.urlsafe_b64decode` has some strange behaviour. I remember running into issues with JWT when using that code too. Might be worth to take a look at – Bram Aug 31 '21 at 07:22
  • @Topaco here which of two (key.pem or encoded_text ) should be must be 32 bytes long and Base64url encoded ? – Sarvendra Singh Aug 31 '21 at 13:34
  • `private_key` (the Fernet key) and thus the content of the `key.pem` file. Check [this](https://cryptography.io/en/latest/fernet/?highlight=fernet#fernet-symmetric-encryption). – Topaco Aug 31 '21 at 13:52

0 Answers0