Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Load a Private Key from BinData (Any Format)
See more Private Key Examples
Demonstrates the Chilkat PrivateKey.LoadAnyFormat method, which loads a private key by inspecting the bytes in a BinData. The first argument is the BinData and the second is the password (used if the detected key is encrypted).
Note: The file path is relative to the application's current working directory. Absolute paths may also be used. Supply the path to your own key file.
Background: When you have key bytes in memory but do not know their exact format — downloaded, decrypted, or pulled from a database — this figures it out for you, recognizing PKCS #1/#8 DER, encrypted PKCS #8, PEM, JWK, Chilkat XML, Microsoft formats, and more. It removes the guesswork of choosing a specific loader, at the small cost of the detection step. Supply the password so it can decrypt an encrypted key without a second call.
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.BinData,
Chilkat.PrivateKey;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
bd: TBinData;
privKey: TPrivateKey;
password: string;
begin
success := False;
// Demonstrates the PrivateKey.LoadAnyFormat method, which loads a private key by inspecting the
// bytes in a BinData. The 1st argument is the BinData and the 2nd is the password (used if the
// detected key is encrypted).
bd := TBinData.Create;
success := bd.LoadFile('qa_data/some_private_key');
if (success = False) then
begin
WriteLn(bd.LastErrorText);
Exit;
end;
privKey := TPrivateKey.Create;
// 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';
// Chilkat auto-detects the representation: PKCS #1/#8 DER, encrypted PKCS #8, PEM, JWK,
// Chilkat XML, Microsoft formats, and more.
success := privKey.LoadAnyFormat(bd,password);
if (success = False) then
begin
WriteLn(privKey.LastErrorText);
Exit;
end;
WriteLn('Loaded a ' + privKey.KeyType + ' private key.');
bd.Free;
privKey.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.