Search code examples
actionscript-3as3crypto

Error: PKCS#5:unpad: Invalid padding value. expected [154], found [253] using as3crypto


I downloaded as3crypto and have been trying to integrate it into my project. I'm trying to do a simple test to match the decrypt results I'm getting in the demo - http://crypto.hurlant.com/demo/ but I'm getting the above error.

I'm working on the Secret Key tab. I'm using AES, CBC, PKCS5 and prepending the IV. Both the Key and Cipher text is set to HEX.

I first did an Encrypt and then copy the key and the cipher text to my function to test the decrypt to see if it match. I copied the code direct from the SecretTab.mxml and modified it a little to take the hard coded values.

I wrote a small program in C# to decrypt it using the same values and it works fine.

I verified the key and cipher text many times and it is correct.

    public static function decrypt2():void 
    {
        // 2: get a key
        var k:String = Hex.fromString("e5693983c5c21e0f6191eb025d12803d6d17c5359994bf435b964cd0c107fc2c");
        var kdata:ByteArray = Hex.toArray(k);
        //trace(String.fromCharCode(kdata[0]));

        // 3: get an output
        var txt:String = Hex.fromString("691682969f1946a1465ccfe19d429ace4188ee254425caa7fa84db5b1fba44a77f1dedfba7a1ffe516cb0646638e28f8ae6422b3cd63d380b21f8b8dcfbe067a");
        var data:ByteArray = Hex.toArray(txt);

        // 1: get an algorithm..
        var name:String = "simple-aes-cbc";              

        var pad:IPad = new PKCS5; //:new NullPad;
        //var pad:IPad = new NullPad();
        var mode:ICipher = Crypto.getCipher(name, kdata, pad);
        pad.setBlockSize(mode.getBlockSize());
        // if an IV is there, set it.
         if (mode is IVMode) {
                trace("mode is IVMode");
                var ivmode:IVMode = mode as IVMode;
                //ivmode.IV = Hex.toArray(iv.text);
            }
        mode.decrypt(data);
        trace(Hex.fromArray(data));
    }

Solution

  • I found my typos. I had to change the following lines

    var k:String =        Hex.fromString("e5693983c5c21e0f6191eb025d12803d6d17c5359994bf435b964cd0c107fc2c");
    

    to

    var k:String = "e5693983c5c21e0f6191eb025d12803d6d17c5359994bf435b964cd0c107fc2c";
        v
    

    and

    var txt:String = Hex.fromString("691682969f1946a1465ccfe19d429ace4188ee254425caa7fa84db5b1fba44a77f1dedfba7a1ffe516cb0646638e28f8ae6422b3cd63d380b21f8b8dcfbe067a");
    

    to

    var txt:String = "691682969f1946a1465ccfe19d429ace4188ee254425caa7fa84db5b1fba44a77f1dedfba7a1ffe516cb0646638e28f8ae6422b3cd63d380b21f8b8dcfbe067a";
    

    and

    trace(Hex.fromArray(data));
    

    to

    trace(Hex.toString(Hex.fromArray(data)));