I have a client in Flex and a server in Python and i'm trying to make AES work between them but for some reason it doesn't work.
My server code:
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
My client code:
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);
}
It seems like a simple case but I'm doing something wrong. What is it?
BTW: I get RangeError: Error #2006: The supplied index is out of bounds
from mode.decrypt(data);
I finally managed to get it to work.
The client code should look like this:
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));
}