Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Convert a PFX to a Java KeyStore
See more PFX/P12 Examples
Demonstrates Pfx.ToJksObj, which populates a JavaKeyStore with a JKS representation of the PFX, then writes it to a file.
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. One private-key entry is created for each private key, with certificate chains built from the PFX certificates. The password protects the generated Java KeyStore.
Chilkat Pascal (Lazarus/Delphi) Downloads
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;
password: string;
jksPassword: string;
jks: TJavaKeyStore;
begin
success := False;
pfx := TPfx.Create;
// 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.
// The PFX password should come from a secure source rather than being hard-coded.
password := 'myPfxPassword';
success := pfx.LoadPfxFile('qa_data/identity.pfx',password);
if (success = False) then
begin
WriteLn(pfx.LastErrorText);
Exit;
end;
// Populate a Java KeyStore (JKS) representation of this PFX. One private-key entry is created for
// each private key, and the PFX certificates are used to build the certificate chains. The 1st
// argument is the alias for the first private-key entry, and the 2nd protects the generated JKS.
// The JKS password should come from a secure source rather than being hard-coded.
jksPassword := 'myJksPassword';
jks := TJavaKeyStore.Create;
success := pfx.ToJksObj('myalias',jksPassword,jks);
if (success = False) then
begin
WriteLn(pfx.LastErrorText);
Exit;
end;
// The Java KeyStore object can then be written to a .jks file.
success := jks.ToFile(jksPassword,'qa_output/identity.jks');
if (success = False) then
begin
WriteLn(jks.LastErrorText);
Exit;
end;
WriteLn('Wrote a Java KeyStore with ' + jks.NumPrivateKeys + ' private key(s).');
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.