我正在使用 AES 对象 (aesDecryptObj ) 来解密使用单独的 AES 对象 (aesEncryptObj ) 加密的密文。
def aesInit():
global aesEncryptObj
global aesDecryptObj
aesKey = <my key>
aesEncryptObj = AES.new(aesKey, AES.MODE_CBC, iv=<My Init Vector>)
aesDecryptObj = AES.new(aesKey, AES.MODE_CBC, iv=<My Init Vector>)
def aesEncrypt(clearStr):
global aesEncryptObj
padded_data = pad(str(clearStr).encode("utf-8"), aesEncryptObj.block_size)
e = aesEncryptObj.encrypt(padded_data)
eb64 = base64.b64encode(e)
d = eb64.decode('ascii')
return(d)
def aesDecrypt(encryptedStr):
global aesDecryptObj
e = base64.b64decode(encryptedStr)
b = aesDecryptObj.decrypt(e)
b = unpad(b, aesDecryptObj.block_size)
clearStr = b.decode('utf-8')
return(clearStr)
aesInit()
cypherText = aesEncrypt('test') #this line will render the same result no matter how many times it is repeated
print(aesDecrypt(cypherText)) #this line executes fine
print(aesDecrypt(cypherText)) #this line throws the "padding is incorrect" error
连续使用 aesEncryptObj 任意次数都会产生成功的结果,但是,当我使用 aesDecryptObj 连续两次或更多次解密给定的密文时,我收到以下错误:
File "/usr/lib64/python3.6/site-packages/Cryptodome/Util/Padding.py", line 90, in unpad
raise ValueError("Padding is incorrect.")
ValueError: Padding is incorrect.
如果给定相同的密文,aesDecryptObj 是否会产生与第一次解密该值时相同的结果?