Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Pfx,
  Chilkat.JavaKeyStore;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  pfx: TPfx;
  jksPassword: string;
  alias: string;
  jks: TJavaKeyStore;

begin
  success := False;

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

  pfx := TPfx.Create;

  //  Load the PKCS12 from a file
  success := pfx.LoadPfxFile('/someDir/my.p12','myPfxPassword');
  if (success = False) then
    begin
      WriteLn(pfx.LastErrorText);
      Exit;
    end;

  jksPassword := 'myJksPassword';
  alias := 'firstPrivateKeyAlias';

  jks := TJavaKeyStore.Create;

  //  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.
  success := pfx.ToJksObj(alias,jksPassword,jks);
  if (success = False) then
    begin
      WriteLn(pfx.LastErrorText);
      Exit;
    end;

  //  Save the Java keystore to a file.
  success := jks.ToFile(jksPassword,'/myKeystores/my.jks');
  if (success <> True) then
    begin
      WriteLn(jks.LastErrorText);
      jks.Free;
      Exit;
    end;

  WriteLn('Successfully converted PFX to JKS.');


  pfx.Free;
  jks.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.