【发布时间】:2013-12-28 22:39:40
【问题描述】:
我正在尝试使用加密从 AutoIt 与 Python TCP 服务器进行通信,但我认为我的算法有问题,因为两种加密/解密的结果不同:
AutoIt:
#include <Crypt.au3>
Global $key = "pjqFX32pfaZaOkkCFQuYziOApaBgRE1Y";
Global $str = "Am I welcome???"
_Crypt_Startup()
$hKey = _Crypt_DeriveKey($key, $CALG_AES_256)
$s = _Crypt_EncryptData($str, $hKey, $CALG_USERKEY)
$s = _Base64Encode($s)
ConsoleWrite("Encrypted: " & $s & @CRLF)
$s = _Base64Decode($s)
$str = _Crypt_DecryptData($s, $hKey, $CALG_USERKEY)
ConsoleWrite("Decrypted: " & BinaryToString($str) & @CRLF)
AutoIt 输出:
Encrypted: ZFBnThUDPRuIUAPV6vx9Ng==
Decrypted: Am I welcome???
Python:
#!/usr/bin/env python
from Crypto.Cipher import AES
import base64
import binascii
BLOCK_SIZE = 16
PADDING = binascii.unhexlify(b"07")
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
secret = 'pjqFX32pfaZaOkkCFQuYziOApaBgRE1Y'
cipher=AES.new(key=secret, mode=AES.MODE_ECB)
encoded = EncodeAES(cipher, 'Am I welcome???')
print 'Encrypted string:', encoded
decoded = DecodeAES(cipher, encoded)
print 'Decrypted string:', decoded
Python 输出:
Encrypted string: NDJepp4CHh5C/FZb4Vdh4w==
Decrypted string: Am I welcome???
加密后的结果不一样...
我的“错误”在哪里?
【问题讨论】:
-
我最初认为这是字符串编码的问题,但我已经尝试了 AutoIt 中我能想到的所有方法,但无法获得与您的 python 代码相同的结果。 this 与你的 python 代码相关吗?
-
这似乎真的是python方面的问题。我从 NIST 文件中针对 AutoIT 部分运行了 KAT,它通过了所有测试。 PyCrypto 没有通过它。所以我想我必须为 python 找到另一个 AES 实现。另见:eli.thegreenplace.net/2010/06/25/…
-
似乎我发现了“问题”... AutoIT 默认使用 0x00 进行填充,python 使用 0x20。更新代码后,我将立即“自我回答”。谢谢马特。