gpt4 book ai didi

python - 加密 : AES Decryption

转载 作者:太空狗 更新时间:2023-10-30 00:01:32 24 4
gpt4 key购买 nike

为什么 Pycrypto AES 解密在使用用于加密的 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

最佳答案

根据 BlockAlgo#encrypt从中派生 AES 类:

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.

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

关于python - 加密 : AES Decryption,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43199234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com