输入字符串长度限制在pycrypto中

时间:2017-03-16 14:20:29

标签: python pycrypto

我正在尝试使用 pycrypto 加密我的数据。我已经为此编写了以下代码。

from Crypto.Cipher import AES
obj = AES.new('1234567891011123', AES.MODE_CBC, 'This is an IV456')
message = "Jeannine"
ciphertext1 = obj.encrypt(message)
print(ciphertext1)
message1 = "Jeannine"
ciphertext2 = obj.encrypt(message1)
print(ciphertext2)
obj2 = AES.new('1234567891011123', AES.MODE_CBC, 'This is an IV456')
dciphertext1 = obj2.decrypt(ciphertext1)
print(dciphertext1)
dciphertext2=obj2.decrypt(ciphertext2)
print(dciphertext2)

但我收到的错误

Traceback (most recent call last):
  File "cipher.py", line 4, in <module>
    ciphertext1 = obj.encrypt(message)
ValueError: Input strings must be a multiple of 16 in length

如何在输入字符串中保持控制?输入字符串可以是任意长度。

1 个答案:

答案 0 :(得分:1)

您在CBC模式下使用AES,它需要长度为16的倍数的字符串,因此您可能需要添加一些填充。

按照this topic中描述的步骤操作(这看起来不像我的副本,但答案对您有用)。

相关问题