获得ValueError:使用Django和Python进行加密和解密

时间:2017-06-26 13:32:33

标签: python django encryption

尝试加密字符串类型值时出现以下错误。

错误:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/lampp/htdocs/d30/carClinic_vulnerable/bookingservice/views.py", line 141, in signsave
    obj = AES.new('this is a carkey123', AES.MODE_CBC, 'This is an IV456')
  File "/usr/lib/python2.7/dist-packages/Crypto/Cipher/AES.py", line 94, in new
    return AESCipher(key, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/Crypto/Cipher/AES.py", line 59, in __init__
    blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
ValueError: AES key must be either 16, 24, or 32 bytes long

我正在解释下面的代码。

name = request.POST.get('uname')
obj = AES.new('this is a carkey123', AES.MODE_CBC, 'This is an IV456')
enpass = obj.encrypt(name)

1 个答案:

答案 0 :(得分:2)

答案是在错误信息中用简单的字母写的:

  

“AES密钥长度必须为16,24或32字节”。

您的密钥('这是一个carkey123')长度为19个字节,请使用长度为16,24或32个字节的密钥。