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

  // Generate a 2048-bit key.  Chilkat RSA supports
  // key sizes ranging from 512 bits to 8192 bits.
  final privKey = CkPrivateKey();
  try {
    rsa.genKey(2048, privKey);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final pubKey = CkPublicKey();
  privKey.toPublicKey(pubKey);

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

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

  print('Success.');
}