使用PyCrypto的As3Crypto(flex) - 如何使AES工作?

时间:2014-06-18 16:53:38

标签: python flex encryption pycrypto as3crypto

我在Flex中有一个客户端,在Python中有一个服务器,我正在尝试在它们之间使用AES,但由于某种原因它不起作用。

我的服务器代码:

import sys
from Crypto.Cipher import AES
from binascii import hexlify, unhexlify

BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]

def encrypt(str):
    cipher = AES.new(unhexlify('some 64 byte key here'), AES.MODE_CBC, '16 byte iv')
    hex_str = hexlify(cipher.encrypt(pad(str)))
    return hex_str

我的客户代码:

static public function decrypt(txt:String) : String
{
    var k:String = "some 64 byte key here";
    var pad:IPad = new PKCS5();
    var mode:ICipher = Crypto.getCipher("aes-cbc", Hex.toArray(k), pad);
    pad.setBlockSize(mode.getBlockSize());

    var ivmode:IVMode = mode as IVMode;
    ivmode.IV = Hex.toArray(Hex.fromString("16 byte iv"));

    var data:ByteArray = Hex.toArray(Hex.toString(txt));
    mode.decrypt(data);

    return Hex.fromArray(data);
}

这似乎是一个简单的案例,但我做错了什么。它是什么?

BTW:我从mode.decrypt(数据)获得RangeError: Error #2006: The supplied index is out of bounds;

1 个答案:

答案 0 :(得分:1)

我终于设法让它发挥作用。

客户端代码应如下所示:

static public function decrypt(txt:String) : String
{
    var k:String = "some 64 byte key here";
    var pad:IPad = new PKCS5();
    var mode:ICipher = Crypto.getCipher("aes-cbc", Hex.toArray(k), pad);
    pad.setBlockSize(mode.getBlockSize());

    var ivmode:IVMode = mode as IVMode;
    ivmode.IV = Hex.toArray(Hex.fromString("16 byte iv"));

    var data:ByteArray = Hex.toArray(txt);
    mode.decrypt(data);

    return Hex.toString(Hex.fromArray(data));
}