TypeError: argument should be a bytes-like object or ASCII string, not 'dict'
Asked Answered
S

1

8

I am getting this error when I am decrypting the encrypted message.I fetched the the encrypted message from the db in django views as pw = donator.objects.filter(emai=email).values('passw')and passed the pw object in the decrypt_message() function. The decrypt_messag() function is :

def decrypt_message(encrypted_message,key):
    """
    Decrypts an encrypted message
    """
    f = Fernet(key)
    decrypted_message = f.decrypt(encrypted_message)
    return decrypted_message.decode()

The error message is:

File "C:\Users\Neelesh Singh\workspace\BookHouse\donatorapp\views.py", line 129, in decrypt_message
    f = Fernet(key)
  File "C:\Users\Neelesh Singh\AppData\Local\Programs\Python\Python39\lib\site-packages\cryptography\fernet.py", line 37, in __init__
    key = base64.urlsafe_b64decode(key)
  File "C:\Users\Neelesh Singh\AppData\Local\Programs\Python\Python39\lib\base64.py", line 131, in urlsafe_b64decode
    s = _bytes_from_decode_data(s)
  File "C:\Users\Neelesh Singh\AppData\Local\Programs\Python\Python39\lib\base64.py", line 45, in _bytes_from_decode_data
    raise TypeError("argument should be a bytes-like object or ASCII "
TypeError: argument should be a bytes-like object or ASCII string, not 'dict'
Stouffer answered 6/11, 2021 at 14:47 Comment(0)
T
1

You should pass the item associated with passw to the function, so:

pws = donator.objects.filter(emai=email).values('passw')
for pw in pws:
    #               use pw['passw'], not pw ↓
    decrypt_message(encrypted_message, pw['passw'])
Tierratiersten answered 6/11, 2021 at 14:51 Comment(3)
Now after doing the chnges as you mentioned I am getting error : ValueError: Fernet key must be 32 url-safe base64-encoded bytes.Stouffer
@Stouffer the password string has to be converted to bytes before passing to the function. Do pw['passw'].encode()Inglebert
@AswinMurugesh: It doesn't.Deceit

© 2022 - 2024 — McMap. All rights reserved.