How to decrypt AES 128 in CryptoJS (nodejs / web browser)

13,430

This worked for me:

var CryptoJS = require("crypto-js");
var data = JSON.stringify({abc: 'xyz'});

var encrypted = CryptoJS.AES.encrypt(data, "my-secret");
console.log(encrypted.toString());

var decrypted = CryptoJS.AES.decrypt(encrypted, "my-secret");
var object = JSON.parse(decrypted.toString(CryptoJS.enc.Utf8));
console.log(object);
console.log(object.abc);

Hope this is what you wanted to do. This should also work with Base64 inputs.

Share:
13,430
Gregor Mountain
Author by

Gregor Mountain

Updated on June 22, 2022

Comments

  • Gregor Mountain
    Gregor Mountain almost 2 years

    I'm using a nodejs server, and i successfully encrypt/decrypt (with IV & Key) a json in Base64 with crypto node module.

    However, when I send an emit action with my AES 128 CTR json to a web client, i received correctly my json with base64 data encrypted, but my problem is when i wan't to decrypt this.

    In CryptoJS documentation, i found an example like this :

    var text = "Hello world";
    var key = CryptoJS.enc.Base64.parse("myKey");
    var iv  = CryptoJS.enc.Base64.parse("myIv");
    
    var encrypted = CryptoJS.AES.encrypt(text, key, {iv: iv});
    console.log(encrypted); // print something like that s {init: function, $super: s, ciphertext: l.WordArray.t.extend.init, key: s, iv: s…}
    
    var decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv: iv});
    console.log(decrypted.toString(CryptoJS.enc.Utf8)); // print Hello world
    

    We can easily decrypt an "Hello world" because it was encrypted by CryptoJS in my web client. But my problem is that i want to decrypt a data which is not using CryptoJS encrypt system.

    I want to do something like this :

    var text = "myJsonEncryptedInBase64";
    var key = CryptoJS.enc.Base64.parse("myKey");
    var iv  = CryptoJS.enc.Base64.parse("myIv");
    
    
    var decrypted = CryptoJS.AES.decrypt(text, key, {iv: iv});
    console.log(decrypted.toString(CryptoJS.enc.Utf8)); //print my decrypted json
    

    I've got an error because CryptoJS decrypt method using an array data like this :

    s {init: function, $super: s, ciphertext: l.WordArray.t.extend.init, key: s, iv: s…}
    

    Can you please help me ?

    Thanks a lot.

    Edit :

    Node.js input : {name:"toto", age:"15"} (encrypted with crypto in Base64).

    Node.js output: mfrpKm5k5YtGkX6rp9/Bmz+cCkcz5tiLKQcxmOpDUow=

    Emitting output throw socket.io to web client.

    Web client JS input : mfrpKm5k5YtGkX6rp9/Bmz+cCkcz5tiLKQcxmOpDUow=

    Web client JS ouput : {name:"toto", age:"15"} (Decrypting with same IV & KEY)

    • Artjom B.
      Artjom B. about 9 years
      CryptoJS uses CBC mode by default, so if you want to use CTR, you have to set this option during decryption. Can you show example input and expected output?
    • Gregor Mountain
      Gregor Mountain about 9 years
      @ArtjomB. My bad, i'm using "aes-128-cbc" algorythm indeed. For example i've got a json in my node server {name:"toto", age:"15"}, i used crypto to encrypt my json in Base64. And i want to decrypt it in my website with CryptoJS decrypt method.