Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which rsa library recommended for "react native" platform?

Which rsa library recommended for "react native" platform with android support?

Thank you

EDIT:

I tried react-native-rsa:

var encrypted = '';
try {
  var RSAKey = require('react-native-rsa');
  const bits = 1024;
  const exponent = '10001';
  var rsa = new RSAKey();
  rsa.generate(bits, exponent);
  var publicKey = rsa.getPublicString(); 
  var privateKey = rsa.getPrivateString(); 
} catch (error) {
  console.log(1);
  console.log(error);
}

try {

  rsa = new RSAKey();
  rsa.setPrivateString(privateKey);
  var originText = 'Test 123 Test 321';
  encrypted = rsa.encrypt(originText);

} catch (error) {
  console.log(2);
  console.log(error);
}

try {
  rsa = new RSAKey();
  rsa.setPublicString(publicKey);
  var decrypted = rsa.decrypt(encrypted);
  console.log(decrypted);

} catch (error) {
  console.log(3);
  console.log(error);
}

But I got this error:

I/ReactNativeJS( 9238): 3                                                                                                                                                                                  
I/ReactNativeJS( 9238): { [TypeError: null is not an object (evaluating 'e.bitLength')]                                                                                                                    
I/ReactNativeJS( 9238):   line: 96781,                                                                                                                                                                     
I/ReactNativeJS( 9238):   column: 8,                                                                                                                                                                       
I/ReactNativeJS( 9238):   sourceURL: 'http://10.0.3.2:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false' }  
like image 734
DinDan Avatar asked Oct 05 '16 16:10

DinDan


People also ask

Does Crypto JS support RSA?

No. CryptoJS is a JavaScript library for symmetric, but not asymmetric encryption, i.e. it does not support RSA.


1 Answers

Try react-native-rsa, it's simple to use and will support android platform.

First install library using Npm:

npm install react-native-rsa

Generate RSA keys:

var RSAKey = require('react-native-rsa');
const bits = 1024;
const exponent = '10001'; // must be a string
var rsa = new RSAKey();
var r = rsa.generate(bits, exponent);
var publicKey = rsa.RSAGetPublicString(); // return json encoded string
var privateKey = rsa.RSAGetPrivateString(); // return json encoded string

Encript:

var rsa = new RSAKey();
rsa.setPublicString(publicKey);
var originText = 'sample String Value';
var encrypted = rsa.encrypt(originText);

Decript:

rsa.setPrivateString(privateKey);
var decrypted = rsa.decrypt(encrypted); // decrypted == originText

Reference: https://www.npmjs.com/package/react-native-rsa

like image 54
leo7r Avatar answered Sep 16 '22 14:09

leo7r