我创建了一个函数,它使用Crypto.Cipher
:
import os
from Crypto.Cipher import AES
def encrypt_pass(user_entered_plaintext):
encrypt_setup = AES.new(os.urandom(32), AES.MODE_CBC, os.urandom(16))
plaintext = user_entered_plaintext
ciphertext = encrypt_setup.encrypt(plaintext)
print(ciphertext)
return ciphertext
如果我尝试运行这个,比如:
^{pr2}$我收到此错误:
ValueError: Input strings must be a multiple of 16 in length
如果我尝试改变:
encrypt_setup = AES.new(os.urandom(32), AES.MODE_CBC, os.urandom(16))
到
encrypt_setup = AES.new(os.urandom(32), AES.MODE_CBC, os.urandom(8))
我得到:
ValueError: IV must be 16 bytes long
如何强制输入字符串最小值为8?在
只需指定PKCS#7填充即可。您必须在加密之前添加它,而在解密之后删除它,因为对于编写pycrypto的bozos来说,并没有将填充作为一个选项。(我觉得PHP-mcrypt不好!)通过codealk查看链接。在
AES加密是基于块的,块大小是16字节,因此如果输入数据不是块大小的倍数,则必须添加块大小填充。在
在第二次尝试中,您将iv更改为8字节,但它必须是16字节的块大小。在
但您没有保存密钥或iv,因此以后无法解密数据。您需要为它们使用变量。在
相关问题 更多 >
编程相关推荐