I have searched here and googled, but couldn't really find what I am looking for. Looks like this should be pretty easy but few similar threads went unanswered as SO. I am hoping if I get the answer. So I have this fiddle JS Fiddle which encrypts and decrypts in CryptoJS, no problem so far. Code is here:
var options = { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, keySize: 256 };
var encrypted = CryptoJS.AES.encrypt('encrypt me', 'A37u172sSFS9O9JNHs82u38djdncnvyz', options);
var decrypted = CryptoJS.AES.decrypt(encrypted, "A37u172sSFS9O9JNHs82u38djdncnvyz", options);
var plaintext = decrypted.toString(CryptoJS.enc.Utf8);
But when I take the encrypted string, and I try to decrypt, I am unable to. See this node fiddle: Node Fiddle, and code is:
const crypto = require('crypto');
const mykey = crypto.createDecipher('aes-256-cbc', 'A37u172sSFS9O9JNHs82u38djdncnvyz');
//A37u172sSFS9O9JNHs82u38djdncnvyz9
const mystr = mykey.update('U2FsdGVkX18mFQOkolgDJ0cjOfYqiqKCUGdNoA2nESI=', 'base64', 'utf8');
Ideally, the mystr string should return to me the text 'encrypt me', but it shows some gibberish characters. I know it should be easy to find, but I am unable to understand what I am doing wrong. I am totally noob when it comes to encryption. Any help here?
Old post but I just did the opposite of this it should be relevant, posting this as I didnt find any other similar article on here.
Encrypt Node JS Crypto.
const key = crypto.createHash('sha256').update(String(random)).digest();
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
return cipher.update(JSON.stringify(data || {}), 'utf8', 'base64') + cipher.final('base64');
Decrypt in CryptoJS
const key = CryptoJS.SHA256(randomString) // same string as encrypt
const plaintextArray = CryptoJS.AES.decrypt(
{
ciphertext: CryptoJS.enc.Base64.parse(data),
salt: ''
},
key,
{ iv: CryptoJS.enc.Utf8.parse(iv), mode: CryptoJS.mode.CBC }
)
return CryptoJS.enc.Utf8.stringify(plaintextArray)