10

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

2
  • can't you use pure js libs like crypto-js? Commented Mar 18, 2020 at 9:00
  • 3
    @LonelyCpp Not with expo. It produces the following error : The package at "node_modules/crypto-js/core.js" attempted to import the Node standard library module "crypto". It failed because React Native does not include the Node standard library. Read more at docs.expo.io/versions/latest/introduction/faq/… Failed building JavaScript bundle. Commented Mar 18, 2020 at 13:57

5 Answers 5

15

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Do you have any ideas for using AES-GCM? GCM Mode is not supported in crypto-es.
6
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)

Comments

2

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.

1 Comment

It should be noted that this has a SEVERE security vulnerability and is suseptable to cipher attacks. npm audit should show this.
2

I decided to use jshashes for my React-native & Expo project. The goal of this module is to reimplement hash node's crypto functions in pure javascript without dependency on node:

yarn add jshashes

Comments

0

first use this command :

npm i crypto-es 

then now you should import it with this command :

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();

now for decryption : install the package of crypto-js with this command :

npm i crypto-js 

then lets decrypt it

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

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

console.log(result)

so use this it will be ok

2 Comments

expo-crypto documentation is not good , but you can use crypto-es its nice and you can decrypt it easily with some library like crypto-js and etc
Do you know if they will support aes gcm mode soon?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.