decrypt a as3crypto encrypted text in PHP

后端 未结 2 836
星月不相逢
星月不相逢 2021-01-16 12:04

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. ha

相关标签:
2条回答
  • 2021-01-16 13:06

    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.

    0 讨论(0)
  • 2021-01-16 13:06

    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);
    }       
    
    0 讨论(0)
提交回复
热议问题