2013-12-11 100 views
5

我試圖從AutoIt的使用加密Python的TCP服務器進行通信,但我覺得既然兩個加密的結果有什麼毛病我的算法/的解密是不同的:的AutoIt到Python加密/解密

的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??? 

加密的結果是不一樣的......

哪裏是我的 「錯誤」?

+0

我最初認爲這是一個字符串編碼的問題,但我已經嘗試了所有我能想到的在AutoIt中,並且無法獲得與Python代碼相同的結果。是[this](http://stackoverflow.com/a/12221931/611562)與你的python代碼有關嗎? – Matt

+0

這真的好像是python方面的一個問題。我從NIST文件中針對AutoIT部分運行KAT,並通過了所有測試。 PyCrypto沒有通過它。所以我認爲我必須爲python找到另一個AES實現。另見:http://eli.thegreenplace.net/2010/06/25/aes-encryption-of-files-in-python-with-pycrypto/#comment-979860 –

+0

似乎我發現了「問題」...默認情況下,AutoIT使用0x00填充,python使用0x20。只要我更新了代碼,我就會「自我回答」。謝謝馬特。 –

回答

6

這個問題可以通過改變填充和使用的AutoIt不同的AES實現解決:

rijndael.au3從這裏:http://www.autoitscript.com/forum/topic/44581-crypto-suite/

的AutoIt:

#include <rijndael.au3> 
#include <String.au3> 

Global $key = "pjqFX32pfaZaOkkC"; 
Global $text = "Am I welcome???" 
$encrypted = _StringToHex(BinaryToString(_rijndaelCipher($key, $text, 128, 0, ''))) 
ConsoleWrite("Encrypted: " & $encrypted & @CRLF) 
$decrypted = BinaryToString(_rijndaelInvCipher($key, _HexToString($encrypted), 128, 0, '')) 
ConsoleWrite("Decrypted: " & $decrypted & @CRLF) 

輸出:

Encrypted: A6848F1EF8C7C1313689E18567235A93 
Decrypted: Am I welcome??? 

Python:

#!/usr/bin/env python 

from Crypto.Cipher import AES 
import base64 

BLOCK_SIZE = 16 

PADDING = chr(0) 

pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING 

EncodeAES = lambda c, s: base64.b16encode(c.encrypt(pad(s))) 
DecodeAES = lambda c, e: c.decrypt(base64.b16decode(e)).rstrip(PADDING) 

text = 'Am I welcome???' 
secret = 'pjqFX32pfaZaOkkC' 

cipher=AES.new(key=secret, mode=AES.MODE_ECB) 

encoded = EncodeAES(cipher, text) 
print 'Python Encrypted string: ', encoded 

decoded = DecodeAES(cipher, encoded) 
print 'Python Decrypted string: ', decoded.encode("hex") 
print 'Python Decrypted string: ', decoded 

myencoded = "A6848F1EF8C7C1313689E18567235A93" 
print "AutoIt Result:   ", myencoded 
decoded = DecodeAES(cipher, myencoded) 
print 'From AU Decrypted string:', decoded 
mydecoded = EncodeAES(cipher, decoded) 
print 'Re-Encrypted string:  ', mydecoded.upper() 

輸出:

Python Encrypted string: A6848F1EF8C7C1313689E18567235A93 
Python Decrypted string: 416d20492077656c636f6d653f3f3f 
Python Decrypted string: Am I welcome??? 
AutoIt Result:   A6848F1EF8C7C1313689E18567235A93 
From AU Decrypted string: Am I welcome??? 
Re-Encrypted string:  A6848F1EF8C7C1313689E18567235A93 

不要繼續使用base64編碼/解碼,因爲在發送原始二進制是細的TCP流。