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

Example: Mime.GetDecryptCertInfo method

Demonstrates the GetDecryptCertInfo method.

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.Mime,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  json: TJsonObject;
  serial: string;
  issuerCN: string;
  i: Integer;
  count: Integer;

begin
  success := False;

  mime := TMime.Create;

  //  Load MIME that is has Content-Type like this:
  //  Content-Type: application/pkcs7-mime; smime-type="enveloped-data"; name="smime.p7m"; smime-type="enveloped-data"
  success := mime.LoadMimeFile('qa_data/mime/enveloped_data.eml');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Get information about the certificate that would be needed to decrypt.
  //  An enveloped-data can potentially be decrypted by multiple certificates if it was encrypted in a way that allows it,
  //  but in most cases, only a single certificate with associated private key (that of the message recipient) is possible.
  json := TJsonObject.Create;
  success := mime.GetDecryptCertInfo(json);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  json.EmitCompact := False;
  WriteLn(json.Emit());

  //  Sample output:

  //  {
  //    "recipientInfo": [
  //      {
  //        "serial": "****",
  //        "issuerCN": "****"
  //      }
  //    ]
  //  }

  //  Get each certificate's information like this:

  i := 0;
  count := json.SizeOfArray('recipientInfo');
  while i < count do
    begin
      json.I := i;
      serial := json.StringOf('recipientInfo[i].serial');
      issuerCN := json.StringOf('recipientInfo[i].issuerCN');
      i := i + 1;
    end;



  mime.Free;
  json.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.