推荐哪个 rsa 库用于“react native”平台?[关闭]
2016-10-05
13094
推荐哪个 rsa 库用于支持 android 的“react native”平台?
谢谢
编辑:
我尝试了
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' }
3个回答
尝试一下
react-native-rsa
,它使用起来很简单,并且支持 Android 平台。
首先使用 Npm 安装库:
npm install react-native-rsa
生成 RSA 密钥:
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
加密:
var rsa = new RSAKey();
rsa.setPublicString(publicKey);
var originText = 'sample String Value';
var encrypted = rsa.encrypt(originText);
解密:
rsa.setPrivateString(privateKey);
var decrypted = rsa.decrypt(encrypted); // decrypted == originText
leo7r
2016-10-05
按更新方式使用此包: react-native-rsa-native
Muhammad
2020-11-17
尝试一下 https://github.com/jerson/rsa-mobile react-native-fast-rsa 和 flutter-rsa 中使用的原生代码
kimo_ouz
2021-09-24