Search code examples
javascriptencryptionaescryptojs

AES encryption with jquery


I am trying to encrypt and decrypt AES locally using the CryptoJS library.
I have the code from the example:

var encrypted = CryptoJS.AES.encrypt(mess, pass);
var decrypted = CryptoJS.AES.decrypt(encrypted, pass);

but the decrypted variable is not returing the mess variable? Why is that?

Please see JSFiddle?


Solution

  • From the documentation:

    The hash you get back isn't a string yet. It's a WordArray object. When you use a WordArray object in a string context, it's automatically converted to a hex string.

    [...]

    You can convert a WordArray object to other formats by explicitly calling the toString method and passing an encoder.

    Replace decrypted with decrypted.toString(CryptoJS.enc.Utf8)), see the updated fiddle.