Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Export a Private Key as DER into a BinData
See more Private Key Examples
Demonstrates the Chilkat PrivateKey.GetPkcsBd method, which exports the key as DER into a BinData. The first argument selects PKCS #1 (true) versus PKCS #8 (false), the second is the password (empty for unencrypted), and the third is the BinData that receives the bytes.
Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: This is the in-memory binary export — the raw DER bytes land in a
BinData ready to hash, encrypt, or hand to another API without a temporary file. One method covers several cases: the boolean picks the traditional (PKCS #1) versus modern (PKCS #8) structure, and a nonempty password produces an encrypted PKCS #8 export (with the cipher from Pkcs8EncryptAlg). Source any password securely.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.PrivateKey,
Chilkat.BinData;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
privKey: TPrivateKey;
password: string;
usePkcs1: Boolean;
bd: TBinData;
begin
success := False;
// Load a private key to export.
privKey := TPrivateKey.Create;
success := privKey.LoadPemFile('qa_data/private.pem');
if (success = False) then
begin
WriteLn(privKey.LastErrorText);
Exit;
end;
// The key password is not hard-coded in a real application; obtain it from an interactive
// prompt, environment variable, or a secrets vault.
password := 'myKeyPassword';
// Export the key as DER into a BinData. The 1st argument selects PKCS #1 (True) versus PKCS #8
// (False); the 2nd is the password (empty string for an unencrypted export); the 3rd is the
// BinData that receives the DER bytes.
usePkcs1 := False;
bd := TBinData.Create;
success := privKey.GetPkcsBd(usePkcs1,password,bd);
if (success = False) then
begin
WriteLn(privKey.LastErrorText);
Exit;
end;
WriteLn('Exported ' + bd.NumBytes + ' DER bytes.');
privKey.Free;
bd.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.