Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.0.0+

Generate RSA Public/Private Key Pair and Export to PEM

See more RSA Examples

_LANGUAGE_ example code showing how to generate an RSA public/private key pair and export to PEM files.

Chilkat React Native Downloads

React Native
import { PrivateKey, PublicKey, Rsa } 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();

  // Generate a 2048-bit key.  Chilkat RSA supports
  // key sizes ranging from 512 bits to 8192 bits.
  const privKey = new PrivateKey();
  try {
    rsa.genKey(2048, privKey);
  } catch {
    console.log(rsa.lastErrorText);
    return;
  }

  const pubKey = new PublicKey();
  privKey.toPublicKey(pubKey);

  // Save the private key in PEM format:
  try {
    privKey.savePemFile('privateKey.pem');
  } catch {
    console.log(privKey.lastErrorText);
    return;
  }

  // Save the public key in PEM format:
  try {
    pubKey.savePemFile(false, 'publicKey.pem');
  } catch {
    console.log(pubKey.lastErrorText);
    return;
  }

  console.log('Success.');
}