2013-12-11 5 views
5

मैं एन्क्रिप्शन का उपयोग एक अजगर टीसीपी सर्वर के साथ AutoIt से संवाद करने के लिए कोशिश कर रहा हूँ, लेकिन मैं वहाँ कुछ मेरी एल्गोरिदम के साथ गलत दोनों encryptions के परिणामों के बाद से है लगता है/decryptions अलग हैं:AutoIt/डिक्रिप्ट

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

पायथन:

#!/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 

अजगर उत्पादन:

Encrypted string: NDJepp4CHh5C/FZb4Vdh4w== 
Decrypted string: Am I welcome??? 

एन्क्रिप्टेड परिणाम ही नहीं हैं ...

मेरे "बग" है?

+0

मैंने शुरू में सोचा कि यह स्ट्रिंग एन्कोडिंग के साथ एक मुद्दा था, लेकिन मैंने ऑटोआईट में जो कुछ भी सोच सकता हूं उसकी कोशिश की है और आपके पायथन कोड के समान परिणाम नहीं मिल सकता है। क्या यह [http://stackoverflow.com/a/12221931/611562) आपके पायथन कोड से प्रासंगिक है? – Matt

+0

यह वास्तव में पाइथन पक्ष पर एक समस्या प्रतीत होता है। मैंने ऑटोआईटी भाग के खिलाफ एनआईएसटी फाइलों से केएटी भाग लिया और यह सभी परीक्षण पास कर दिया। PyCrypto इसे पारित नहीं किया था। तो मुझे लगता है कि मुझे अजगर के लिए एक और एईएस कार्यान्वयन मिलना है। यह भी देखें: http://eli.thegreenplace.net/2010/06/25/aes-encryption-of-files-in-python-with-pycrypto/#comment-979860 –

+0

ऐसा लगता है कि मुझे "समस्या" मिली ... डिफ़ॉल्ट रूप से स्वचालित रूप से 0x00 पैडिंग और पायथन के लिए 0x20 का उपयोग करता है। जैसे ही मैंने कोड अपडेट किया, मैं "आत्म उत्तर" दूंगा। धन्यवाद मैट। –

उत्तर

6

समस्या paddings बदल रहा है और AutoIt में एक अलग एईएस कार्यान्वयन का उपयोग करके हल किया जा सकता:

यहां से 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??? 

पायथन:

#!/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 

कच्चे द्विआधारी भेजने के बाद से बेस 64 एन्कोडिंग/डिकोडिंग का उपयोग जारी न करें टीसीपी धाराओं के लिए ठीक है।