哪个rsa库推荐用于“反应原生”平台?

时间:2016-10-05 16:06:55

标签: javascript node.js react-native

哪个rsa库推荐用于“反应原生”平台和android支持?

谢谢

修改

我尝试了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);
}

但我收到了这个错误:

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' }  

2 个答案:

答案 0 :(得分:0)

试试<div resize id="view"> <h1>Resize this</h1> </div> ,它使用起来很简单,并且支持Android平台。

首先使用Npm安装库:

react-native-rsa

生成RSA密钥:

npm install react-native-rsa

Encript:

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

Decript:

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

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

答案 1 :(得分:0)

使用此软件包,因为它是更新版本:react-native-rsa-native