How to decrypt messages with CryptoJS AES and JavaScript?

Sometimes, we want to decrypt messages with CryptoJS AES and JavaScript.

In this article, we’ll look at how to decrypt messages with CryptoJS AES and JavaScript.

How to decrypt messages with CryptoJS AES and JavaScript?

To decrypt messages with CryptoJS AES and JavaScript, we can call the CryptoJS.AES.decrypt method with the encrypted message and the secret key.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>

to add the CryptoJS script.

Then we write:

const key = "secret";
const data = CryptoJS.AES.encrypt("Message", key);
const decrypted = CryptoJS.AES.decrypt(data, key).toString(CryptoJS.enc.Utf8);
console.log(decrypted)

We call CryptoJS.AES.encrypt to encrypt 'Message' with the secret key.

Then we call CryptoJS.AES.decrypt with the encrypted data and the secret key to decrypt data.

Then we convert the decrypted data to a string with toString.

Therefore, decrypted is also 'Message'.

Conclusion

To decrypt messages with CryptoJS AES and JavaScript, we can call the CryptoJS.AES.decrypt method with the encrypted message and the secret key.