在线工具和Python脚本有两种不同的AES密文结果?

时间:2016-01-17 11:43:51

标签: python python-2.7 encryption aes pycrypto

我在 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
>>> 

在线工具: enter image description here 为什么呢?

1 个答案:

答案 0 :(得分:2)

您的代码使用密钥00112233445566778899AABBCC DDEE FF,但您在网络上使用另一个密钥namly 00112233445566778899AABBCC EEDD FF

其中 DD EE 已切换。初始化向量也是如此。