我编写了以下程序,以CBC模式使用AES算法加密我的数据:

import hmac
import base64
import hashlib
from Crypto.Cipher import AES

AES_KEY = "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"
IV = "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"

print "KEY: ",
for i in AES_KEY:
    print str(i).encode("hex"),
print

print "IV:  ",
for i in IV:
    print str(i).encode("hex"),
print

# Encryption
def aes_encrypt(plain, key, iv):
    AES.key_size =16
    encryption_suite = AES.new(key, AES.MODE_CBC, iv)
    cipher_text = encryption_suite.encrypt(plain)
    return cipher_text

# Decryption
def aes_decrypt(cipher, key, iv):
    decryption_suite = AES.new(key, AES.MODE_CBC, iv)
    plain_text = decryption_suite.decrypt(cipher)
    return plain_text

result = aes_encrypt("testtesttesttest",AES_KEY,IV)

print "OUT: ",
for l in result:
    print str(l).encode("hex"),


但是,当我使用此在线工具检查其输出时,它们并不相等:

我的程序输出:

Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
KEY:  00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff
IV:   00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff
OUT:  90 e6 d6 31 61 66 eb dd ad 48 53 e8 a0 ca c6 48
>>>


在线工具:
python - 在线工具和Python脚本有两个不同的AES密文结果吗?-LMLPHP
为什么?

最佳答案

您的代码使用密钥00112233445566778899AABBCCDDEEFF,但您在网络上使用另一个密钥00112233445566778899AABBCCEEDDFF

在DD和EE之间切换。初始化向量也是如此。

关于python - 在线工具和Python脚本有两个不同的AES密文结果吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34837925/

10-10 16:18