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

Load PEM Public/Private Key into RSA Object

See more RSA Examples

Demonstrates how to load a PEM key into the Chilkat RSA object.

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();

  // First demonstrate importing a PEM public key:
  const publicKeyPem = 'PEM public-key data goes here';
  const pubkey = new PublicKey();

  try {
    pubkey.loadFromString(publicKeyPem);
  } catch {
    console.log(pubkey.lastErrorText);
    return;
  }

  try {
    rsa.usePublicKey(pubkey);
  } catch {
    console.log(rsa.lastErrorText);
    return;
  }

  // Demonstrate importing a PEM private key:
  const privateKeyPem = 'PEM private-key data goes here';
  const privkey = new PrivateKey();

  // If the private key PEM is protected with a password, then 
  // call LoadEncryptedPem.  Otherwise call LoadPem.
  try {
    privkey.loadPem(privateKeyPem);
  } catch {
    console.log(privkey.lastErrorText);
    return;
  }

  try {
    rsa.usePrivateKey(privkey);
  } catch {
    console.log(rsa.lastErrorText);
    return;
  }

  console.log('OK!');
}