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

Load PKCS12 / PFX and Access Contents

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and iterates over the contents which include private keys and certificates.

Chilkat Dart Downloads

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

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

  // Load the PKCS12 from a file
  try {
    pfx.loadPfxFile('/someDir/my.p12', 'pfxFilePassword');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final numPrivateKeys = pfx.numPrivateKeys;

  final privKey = CkPrivateKey();

  print('Private Keys:');

  var i = 0;
  while (i < numPrivateKeys) {
    pfx.privateKeyAt(i, privKey);

    // Do something with the private key ...

    i++;
  }

  final cert = CkCert();

  final numCerts = pfx.numCerts;

  print('Certs:');
  i = 0;
  while (i < numCerts) {
    pfx.certAt(i, cert);
    print(cert.subjectDN);

    // If the certificate has a private key (one of the private keys within the PFX)
    // then it can also be obtained via the certificate object:
    if (cert.hasPrivateKey()) {

      print('Has private key!');

      cert.getPrivateKey(privKey);
      // ...
    }

    i++;
  }
}