首页 > 解决方案 > PyCrypto AES-CTR 模式得到不同的输出

问题描述

我使用 PyCrypto 加密我的数据,但为什么他们的输出不同?

from Crypto.Cipher import AES
from Crypto.Util import Counter


data = b'\x02\x01\xf2\xca\x04\x03\x02P\x02\x02\x01\x80\xd0\x0f\x80\xd0\x0f'
key = b'random 16 string'
nonce = b'string 16 rand\x00\x01'
cipher = AES.new(key, AES.MODE_CTR, counter=lambda: nonce)
data_encrypted = cipher.encrypt(data)
print(data_encrypted)
# output is: b'_M\xed(\t4\xc4\x94\x80\x83K\x94qL\x15+R'

counter = Counter.new(2 * 8, prefix=b'string 16 rand', initial_value=1)
cipher = AES.new(key, AES.MODE_CTR, counter=counter)
data_encrypted = cipher.encrypt(data)
print(data_encrypted)
# output is: b'_M\xed(\t4\xc4\x94\x80\x83K\x94qL\x15+\xce'

这两种方法都会导致内存泄漏。如何使用pycryptodome实现第一个效果

标签: python-3.xpycrypto

解决方案


推荐阅读