0

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));
    }
jbassking10
  • 833
  • 4
  • 15
  • 42

2 Answers2

0

You need to explicitly set both byte arrays, the data and the IV, before decrypting. I don't think as3crypto will break it out for you, even if the simple flag is used on the name.

I don't know what your example states, however the error indicates that the byte array is not correct. Example with the IV explicitly set.

var cipher:ICipher = Crypto.getCipher("aes256-cbc", keydata);;
var dataBA:ByteArray = Hex.toArray(Hex.fromString(data));
var data:ByteArray = Hex.toArray(Hex.fromString("some data"));
(cipher as IVMode).IV = Hex.toArray(Hex.fromString("some IV"));
cipher.decrypt(data);
0xc3
  • 26
  • 4
  • The code from the demo only sets it if mode is IVMode – jbassking10 Aug 13 '16 at 17:30
  • cbc is IVmode. Your sample has the setting of the IV mode commented out. I edited the answer to show a sample of setting the IV. – 0xc3 Aug 13 '16 at 18:57
  • The if statement fails. The IV is pre-pended to the cipher itself so it would have to be read and removed from the cipher. That's from what I understand. – jbassking10 Aug 13 '16 at 19:51
0

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))); 
jbassking10
  • 833
  • 4
  • 15
  • 42