Sample code for 30+ languages & platforms
Dart

Load Identities from PEM Text

See more PFX/P12 Examples

Demonstrates Pfx.LoadPem, which loads one or more certificate/private-key identities from PEM-formatted text and builds the contents of the Pfx object.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The password decrypts any encrypted private keys in the PEM. This builds a PFX in memory from PEM material, which can then be serialized to PKCS#12.

Chilkat Dart Downloads

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

void main() {
  final pfx = CkPfx();

  // The file path is relative to the application's current working directory.  An absolute path
  // may also be used.  Supply the path appropriate to your own environment.
  // Load the PEM text (certificates and private keys) into a string.
  final sbPem = CkStringBuilder();
  try {
    sbPem.loadFile('qa_data/identity.pem', 'utf-8');
  } on ChilkatException {
    print('Failed to load the PEM file.');
    return;
  }

  final String pemText;
  try {
    pemText = sbPem.getAsString();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // The password decrypts any encrypted private keys in the PEM (use an empty string if none are
  // encrypted).  A password should come from a secure source rather than being hard-coded.
  final password = 'myPemPassword';
  try {
    pfx.loadPem(pemText, password);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Loaded ${pfx.numPrivateKeys} private key(s).');
}