简体   繁体   中英

AES Encryption In .Net And Decryption In Node JS

I have a simple node js code with crypto js library through which I am decrypting a json object which I will receive from a .net based desktop application, hence the whole process is cross-platform. For testing I can easily encrypt using crypto js library and decrypt the cipher text generated by it. But my doubt is if this method is going to be compatible with encryption in .net. As I'm new to this, so far what I've learned is that the crypto js library generates a random Integration vector (IV) value. If an IV is generated by the .net code as well, do I need to specify it during decryption in node js as well? Currently I'm just using the simple example given in the documentation and no IV or padding is specified and I'm not sure if any default specifications are used by this library. In short I just need to make sure I'm doing this correctly and the method used for decryption won't cause any issues for the encryption part.

var CryptoJS = require("crypto-js");

var aeskey = "bQeThWmZq4t7w!z%C*F-JaNcRfUjXn2r";
var cloudcreds = {
  accessKeyId: "abcdef",
  accessKeySecret: "zxywvt",
};

//Encryption - Tbd with .net
var encryptedData = CryptoJS.AES.encrypt(
  JSON.stringify(cloudcreds),
  aeskey
).toString();
console.log("Encrypted Cloud Creds =>", encryptedData);

//Decryption - With node js
var bytes = CryptoJS.AES.decrypt(encryptedData, aeskey);
var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
console.log("Decrypted Cloud Creds => ", decryptedData);

With the crypto module I'm now using my own IV and so far it seems like this could work.

var crypto = require("crypto");

var algorithm = "aes-256-cbc"; //algorithm to use
const key = "3zTvzr3p67VC61jmV54rIYu1545x4TlY"; //create key
var data = {
  name: "Catalin",
  surname: "Munteanu",
  address: "Romania",
};
data = JSON.stringify(data);
const iv = "0000000000000000"; // generate different ciphertext everytime

const cipher = crypto.createCipheriv(algorithm, key, iv);
var encrypted = cipher.update(data, "utf8", "base64") + cipher.final("base64"); // encrypted text
console.log(encrypted);

const decipher = crypto.createDecipheriv(algorithm, key, iv);
var decrypted = decipher.update(encrypted, "base64", "utf8") + decipher.final("utf8"); //deciphered text
console.log(JSON.parse(decrypted));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM