Home >Backend Development >PHP Tutorial >How to Decrypt Encrypted Data Using CryptoJS in JavaScript?

How to Decrypt Encrypted Data Using CryptoJS in JavaScript?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-30 01:53:10498browse

How to Decrypt Encrypted Data Using CryptoJS in JavaScript?

Decrypt Encrypted Data Using CryptoJS in JavaScript

In this scenario, you're using PHP for encryption and CryptoJS for decryption. To retrieve the decrypted data in CryptoJS, you need to decode the base64-encoded encrypted string.

Here's a revised version of the JavaScript code:

var key = 'encryptionkey';
var encrypted = `<?php echo $msgBase64 ?>`;
var base64Decoded = CryptoJS.enc.Base64.parse(encrypted);
var decrypted = CryptoJS.AES.decrypt(base64Decoded, key);
console.log( decrypted.toString(CryptoJS.enc.Utf8) );

This code first decodes the base64-encoded encrypted string using CryptoJS.enc.Base64.parse. Then, it decrypts the decoded string using the defined key. Finally, it converts the decrypted result to a UTF-8 string and outputs it to the console.

The above is the detailed content of How to Decrypt Encrypted Data Using CryptoJS in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn