I got a little problem regarding AES decryption. I have to decrypt some text that has been encrypted using 128bit AES / ECB with PHP mcrypt. The thing is, the key that is being used to encrypt/decrypt is 32 bytes long instead of 16. This is all good when I encrypt/decrypt through mcrypt, but now I have to write a decrypt function in another language (LUA). The problem I have is that the library I'm using in LUA doesn't support keys longer than 16 bytes, it has to be exactly 16 bytes long. I guess that mcrypt internally transforms keys longer than 16 bytes into a 16 byte form, but I don't know how. What I'm trying to do is to do the exact same key transformation in LUA so my library can decrypt it. Changing the key is a no go, as it was given by a service provider. Here's the PHP code that does the encrypt/decrypt:
function aes_encrypt($sStr, $sKey = "12345678901234561234567890123456") {
return base64_encode(mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
$sKey,
$sStr,
MCRYPT_MODE_ECB
));
}
function aes_decrypt($sStr, $sKey = "12345678901234561234567890123456") {
$str = rtrim(mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$sKey,
base64_decode($sStr),
MCRYPT_MODE_ECB
),"\0\4");
return preg_replace("/[^\d]/", "", $str);
}
Any help is much appreciated.