首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AutoIt到Python的加密/解密

AutoIt到Python的加密/解密
EN

Stack Overflow用户
提问于 2013-12-11 05:08:01
回答 1查看 1.8K关注 0票数 5

我试图使用加密从AutoIt与Python服务器进行通信,但我认为我的算法有问题,因为这两种加密/解密的结果是不同的:

AutoIt:

代码语言:javascript
复制
#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输出:

代码语言:javascript
复制
Encrypted: ZFBnThUDPRuIUAPV6vx9Ng==
Decrypted: Am I welcome???

Python:

代码语言:javascript
复制
#!/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输出:

代码语言:javascript
复制
Encrypted string: NDJepp4CHh5C/FZb4Vdh4w==
Decrypted string: Am I welcome???

加密结果是不一样的..。

我的“窃听器”呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-11 20:24:41

这个问题可以通过更改填充和在AutoIt中使用不同的AES实现来解决:

来自这里的rijndael.au3:http://www.autoitscript.com/forum/topic/44581-crypto-suite/

AutoIt:

代码语言:javascript
复制
#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)

输出:

代码语言:javascript
复制
Encrypted: A6848F1EF8C7C1313689E18567235A93
Decrypted: Am I welcome???

Python:

代码语言:javascript
复制
#!/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()

输出:

代码语言:javascript
复制
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流是可以的。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20510975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档