0

I am trying to decrypt my password but it keeps giving me an error saying my token is invalid which is weird as i use it in the lines before and it works.

the error is:

cryptography.fernet.InvalidToken
from cryptography.fernet import Fernet
import time
key = Fernet.generate_key()
cipher_suite = Fernet(key)

cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
plain_text = cipher_suite.decrypt(cipher_text)
PASSWORD = b'gAAAAABjsxMwxBTFdQ9_tFMYv-Ko1Ja7t5afvMiJBehRMqJyp-OapyRbP2IPBsboI-rZilgXhcIIpWWXq1Bwo732st7celJvX52Ahxten8lbF0LMzw7ND9Q9r2UFPeTyJjOp9Dr0mqhd'
password_decrypted = cipher_suite.decrypt(PASSWORD)
Traceback (most recent call last):
  File "/home/codespace/.python/current/lib/python3.10/site-packages/cryptography/fernet.py", line 133, in _verify_signature
    h.verify(data[-32:])
  File "/home/codespace/.python/current/lib/python3.10/site-packages/cryptography/hazmat/primitives/hmac.py", line 70, in verify
    ctx.verify(signature)
  File "/home/codespace/.python/current/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/hmac.py", line 84, in verify
    raise InvalidSignature("Signature did not match digest.")
cryptography.exceptions.InvalidSignature: Signature did not match digest.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/workspaces/big-schoolwork-repo/password manager/cli/password manager/main.py", line 9, in <module>
    password_decrypted = cipher_suite.decrypt(PASSWORD)
  File "/home/codespace/.python/current/lib/python3.10/site-packages/cryptography/fernet.py", line 90, in decrypt
    return self._decrypt_data(data, timestamp, time_info)
  File "/home/codespace/.python/current/lib/python3.10/site-packages/cryptography/fernet.py", line 151, in _decrypt_data
    self._verify_signature(data)
  File "/home/codespace/.python/current/lib/python3.10/site-packages/cryptography/fernet.py", line 135, in _verify_signature
    raise InvalidToken
cryptography.fernet.InvalidToken
k972
  • 3
  • 3
  • Can you [edit] your question to include the full traceback of your error? – Michael M. Jan 02 '23 at 18:29
  • why is there a 2 on line 3 – ppap Jan 02 '23 at 18:37
  • sorry just ignore that must of accidentally added it when editing question – k972 Jan 02 '23 at 18:38
  • might be with the " " vs ' ' you are using , maybe try to keep this consistent – ppap Jan 02 '23 at 18:38
  • what do you mean by that? ""vs'' – k972 Jan 02 '23 at 18:41
  • 1
    Its because you didn't `.encrypt` `PASSWORD`. `PASSWORD` is a bytes like object, which is the same return type from `.encrypt`. Which is why you don't get a `TypeError`, but rather an `InvalidToken` error. – Shmack Jan 02 '23 at 18:42
  • 1
    oh okay, thank you i understand what was going wrong now. – k972 Jan 02 '23 at 18:43
  • If you want to make this library even more confusing for a beginner, wait until you start using SHAH encryptions and need more space for your message you want to encrypt, then fernet really comes in handy. – Shmack Jan 02 '23 at 18:48

0 Answers0