Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Encrypt data in React native (Using Expo)

I'm trying to simply encrypt a message using a given key and iV. I've tried several libraries to achieve this but Expo isn't compatible with any of them. I couldn't find any encryption libraries for Expo (That support AES). I guess my question is : How do I encrypt data in React Native running Expo

Ps : I am not interested in expo-crypto

like image 337
0xD1x0n Avatar asked Mar 18 '20 05:03

0xD1x0n


People also ask

Is crypto available in react-native?

the crypto in this boxThe goal of this module is to reimplement node's crypto module so that it can run in react-native supported environments. Here is the subset that is currently implemented: createHash (sha1, sha224, sha256, sha384, sha512, md5, rmd160) createHmac (sha1, sha224, sha256, sha384, sha512, md5, rmd160)

How JSON encrypt data in react JS?

To encrypt and decrypt data, simply use encrypt() and decrypt() function from an instance of crypto-js. var bytes = CryptoJS. AES. decrypt(ciphertext, 'my-secret-key@123');


3 Answers

Another possibility (what I did) is to use the CryptoES library.

https://www.npmjs.com/package/crypto-es

After long search I found it, it is a continued development of the 3.1 version of the CryptoJS library and can be used with Expo.

like image 188
amirzolal Avatar answered Oct 24 '22 07:10

amirzolal


npm i --save crypto-es crypto-js 
import CryptoES from "crypto-es";

Then you should encrypt the text, for example:

var mytexttoEncryption = "Hello" 
const encrypted = CryptoES.AES.encrypt(mytexttoEncryption ,"your password").toString();

var C = require("crypto-js");

var Decrypted = C.AES.decrypt(E, "your password");
var result =Decrypted.toString(C.enc.Utf8);

console.log(result)
like image 29
Ahmed Raza Avatar answered Oct 24 '22 08:10

Ahmed Raza


Use [email protected] , 3.1.x version use Math.random() and doesn't require node "crypto" package. It's not as safe as the latest version but works for me.

yarn add [email protected] 

I only use it for decrypting. If you really need it for some security requirements I suggest you encrypt it in server node enviroment.

like image 3
user1421427 Avatar answered Oct 24 '22 08:10

user1421427