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

Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

See more OpenSSL Examples

How to create a PKCS12 (.p12 or .pfx) from a certificate file and private key file: Demonstrates how to duplicate this OpenSSL command:
Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

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.PrivateKey,
  Chilkat.CertChain,
  Chilkat.Cert;

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

procedure RunDemo;
var
  success: Boolean;
  pkey: TPrivateKey;
  cert: TCert;
  certChain: TCertChain;
  pfx: TPfx;
  password: string;

begin
  success := False;

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

  pkey := TPrivateKey.Create;

  //  Load the private key from the file.
  success := pkey.LoadAnyFormatFile('certFile.key','');
  if (success <> True) then
    begin
      WriteLn(pkey.LastErrorText);
      Exit;
    end;

  cert := TCert.Create;
  //  The LoadFromFile method auto-recognizes the format...
  success := cert.LoadFromFile('certfile.cer');
  if (success <> True) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  We'll need a cert chain object to create the PKCS12, so get it
  //  from the cert.  

  certChain := cert.GetCertChain();
  if (not cert.LastMethodSuccess) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  Create the PFX object, add the cert and private key, and write to a .pfx file.
  pfx := TPfx.Create;

  //  The cert(s) are automatically added in the call to AddPrivateKey
  success := pfx.AddPrivateKey(pkey,certChain);
  if (success <> True) then
    begin
      WriteLn(pfx.LastErrorText);
      Exit;
    end;

  //  Write the .pfx to a file.
  password := 'myPassword';
  success := pfx.ToFile(password,'certfile.pfx');
  if (success <> True) then
    begin
      WriteLn(pfx.LastErrorText);
      Exit;
    end;

  WriteLn('Success.');


  pkey.Free;
  cert.Free;
  pfx.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.