Daniel Kobe
Daniel Kobe

Reputation: 9825

Decrypt message with encrypted salt string and secret passphrase using CryptoJS

I want decode an encrypted string using cryptoJS. I get how to decode the encrypted object but couldnt understand how to decrypt the string.

Heres what I tried:

var enc = CryptoJS.AES.encrypt('hellloooo', 'secretpassphrase');
console.log('encrypted', enc.salt.toString());
console.log('decrypted', CryptoJS.AES.decrypt(CryptoJS.enc.salt.parse(enc.salt.toString()), 'secretpassphrase').toString(CryptoJS.enc.Utf8));

Upvotes: 0

Views: 3541

Answers (1)

Artjom B.
Artjom B.

Reputation: 61942

The salt is some random value that is randomly generated during encryption in order to derive the actual key and IV from the given password. It doesn't hold the secret, so trying to decrypt it won't give you anything useful.

Here are two ways to decrypt the ciphertext

CryptoJS.AES.decrypt(enc, 'secretpassphrase').toString(CryptoJS.enc.Utf8);
CryptoJS.AES.decrypt(enc.toString(), 'secretpassphrase').toString(CryptoJS.enc.Utf8);

The salt is still present in the enc object, so the decrypt() function can use it to recreate the key and IV to decrypt the string.

Upvotes: 1

Related Questions