Skip to main content
 首页 » 编程设计

python之在线工具和Python脚本有两个不同的AES密文结果

2024年10月24日11shihaiming

我编写了以下程序,以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 
>>>  

在线工具: 为什么?

请您参考如下方法:

您的代码使用 key 00112233445566778899AABBCCDDEEFF 但您在网络上使用另一个 key namly 00112233445566778899AABBCCEEDDFF

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