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

Convert PKCS12 / PFX to Java Keystore (JKS)

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and saves it to a Java keystore (JKS) file.

Chilkat Dart Downloads

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

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

  final pfx = CkPfx();

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

  final jksPassword = 'myJksPassword';
  final alias = 'firstPrivateKeyAlias';

  final jks = CkJavaKeyStore();

  // Convert to a Java keystore object.
  // The jksPassword is the password to be used for the JKS private key entries. 
  // It may be the same as the PFX password, but can also be different if desired.
  try {
    pfx.toJksObj(alias, jksPassword, jks);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Save the Java keystore to a file.
  try {
    jks.toFile(jksPassword, '/myKeystores/my.jks');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);

    return;
  }

  print('Successfully converted PFX to JKS.');
}