Sample code for 30+ languages & platforms
Dart 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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final rsa = CkRsa();

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

  try {
    pubkey.loadFromString(publicKeyPem);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    rsa.usePublicKey(pubkey);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Demonstrate importing a PEM private key:
  final privateKeyPem = 'PEM private-key data goes here';
  final privkey = CkPrivateKey();

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

  try {
    rsa.usePrivateKey(privkey);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('OK!');
}