Sample code for 30+ languages & platforms
React Native

Load RSA Public Key from JWK Format (JSON Web Key)

See more RSA Examples

Demonstrates how to load an RSA public key from JWK (JSON Web Key) format.

Note: This example requires Chilkat v9.5.0.66 or later.

Chilkat React Native Downloads

React Native
import { JsonObject, PublicKey } from '@chilkat/react-native'

function chilkatExample() {
  // Note: This example requires Chilkat v9.5.0.66 or later.

  // First build a JWK sample to load..
  const json = new JsonObject();
  json.updateString('kty', 'RSA');
  json.updateString('n', '33TqqLR3eeUmDtHS89qF3p4MP7Wfqt2Zjj3lZjLjjCGDvwr9cJNlNDiuKboODgUiT4ZdPWbOiMAfDcDzlOxA04DDnEFGAf-kDQiNSe2ZtqC7bnIc8-KSG_qOGQIVaay4Ucr6ovDkykO5Hxn7OU7sJp9TP9H0JH8zMQA6YzijYH9LsupTerrY3U6zyihVEDXXOv08vBHk50BMFJbE9iwFwnxCsU5-UZUZYw87Uu0n4LPFS9BT8tUIvAfnRXIEWCha3KbFWmdZQZlyrFw0buUEf0YN3_Q0auBkdbDR_ES2PbgKTJdkjc_rEeM0TxvOUf7HuUNOhrtAVEN1D5uuxE1WSw');
  json.updateString('e', 'AQAB');

  // The JSON contains the following:
  // { 
  //   "kty": "RSA",
  //   "n": "33TqqLR3eeUmDtHS89qF3p4MP7Wfqt2Zjj3lZjLjjCGDvwr9cJNlNDiuKboODgUiT4ZdPWbOiMAfDcDzlOxA04DDnEFGAf-kDQiNSe2ZtqC7bnIc8-KSG_qOGQIVaay4Ucr6ovDkykO5Hxn7OU7sJp9TP9H0JH8zMQA6YzijYH9LsupTerrY3U6zyihVEDXXOv08vBHk50BMFJbE9iwFwnxCsU5-UZUZYw87Uu0n4LPFS9BT8tUIvAfnRXIEWCha3KbFWmdZQZlyrFw0buUEf0YN3_Q0auBkdbDR_ES2PbgKTJdkjc_rEeM0TxvOUf7HuUNOhrtAVEN1D5uuxE1WSw",
  //   "e": "AQAB",
  // } 

  // Note: The JSON can contain other members, such as "use", "kid", or anything else.  These will be ignored.
  json.emitCompact = false;

  // Show the JWK string to be loaded:
  const jwkStr = json.emit();

  const pubKey = new PublicKey();
  // The LoadFromString method will automatically detect the format.
  try {
    pubKey.loadFromString(jwkStr);
  } catch {
    console.log(pubKey.lastErrorText);
    return;
  }

  // OK.. the JWK is loaded.  It can be used in whatever way desired...

  // The key can be retrieved in any other format, such as XML or PEM..
  console.log(pubKey.getXml());

  // XML output:
  // <RSAPublicKey>
  //     <Modulus>33TqqLR3eeUmDtHS89qF3p4MP7Wfqt2Zjj3lZjLjjCGDvwr9cJNlNDiuKboODgUiT4ZdPWbOiMAfDcDzlOxA04DDnEFGAf+kDQiNSe2ZtqC7bnIc8+KSG/qOGQIVaay4Ucr6ovDkykO5Hxn7OU7sJp9TP9H0JH8zMQA6YzijYH9LsupTerrY3U6zyihVEDXXOv08vBHk50BMFJbE9iwFwnxCsU5+UZUZYw87Uu0n4LPFS9BT8tUIvAfnRXIEWCha3KbFWmdZQZlyrFw0buUEf0YN3/Q0auBkdbDR/ES2PbgKTJdkjc/rEeM0TxvOUf7HuUNOhrtAVEN1D5uuxE1WSw==</Modulus>
  //     <Exponent>AQAB</Exponent>
  // </RSAPublicKey>

  // Choose PCKS1 or PCKS8 PEM format..
  const bPreferPkcs1 = false;
  console.log(pubKey.getPem(bPreferPkcs1));

  // PEM output
  // -----BEGIN PUBLIC KEY-----
  // MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA33TqqLR3eeUmDtHS89qF
  // 3p4MP7Wfqt2Zjj3lZjLjjCGDvwr9cJNlNDiuKboODgUiT4ZdPWbOiMAfDcDzlOxA
  // 04DDnEFGAf+kDQiNSe2ZtqC7bnIc8+KSG/qOGQIVaay4Ucr6ovDkykO5Hxn7OU7s
  // Jp9TP9H0JH8zMQA6YzijYH9LsupTerrY3U6zyihVEDXXOv08vBHk50BMFJbE9iwF
  // wnxCsU5+UZUZYw87Uu0n4LPFS9BT8tUIvAfnRXIEWCha3KbFWmdZQZlyrFw0buUE
  // f0YN3/Q0auBkdbDR/ES2PbgKTJdkjc/rEeM0TxvOUf7HuUNOhrtAVEN1D5uuxE1W
  // SwIDAQAB
  // -----END PUBLIC KEY-----
}