如何在AS3中使用AES-CBC 128位加密?

时间:2014-04-07 11:15:57

标签: actionscript-3 encryption aes

我想用AES-CBC 128-bit加密AS3中的字符串。怎么做?

1 个答案:

答案 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;   
}