我想用AES-CBC 128-bit
加密AS3中的字符串。怎么做?
答案 0 :(得分:2)
使用AS3Crypto https://code.google.com/p/as3crypto/,以下是摘录:
//notice that decrKey and decrIV 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); //if not use Base64 encode, data would be just byteArray
}
注意:这段代码完全兼容ex:用PHP(或C#),这里用PHP解密它
//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;
}