React Native Requires Chilkat v11.0.0+
React Native
RSA Encrypt with Modulus and Exponent
See more RSA Examples
Demonstrates how to RSA encrypt with a given modulus and exponent.Chilkat React Native Downloads
import { PublicKey, Rsa, Xml } from '@chilkat/react-native'
function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rsa = new Rsa();
// Assuming you already have a base64 modulus and exponent,
// wrap it in XML like this:
const modulus = 'qMBRpdYrAy5aMmo31NErUizh5sbweguSmh4wlK6uJEIDl+kwTlROnE34KOFExeTbJSX0WygPi+vWl0yNq7buIMUKpytossAAWut5khO3CQJxTk7G2gnEPNUUXHiExGgNrLzcSLv8YIlfVALhoRWyC67KOL+a+3taNq3h+BHeWhM=';
const exponent = 'AQAB';
const xml = new Xml();
xml.tag = 'RSAPublicKey';
xml.newChild2('Modulus', modulus);
xml.newChild2('Exponent', exponent);
const pubKey = new PublicKey();
try {
pubKey.loadFromString(xml.getXml());
} catch {
console.log(pubKey.lastErrorText);
return;
}
try {
rsa.usePublicKey(pubKey);
} catch {
console.log(rsa.lastErrorText);
return;
}
const usePrivateKey = false;
const plainText = 'message in a bottle';
rsa.encodingMode = 'base64';
const encryptedStrBase64 = rsa.encryptStringENC(plainText, usePrivateKey);
console.log(encryptedStrBase64);
}