I found that you can actually import the enc object and use that to stringify/parse strings.
I am not sure about hex, but to decrypt a Utf8 formatted string I ended up doing:
Note: This would only work if cypherString = cipherText.toString() -> because by default the encrypt use Utf8 encoding.
const ciphertext = CryptoJS.AES.encrypt('my message', key);
const cypherString = ciphertext.toString(CryptoJS.format.Hex);
const bytes = CryptoJS.AES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(cypherString)
}, key, {format: CryptoJS.format.Hex});
const plaintext = bytes.toString();
plaintext remains empty
@qiudaomao I cannot make this work... any thoughts?
const ciphertext = CryptoJS.AES.encrypt('my message', key);
const cypherString = ciphertext.toString(CryptoJS.format.Hex);
const bytes = CryptoJS.AES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(cypherString)
}, key, {format: CryptoJS.format.Hex});
const plaintext = bytes.toString();
plaintext remains empty
//here returns an encrypted object contains `ciphertext`
//js is really stupid, you never known method input/output types without read the code...
const cipherResult = CryptoJS.AES.encrypt('my message', key);
const cipherString = CryptoJS.enc.Hex.stringify(cipherResult.ciphertext);
const bytes = CryptoJS.AES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(cypherString)
}, key, {format: CryptoJS.format.Hex});
const plaintext = bytes.toString();
var data = [{id: 1}, {id: 2}]
// Encrypt
var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), key);
const cypherString = ciphertext.toString(CryptoJS.format.Hex);
console.log("cipherText here is",cypherString);
var bytes = CryptoJS.AES.decrypt(ciphertext.toString(), key);
var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
var plaintText = '123';
//encrypt
var encryptedData = CryptoJS.AES.encrypt(plaintText, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
console.log("plaintText:" + plaintText);
console.log("encryptedData :" + encryptedData);
var encryptedDataHexStr = encryptedData.toString(CryptoJS.format.Hex);
console.log("encryptedDataHex:" + encryptedDataHexStr );
//-------------------------------------------------------------------------
//decrypt
var encryptedHex = CryptoJS.enc.Hex.parse(encryptedDataHexStr);
var encryptedBase64 = CryptoJS.enc.Base64.stringify(encryptedHex);
console.log("encryptedHex :" + encryptedHex);
console.log("encryptedBase64 :" + encryptedBase64 );
var decryptedData = CryptoJS.AES.decrypt(encryptedBase64, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
let decryptText = decryptedData.toString(CryptoJS.enc.Utf8);
console.log("decryptText :" + decryptText );
This is my solution, share it with everyone
diegoferhu-91, roysG, santipm29, huongbee, cleverpig, slimvan, code4you2021, phamquyhai, and bozi0822 reacted with thumbs up emoji
huongbee, phamquyhai, and bozi0822 reacted with hooray emoji
diegoferhu-91 and roysG reacted with rocket emoji
yxw007, diegoferhu-91, roysG, and huongbee reacted with eyes emoji
All reactions
@aa4790139 bro, do you see that your identifiers are all inconsistent and confusing.
I'm pretty sure encryptedDataHexStr is the same as encryptedDataHex and encryptedHexStr since the latter were never declared. But it's hard to follow like that.
sorry decrypt edit error, already corrected
This work for me, using enc and not only UTF8
const { AES, enc } = require('crypto-js')
const { decrypt, encrypt } = AES
const message = "Hi my friend"
const key= "key123456"
const aesDecrypt = decrypt(message, key).toString(enc.Utf8)
console.log(aesDecrypt) // Hi my friend
var plaintText = '123';
//encrypt
var encryptedData = CryptoJS.AES.encrypt(plaintText, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
console.log("plaintText:" + plaintText);
console.log("encryptedData :" + encryptedData);
var encryptedDataHexStr = encryptedData.toString(CryptoJS.format.Hex);
console.log("encryptedDataHex:" + encryptedDataHexStr );
//-------------------------------------------------------------------------
//decrypt
var encryptedHex = CryptoJS.enc.Hex.parse(encryptedDataHexStr);
var encryptedBase64 = CryptoJS.enc.Base64.stringify(encryptedHex);
console.log("encryptedHex :" + encryptedHex);
console.log("encryptedBase64 :" + encryptedBase64 );
var decryptedData = CryptoJS.AES.decrypt(encryptedBase64, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
let decryptText = decryptedData.toString(CryptoJS.enc.Utf8);
console.log("decryptText :" + decryptText );
This is my solution, share it with everyone
Working for me, thanks
All the examples above didn't work for me, but this one works:
`const SECRET = 'I am batman'
function enc(plainText){
var b64 = CryptoJS.AES.encrypt(plainText, SECRET).toString();
var e64 = CryptoJS.enc.Base64.parse(b64);
var eHex = e64.toString(CryptoJS.enc.Hex);
return eHex;
function dec(cipherText){
var reb64 = CryptoJS.enc.Hex.parse(cipherText);
var bytes = reb64.toString(CryptoJS.enc.Base64);
var decrypt = CryptoJS.AES.decrypt(bytes, SECRET);
var plain = decrypt.toString(CryptoJS.enc.Utf8);
return plain;
Reference: https://itecnote.com/tecnote/javascript-cipher-a-string-using-crypto-js-with-hex-encoding-to-make-it-url-friendly/
In my case, I need a hex from an encrypted plain text, and the plain text when decrypting that generated hex. eg.:
const plainText = "Hello World";
console.log(enc(plainText));
// > "<the_generated_hex>"
console.log(dec("<the_generated_hex>"));
// > "Hello World"
Hope this is helpful :)