Sample code for 30+ languages & platforms
Lazarus Pascal

Example: Crypt2.ClearEncryptCerts method

Demonstrates how to call the ClearEncryptCerts method.

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.BinData,
  Chilkat.Cert,
  Chilkat.Crypt2;

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

procedure RunDemo;
var
  success: Boolean;
  crypt: TCrypt2;
  cert1: TCert;
  cert2: TCert;
  cert3: TCert;
  bd: TBinData;
  cert4: TCert;
  cert5: TCert;

begin
  success := False;

  crypt := TCrypt2.Create;

  //  Tell the crypt object to use 3 certificates.
  //  Do this by calling AddEncryptCert for each certificate.

  cert1 := TCert.Create;
  //  ...
  //  Load the cert from a source such as a .pfx/.p12 file, smart card, USB token, Apple keychain, Windows certificate store, etc.
  //  ...
  crypt.AddEncryptCert(cert1);

  cert2 := TCert.Create;
  //  ...
  crypt.AddEncryptCert(cert2);

  cert3 := TCert.Create;
  //  ...
  crypt.AddEncryptCert(cert3);

  //  Params for public-key encryption to create PKCS7 enveloped-data
  crypt.CryptAlgorithm := 'pki';
  crypt.Pkcs7CryptAlg := 'aes';
  crypt.KeyLength := 256;
  crypt.OaepHash := 'sha256';
  crypt.OaepPadding := True;

  bd := TBinData.Create;
  //  ...
  success := crypt.EncryptBd(bd);

  //  Let's say we now want to encrypt something else with different certs..
  //  First clear the encryption certs.
  crypt.ClearEncryptCerts();

  cert4 := TCert.Create;
  //  ...
  crypt.AddEncryptCert(cert4);

  cert5 := TCert.Create;
  //  ...
  crypt.AddEncryptCert(cert5);

  //  ...
  //  ...

  //  Encrypt using cert4 and cert5.
  success := crypt.EncryptBd(bd);


  crypt.Free;
  cert1.Free;
  cert2.Free;
  cert3.Free;
  bd.Free;
  cert4.Free;
  cert5.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.