I am using CryptoJS to encrypt the password, here is the example code I am using
var keySize = 256;
var ivSize = 128;
var iterations = 100;
var message = "Hello World";
var password = "SecretPassword";
function encrypt (msg, pass) {
var salt = CryptoJS.lib.WordArray.random(128 / 8);
var salt_hex = CryptoJS.enc.Hex.stringify(salt);
console.log("Salt: "+salt);
console.log("Salt_hex: ", salt_hex.toUpperCase());
var key = CryptoJS.PBKDF2(pass, salt, {
keySize: keySize / 32,
iterations: iterations
});
console.log("Key: "+ key);
var iv = CryptoJS.lib.WordArray.random(128/8);
console.log("IV:"+iv);
var encrypted = CryptoJS.AES.encrypt(msg, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
console.log("Enc: ", encrypted.toString());
// salt, iv will be hex 32 in length
// append them to the ciphertext for use in decryption
var transitmessage = salt.toString()+ iv.toString() + encrypted.toString();
return transitmessage;
}
when the function is executed it is providing the data as follows
Salt: 923ca1ab6dc88806b05f012c5f7df49f script.js:13:11 Salt_hex: 923CA1AB6DC88806B05F012C5F7DF49F script.js:14:11 Key: 1438927d9bc8a42b9689a93c0fefc8506a5be0819feb2adb03c7b755c92cfd9b script.js:25:11 IV:d739119c45742e4f21eb8c987184b5c9 script.js:27:11 Enc: WUBXNygIgehq90li8p08nw==
We have to decrypt the generated Encrypted string on the server side using OpenSSL. So we are doing as follows We are copying the encrypted string to "WUBXNygIgehq90li8p08nw==" cryptenc.txt.enc file and executing the openssl command as follows.
openssl aes-256-cbc -d -pbkdf2 -S 923ca1ab6dc88806b05f012c5f7df49f -iter 100 -k SecretPassword -in cryptenc.txt.enc -out cryptencout.txt
The string is not decrypted and getting below error "bad magic number".
Please help me to know what mistake we are doing and what changes to do to make it work