John Little
John Little

Reputation: 12345

crypto-js cant decrypt what it encrypted

I need to encrypt a sting with javascript using AES CBC no pad, pass the IV and encrypted data as HEX over HTTP, then decrypt with javascript on the server side.

The decryption function works, in that I can correctly decrypt data ecrypted using hurlant AS3 libraries correctly. However, the below encryption is not working - the result cannot be decrypted using the decrypt function, nor can it be decrypted using the hurant demo at: http://crypto.hurlant.com/demo/

Instead of the actual data, I am using "1234" as the message in this example.

I have searched and found no documentation for any of this library or its functions, beyond the quickstart guide which only has trivial cases, so everything is by trial and error. I have tried hundreds of variations of the below.

Example generated IV as Hex: "15ae89d17f632d21f0cda04734d38694"

Example generated encrypte data as HEX: "44ddf295"

Example message: "15ae89d17f632d21f0cda04734d3869444ddf295"

Can anyone see what is wrong in my encrypt() function?

// this function doesnt work - the resultant message (which is 
//       IV+Ecypted text all as HEX cannot be decrypted.
function encrypt() {
    var key = CryptoJS.enc.Hex.parse('48656c6c6f2c20576f726c6421888888');
    var IVLEN = 16; // Im guessing this is 16 bytes.
    var iv= CryptoJS.lib.WordArray.random(IVLEN);
    var encrypted;
    var message;

    encrypted = CryptoJS.AES.encrypt("1234", key, { iv: iv, padding: CryptoJS.pad.NoPadding,  mode: CryptoJS.mode.CBC });

    message = CryptoJS.enc.Hex.stringify(iv) + CryptoJS.enc.Hex.stringify(encrypted.ciphertext);

    var test = decrypt(message);  // throws a malformed UTF-8 exception

    alert (test); // should alert "1234"

    return message;

}

// this function works perfectly with data generated using HURLANT crypto libs.
function decrypt(data) {
    var key = CryptoJS.enc.Hex.parse('48656c6c6f2c20576f726c6421888888');
    var ivHexStr, iv;
    var encMessageHexStr;
    var IVLEN = 32;  // This is 16 bytes, as one byte is 2 Hex chars.
    var encrypted = {};
    var decrypted;
    var result;

    ivHexStr = data.substring(0,IVLEN);
    encMessageHexStr = data.substring(IVLEN);

    iv = CryptoJS.enc.Hex.parse(ivHexStr);
    encrypted.key        = key;
    encrypted.iv         = iv;
    encrypted.ciphertext = CryptoJS.enc.Hex.parse(encMessageHexStr);

    decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv, padding: CryptoJS.pad.NoPadding, mode: CryptoJS.mode.CBC });

    result = CryptoJS.enc.Utf8.stringify(decrypted);

    return(result);
}; //decrypt()

Upvotes: 4

Views: 7479

Answers (1)

jbtule
jbtule

Reputation: 31799

With CBC mode padding is required. Neither CFB or OFB need padding.

CryptoJS supports the following modes:

  • CBC (the default)
  • CFB
  • CTR
  • OFB
  • ECB

And CryptoJS supports the following padding schemes:

  • Pkcs7 (the default)
  • Iso97971
  • AnsiX923
  • Iso10126
  • ZeroPadding
  • NoPadding

Upvotes: 2

Related Questions