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

Get the Certificate Used to Decrypt an Email

See more Email Object Examples

Demonstrates the Chilkat Email.LastDecryptCert method, which copies the certificate used to decrypt this email into a Cert object. It should be called after a successful decryption. This example loads an encrypted email and, if it was decrypted, reads the decrypting certificate's common name.

Background: When a message could be decrypted with one of several available private keys, it is often useful to know which certificate actually did the job — for auditing, for confirming the message was encrypted for the expected identity, or for logging. LastDecryptCert hands you that certificate as an object so you can inspect its subject, issuer, validity dates, and more.

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

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

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;
  cert: TCert;

begin
  success := False;

  //  Demonstrates the LastDecryptCert method, which copies the certificate used to decrypt
  //  this email into a Cert object.  Call it after a successful decryption.

  email := TEmail.Create;

  success := email.LoadEml('qa_data/eml/encrypted.eml');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  if (email.ReceivedEncrypted = True) then
    begin
      if (email.Decrypted = True) then
        begin
          //  Get the certificate that was used to decrypt the email.
          cert := TCert.Create;
          success := email.LastDecryptCert(cert);
          if (success = True) then
            begin
              WriteLn('Decrypted with certificate (CN): ' + cert.SubjectCN);
            end;
        end;
    end;

  //  Note: The path "qa_data/..." is a relative local filesystem path,
  //  relative to the current working directory of the running application.


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