react-native-crypto-js icon indicating copy to clipboard operation
react-native-crypto-js copied to clipboard

Error: Malformed UTF-8 data

Open vadhack opened this issue 5 years ago • 15 comments

I used same code for aes encrypt (in Readme file) for my react-native android app and server,

let data = [{id: 1}, {id: 2}] // Encrypt in react-native android app let ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), 'secret key 123').toString();

// Decrypt in the nodejs server let bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123'); let decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));

when my react-native app is in "JS Dev Mode" activated and "JS Minify" desactivated the decryption in the server works fine, but when i configure my app as "JS Dev Mode" desactivated and "JS Minify" activated i cant decrypt the data in the server,

Error: Malformed UTF-8 data at Object.stringify

the error throw in the server only when the app is in production mode, the error happend exactly when execute: bytes.toString(CryptoJS.enc.Utf8)

Can someone explain to me what I am doing wrong or how to solve the problem? please

Please help me, thanks

vadhack avatar Jun 12 '19 06:06 vadhack

i have this problem too !

amomendoost avatar Jun 17 '19 21:06 amomendoost

It was working with 0.54. But when upgrading to react-native 0.59.10, I also encounter the same issue.

It seems that the cause is because react-native is trying to minify an already minified file within this package.

I had to replace this package with https://www.npmjs.com/package/crypto-js.

rchavik avatar Jul 12 '19 05:07 rchavik

I think, when your app is on debug version or release version or a different react-native version, it encrypt in different ways. I tested this and with same string and same secret key in debug version, i've had a string while in release version i've had a different string

KhoumaTAB avatar Nov 12 '19 10:11 KhoumaTAB

Hello! I can confirm that AES.encrypt/decrypt and MD5 return different outputs on the Expo Client application and published APK/IPA. I noticed this in my secure messaging app while testing with two different phones - the other running debug Expo Client version and the other published APK. The AES encrypted data does not decrypt if created on a different app stage (debug/production) eventhough using the same key.

orteidni avatar Mar 17 '20 07:03 orteidni

same problem. how to solve this problem

JP6720 avatar Apr 03 '20 05:04 JP6720

same problem. how to solve this problem

https://github.com/imchintan/react-native-crypto-js/issues/5#issuecomment-510747872

rchavik avatar Apr 03 '20 06:04 rchavik

same problem. how to solve this problem

#5 (comment)

I'm using Expo so do I have to eject to use this?

orteidni avatar Apr 03 '20 06:04 orteidni

I don't know about expo.

I simply removed react-native-crypto-js": "^1.0.0" from package.json and replace it with "crypto-js": "^3.1.9-1",. Run npm install to download the package.

Then change all existing occurences of: import CryptoJS from 'react-native-crypto-js';

to

var CryptoJS = require('crypto-js');

rchavik avatar Apr 03 '20 07:04 rchavik

@rchaviknis it working in release build . Crypto-js

JP6720 avatar Apr 03 '20 07:04 JP6720

I don't know about expo.

I simply removed react-native-crypto-js": "^1.0.0" from package.json and replace it with "crypto-js": "^3.1.9-1",. Run npm install to download the package.

Then change all existing occurences of: import CryptoJS from 'react-native-crypto-js';

to

var CryptoJS = require('crypto-js');

Is this working in realase build for react native.

JP6720 avatar Apr 03 '20 07:04 JP6720

yes. but i'm on 0.59. not sure about recent react-native, but should be the same.

rchavik avatar Apr 03 '20 08:04 rchavik

yes. but i'm on 0.59. not sure about recent react-native, but should be the same.

Yes. This solution works.thanks @rchavik

JP6720 avatar Apr 03 '20 09:04 JP6720

Working in react-native 0.59.1 also.

JP6720 avatar Apr 03 '20 09:04 JP6720

Not working with 0.59.5. What to do?

seema-spark6 avatar Jun 24 '20 06:06 seema-spark6

It was working with 0.54. But when upgrading to react-native 0.59.10, I also encounter the same issue.

It seems that the cause is because react-native is trying to minify an already minified file within this package.

I had to replace this package with https://www.npmjs.com/package/crypto-js.

How you installed crypto-js in react native? I would need to do extra configurations?

I have a RN app with 0.71.8

gasparnd avatar Oct 11 '23 17:10 gasparnd