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

RSA Encrypt and OpenSSL Decrypt

See more OpenSSL Examples

Demonstrates how to use Chilkat to RSA encrypt, and then use OpenSSL to decrypt.

Chilkat React Native Downloads

React Native
import { BinData, PrivateKey, PublicKey, Rsa } from '@chilkat/react-native'

function chilkatExample() {
  // This requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const rsa = new Rsa();

  const privKey = new PrivateKey();
  rsa.genKey(2048, privKey);
  privKey.savePkcs8PemFile('qa_output/privKey.pem');

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

  rsa.encodingMode = 'base64';
  const plainText = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890';
  const bUsePrivateKey = false;
  rsa.usePublicKey(pubKey);
  const encryptedStr = rsa.encryptStringENC(plainText, bUsePrivateKey);

  const bd = new BinData();
  bd.appendEncoded(encryptedStr, 'base64');
  bd.writeFile('qa_output/enc.dat');

  // The OpenSSL command to decrypt is:
  // openssl pkeyutl -in enc.dat -inkey privKey.pem -keyform PEM -pkeyopt rsa_padding_mode:pkcs1 -decrypt

  console.log('OK');
}