admin管理员组文章数量:1279049
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)
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)
Share Improve this question edited Feb 18, 2015 at 14:46 Gregor Mountain asked Feb 18, 2015 at 12:09 Gregor MountainGregor Mountain 2131 gold badge2 silver badges9 bronze badges 2- 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? – Artjom B. Commented Feb 18, 2015 at 12:53
- @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. – Gregor Mountain Commented Feb 18, 2015 at 13:08
1 Answer
Reset to default 9This 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.
本文标签: javascriptHow to decrypt AES 128 in CryptoJS (nodejsweb browser)Stack Overflow
版权声明:本文标题:javascript - How to decrypt AES 128 in CryptoJS (nodejsweb browser) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741217273a2360287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论