【问题标题】:TypeError: argument should be a bytes-like object or ASCII string, not 'dict'TypeError: 参数应该是一个类似字节的对象或 ASCII 字符串,而不是 'dict'
【发布时间】:2021-11-06 14:47:19
【问题描述】:

我在解密加密消息时收到此错误。我从 django 视图中的 db 获取加密消息为 pw = donator.objects.filter(emai=email).values('passw') 并在 decrypt_message() 函数中传递 pw 对象。 decrypt_messag() 函数是:

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

错误信息是:

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'

【问题讨论】:

  • 显然你的keydict,正如错误所说...

标签: python django cryptography fernet


【解决方案1】:

您应该将与passw 关联的项目传递给函数,因此:

pws = donator.objects.filter(emai=email).values('passw')
for pw in pws:
    #               use pw['passw'], not pw ↓
    decrypt_message(encrypted_message, pw['passw'])

【讨论】:

  • 现在在按照您提到的进行更改后,我收到错误:ValueError: Fernet key must be 32 url-safe base64-encoded bytes。
猜你喜欢
  • 2022-11-10
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 2018-03-17
  • 2017-07-18
  • 1970-01-01
相关资源
最近更新 更多