问题
i tried to encrypt a text in as3crypto via the demo app.
now i am trying to decrypt the crypted text via php but it seems that the text is not properly decrypted. has anyone know how to properly decrypt it via PHP? or am I doing it wrong? please enlighten me...
here's the scenario:
encrypt this in http://crypto.hurlant.com/demo/:
encryption: AES
mode: CBC
padding: none
key: 11918f8bcd112e92744125008722050c
text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut massa nec purus laoreet posuere quis vitae tortor.
initialize vector: leave it blank
press encrypt. select base64 and copy the cipher text.
make a php script that has these codes and run it:
$cipher = MCRYPT_RIJNDAEL_128;
$mode = MCRYPT_MODE_CBC;
$key = "11918f8bcd112e92744125008722050c";
$cipher = "PLACE CIPHER TEXT HERE...";
$data = base64_decode($cipher);
echo mcrypt_decrypt($cipher, $key, $data, $mode);
回答1:
the issue probably lies in your key. While you can feed a hex string to as3crypto and it will know what do to with it, mcrypt_decrypt
will interpret each character as it's underlying ASCII value (like a = 97) instead of it's hexadecimal value (a = 10). Use the hex2bin method to convert the hex string into a byte string, and your decryption should probably work fine.
also, an issue may rest with different ideas of a default IV (initialization vector) between as3crypto and php. Since you are using CBC mode, you should be specifying an IV, just as a good security practice. also be aware of the potential pitfalls similar to those with your key, of specifying a hex string in as3 and needing to convert it in php.
回答2:
This would work:
PHP code:
//notice that $key and $iv length must be 16 chars long! ex: 1234567890123456
function decrypt($data,$key,$iv)
{
$decr= mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($data), MCRYPT_MODE_CBC, $iv);
return $decr;
}
And here is AS3 code
//notice that $key and $iv length must be 16 chars long! ex: 1234567890123456
private function encrypt(input:String,decrKey:String,decrIV:String):String
{
var inputBA:ByteArray=Hex.toArray(Hex.fromString(input));
var key:ByteArray = Hex.toArray(Hex.fromString(decrKey));
var pad:IPad = new NullPad();
var aes:ICipher = Crypto.getCipher("aes-cbc", key, pad);
var ivmode:IVMode = aes as IVMode;
ivmode.IV = Hex.toArray(Hex.fromString(decrIV));
aes.encrypt(inputBA);
return Base64.encodeByteArray( inputBA);
}
来源:https://stackoverflow.com/questions/15490903/decrypt-a-as3crypto-encrypted-text-in-php