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

Input string length limit in 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

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

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

按照 this topic 中描述的步骤进行操作(我觉得这不像是重复的,但答案对你很有用)。