brix / crypto-js

JavaScript library of crypto standards.
Other
15.87k stars 2.39k forks source link

core.js:523 Uncaught Error: Malformed UTF-8 data #454

Open mdahsanekram opened 1 year ago

mdahsanekram commented 1 year ago

I have written this function in javaScript to decrypt string in javaScript and encript it in NodeJs and vise versa.

export const decrypt = (args) => {   let plaintext = '';   if (args) {     const ciphertext = decodeURIComponent(args); // Remove decodeURIComponent if not URL-encoded     const bytes = CryptoJS.AES.decrypt(ciphertext, process.env.REACT_APP_SECRETKEY);     // Check if decryption was successful     if (bytes.sigBytes > 0) {       plaintext = bytes.toString(CryptoJS.enc.Utf8);     } else {       // Handle decryption failure, e.g., throw an error or return an appropriate value       // console.error('Decryption failed');     }   }   return plaintext; };

and getting errors : core.js:523 Uncaught Error: Malformed UTF-8 data     at Object.stringify (core.js:523:1)     at WordArray.init.toString (core.js:278:1)     at decrypt (encodingdecoding.js:21:1)     at NewLoyaltyCampaign (NewLoyaltyCampaign.js:152:1)     at renderWithHooks (react-dom.development.js:16305:1)     at updateFunctionComponent (react-dom.development.js:19588:1)     at beginWork (react-dom.development.js:21601:1)     at beginWork$1 (react-dom.development.js:27426:1)     at performUnitOfWork (react-dom.development.js:26557:1)     at workLoopSync (react-dom.development.js:26466:1)

mdahsanekram commented 1 year ago

Encrypt ReactJs export const encrypt = (args) => { try { if (args) { const ciphertext = CryptoJS.AES.encrypt(args, process.env.REACT_APP_SECRETKEY).toString(); return encodeURIComponent(ciphertext); } return null; } catch (error) { // console.error('Encryption error:', error); return null; } };

vohoangankhanh commented 8 months ago

this may help https://copyprogramming.com/howto/why-i-get-malformed-utf-8-data-error-on-crypto-js#google_vignette