Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
pfx: HCkPfx;
sbPem: HCkStringBuilder;
bLoaded: Boolean;
pemText: PWideChar;
password: PWideChar;

begin
success := False;

pfx := CkPfx_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.
//  Load the PEM text (certificates and private keys) into a string.
sbPem := CkStringBuilder_Create();
bLoaded := CkStringBuilder_LoadFile(sbPem,'qa_data/identity.pem');
if (bLoaded <> True) then
  begin
    Memo1.Lines.Add('Failed to load the PEM file.');
    Exit;
  end;
pemText := CkStringBuilder__getAsString(sbPem);
if (CkStringBuilder_getLastMethodSuccess(sbPem) = False) then
  begin
    Memo1.Lines.Add(CkStringBuilder__lastErrorText(sbPem));
    Exit;
  end;

//  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.
password := 'myPemPassword';
success := CkPfx_LoadPem(pfx,pemText,password);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPfx__lastErrorText(pfx));
    Exit;
  end;
Memo1.Lines.Add('Loaded ' + IntToStr(CkPfx_getNumPrivateKeys(pfx)) + ' private key(s).');

CkPfx_Dispose(pfx);
CkStringBuilder_Dispose(sbPem);