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' }
No. CryptoJS is a JavaScript library for symmetric, but not asymmetric encryption, i.e. it does not support RSA.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With