Pycrypto:AES解密

6
为什么使用用于加密的AES对象解密Pycrypto AES时会产生不同的输出,而仅用于解密的AES对象则会产生正确的输出?
from Crypto.Cipher import AES
obj = AES.new('0123456789012345', AES.MODE_CBC, '0123456789012345')
message = '0123456789012345'
ciphertext = obj.encrypt(message)
plaintext = obj.decrypt(ciphertext)
# plaintext here is byte array
obj2 = AES.new('0123456789012345', AES.MODE_CBC, '0123456789012345')
plaintext = obj2.decrypt(ciphertext)
# plaintext here is 0123456789012345

2
调用 encrypt 后,obj 的状态是否被重置?否则,初始向量将会不同... - Maarten Bodewes
如何检查 obj 的状态以及它的不同之处,因为我无法看到库中用于加密和解密函数的源代码。 - Pankhuri Agarwal
也许在文档中有?我目前无法访问它... - Maarten Bodewes
@MaartenBodewes 我认为就是这样。我看不到其他可能的解释。答案可能在libtomcrypt的源代码以及被称为pycrypto的Python包装器中。 - Artjom B.
@ArtjomB。我在家里有文档访问权限,看来我的猜测是正确的,已经添加了答案... - Maarten Bodewes
1个回答

5
根据派生AES类的BlockAlgo#encrypt,有以下内容:

Encrypt data with the key and the parameters set at initialization.

The cipher object is stateful; encryption of a long block of data can be broken up in two or more calls to encrypt(). That is, the statement:

c.encrypt(a) + c.encrypt(b)

is always equivalent to:

c.encrypt(a+b)

That also means that you cannot reuse an object for encrypting or decrypting other data with the same key.

所以你的问题实际上直接记录在类中。

我不理解这个逻辑。考虑一个名为MyClass的类,其中包含方法double(self, number)half(self, number),分别将数字加倍和减半。halfdouble的反函数,因此您可以将其视为加密/解密。 cipher = MyClass(); cipher.half(cipher.double(3))确实返回3 - Eric Jin
操作double和half本身没有任何状态。请查看CBC模式,它将密文传播以用于参数化下一个块的加密/解密。第一个块使用IV进行参数化。 - Maarten Bodewes
然而,它们是具有相同属性和方法的相同类,只是不同的实例名称。 - Eric Jin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接