Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Get EC Public Key from EC Private Key

See more ECC Examples

Demonstrates how to get an EC public key from an EC private key.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.PublicKey;

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

procedure RunDemo;
var
  success: Boolean;
  privKey: TPrivateKey;
  pubKey: TPublicKey;

begin
  success := False;

  //  We have an ECC private key...
  //  The contents of the private key PEM file look like this:

  //  	-----BEGIN PRIVATE KEY-----
  //  	MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg3J8q/24D1sEKGdP9
  //  	72MGYElLGpw/a56Y3t6pfON3uhShRANCAATlSmoizyhAwoYZAOuFBATl07/1RR54
  //  	a1Dzfm16grxJe666AGKR+bSs24hk7TEpaeCTvT8YOOM3l+xKFg7zq6Q9
  //  	-----END PRIVATE KEY-----

  privKey := TPrivateKey.Create;
  success := privKey.LoadPemFile('qa_data/ecc/secp256r1-key-pkcs8.pem');
  if (success = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  //  Get the public key.
  pubKey := TPublicKey.Create;
  privKey.ToPublicKey(pubKey);

  //  Save the public key to a PEM file.
  success := pubKey.SavePemFile(False,'qa_data/ecc/secp256r1-pubkey.pem');
  if (success = False) then
    begin
      WriteLn(pubKey.LastErrorText);
      Exit;
    end;

  //  The contents of the ECC public key PEM file look like this:

  //  	-----BEGIN PUBLIC KEY-----
  //  	MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
  //  	AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
  //  	///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
  //  	NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
  //  	RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
  //  	//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABOVKaiLPKEDChhkA64UEBOXT
  //  	v/VFHnhrUPN+bXqCvEl7rroAYpH5tKzbiGTtMSlp4JO9Pxg44zeX7EoWDvOrpD0=
  //  	-----END PUBLIC KEY-----

  WriteLn('Success.');


  privKey.Free;
  pubKey.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.