0

I want to decode a BASE64 to String with ciphertext, key, and iv using Crypto-JS. I get the error Malformed UTF-8 data.

My code:

const CryptoJS = require('crypto-js')
const fs = require('fs');

// BASE64
const str = fs.readFileSync('file.txt','utf8')

const genRanHex = function(_0x8d0344) {
  for (var _0x15f7b3 = [], _0x2491b5 = '0123456789abcdef', _0x2b0a63 = 0x0; _0x2b0a63 < _0x8d0344; _0x2b0a63++) _0x15f7b3['push'](_0x2491b5['charAt'](Math['floor'](Math['random']() * _0x2491b5['length'])));
  return _0x15f7b3['join']('');
}

var KEYS = genRanHex(0x10) + '3c07f4b0efef700a',
ciphertext = str['substring'](0x18),
key = CryptoJS['enc']['Hex']['parse'](KEYS),
iv = CryptoJS.enc.Base64.parse(str.substring(0x0, 0x18))
try {
  const decrypted = CryptoJS.AES.decrypt(ciphertext, key, {iv }).toString(CryptoJS.enc.Utf8);
  var originalText = JSON.parse(decrypted)
  console.log(originalText)
} catch (err) {
    console.error(err)
}
Christian
  • 4,902
  • 4
  • 24
  • 42
Zaito
  • 11
  • Does this answer your question? [Why I get Malformed UTF-8 data error on crypto-js?](https://stackoverflow.com/questions/58111929/why-i-get-malformed-utf-8-data-error-on-crypto-js) – Christian Jul 09 '22 at 22:22
  • 2
    no i have seen and he hasn't solved my problem – Zaito Jul 09 '22 at 22:45

0 Answers0