How to Encrypt data in React native (Using Expo)

后端 未结 4 1441
梦如初夏
梦如初夏 2021-01-22 19:53

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

相关标签:
4条回答
  • 2021-01-22 20:42

    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
    
    0 讨论(0)
  • 2021-01-22 20:51

    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.

    0 讨论(0)
  • 2021-01-22 20:54

    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

    0 讨论(0)
  • 2021-01-22 20:56

    Use crypto-js@3.1 , 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 crypto-js@3.1 
    

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

    0 讨论(0)
提交回复
热议问题