【问题标题】:AutoIt to Python encrypt/decryptAutoIt 到 Python 加密/解密
【发布时间】: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。更新代码后,我将立即“自我回答”。谢谢马特。

标签: python aes autoit


【解决方案1】:

可以通过更改填充和在 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 流。

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2019-11-04
    • 2019-10-27
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    相关资源
    最近更新 更多