javascript - crypto-js cant decrypt what it encrypted -


i need encrypt sting javascript using aes cbc no pad, pass iv , encrypted data hex on http, decrypt javascript on server side.

the decryption function works, in can correctly decrypt data ecrypted using hurlant as3 libraries correctly. however, below encryption not working - result cannot decrypted using decrypt function, nor can decrypted using hurant demo at: http://crypto.hurlant.com/demo/

instead of actual data, using "1234" message in example.

i have searched , found no documentation of library or functions, beyond quickstart guide has trivial cases, trial , error. have tried hundreds of variations of below.

example generated iv hex: "15ae89d17f632d21f0cda04734d38694"

example generated encrypte data hex: "44ddf295"

example message: "15ae89d17f632d21f0cda04734d3869444ddf295"

can see wrong in encrypt() function?

// function doesnt work - resultant message (which  //       iv+ecypted text hex cannot decrypted. function encrypt() {     var key = cryptojs.enc.hex.parse('48656c6c6f2c20576f726c6421888888');     var ivlen = 16; // im guessing 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 malformed utf-8 exception      alert (test); // should alert "1234"      return message;  }  // function works data generated using hurlant crypto libs. function decrypt(data) {     var key = cryptojs.enc.hex.parse('48656c6c6f2c20576f726c6421888888');     var ivhexstr, iv;     var encmessagehexstr;     var ivlen = 32;  // 16 bytes, 1 byte 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() 

with cbc mode padding required. neither cfb or ofb need padding.

cryptojs supports following modes:

  • cbc (the default)
  • cfb
  • ctr
  • ofb
  • ecb

and cryptojs supports following padding schemes:

  • pkcs7 (the default)
  • iso97971
  • ansix923
  • iso10126
  • zeropadding
  • nopadding

Comments