var CryptoJS = require("crypto-js");
var data = [{id: 1}, {id: 2}]
var secretKey = "12345678901234567890";
var initVector = "12345678901234567890";
// Convert the plain text, key, and IV to WordArrays
let encodedText = CryptoJS.enc.Utf8.parse(JSON.stringify(data));
let encodedKey = CryptoJS.enc.Utf8.parse(secretKey);
let encodedIV = CryptoJS.enc.Utf8.parse(initVector).toString(CryptoJS.enc.Hex).substring(0, 32);
// Create the options object for encryption
let options = {
iv: CryptoJS.lib.WordArray.create([
parseInt(encodedIV.substring(0, 8), 16),
parseInt(encodedIV.substring(8, 16), 16),
parseInt(encodedIV.substring(16, 24), 16),
parseInt(encodedIV.substring(24, 32), 16)
]),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
};
// Encrypt the data using AES encryption with PKCS5Padding and an IV
let encryptedData = CryptoJS.AES.encrypt(
encodedText,
encodedKey,
options
);
var ciphertext = encryptedData.toString();
console.log(ciphertext);
let decryptedData = CryptoJS.AES.decrypt(
ciphertext,
encodedKey,
options
);
// Convert the decrypted data to a Utf8-encoded string
let decryptedText = CryptoJS.enc.Utf8.stringify(decryptedData);
console.log("Decrypted data: " + decryptedText);