Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get Recipient Certificate Info from Encrypted S/MIME
See more MIME Examples
Demonstrates GetDecryptCertInfo, which examines an encrypted S/MIME entity and writes the recipient-certificate identifiers into a JsonObject. This reveals which certificate(s) the message was encrypted for, so the correct private key can be located before decrypting.
The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Tip: Code to parse the returned JSON can be generated with Chilkat's online tool at https://tools.chilkat.io/jsonParse.
Background. A CMS/PKCS #7 encrypted message carries recipient identifiers (such as issuer and serial number). Inspecting them lets an application determine, without decrypting, which key it needs.
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.Mime,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
mime: TMime;
certInfo: TJsonObject;
json: string;
begin
success := False;
mime := TMime.Create;
success := mime.LoadMimeFile('qa_data/encrypted.eml');
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
// Examine the encrypted entity and write the recipient-certificate identifiers to a JsonObject.
// This tells you which certificate(s) the message was encrypted for, so you can locate the right
// private key before attempting to decrypt.
certInfo := TJsonObject.Create;
success := mime.GetDecryptCertInfo(certInfo);
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
certInfo.EmitCompact := False;
json := certInfo.Emit();
WriteLn(json);
// JSON parsing code for this result can be generated at Chilkat's online tool:
// https://tools.chilkat.io/jsonParse
mime.Free;
certInfo.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.