Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.8.0+

Decrypt an S/MIME email and verify its signature

See more Email Object Examples

Register the recipient’s certificate and private key (from a PEM file) before opening signed-encrypted.eml. This workflow expects both encryption and a signature, so it checks both layers before reading the message. Cryptographic signature validity is one input to a signer-trust decision.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
email: TChilkatEmail;
pem: TChilkatPem;
signer: TChilkatCert;

begin
success := 0;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

email := TChilkatEmail.Create(Self);
//  The recipient's certificate and private key are in a PEM file.  The 2nd argument of LoadPemFile
//  is the password of the private key.
pem := TChilkatPem.Create(Self);
success := pem.LoadPemFile('recipient.pem','RECIPIENT_PASSWORD');
if (success <> 1) then
  begin
    Memo1.Lines.Add(pem.LastErrorText);
    Exit;
  end;
success := email.AddPemSource(pem.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
success := email.LoadEml('signed-encrypted.eml');
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
if ((email.ReceivedEncrypted <> 1) or (email.Decrypted <> 1)) then
  begin
    Memo1.Lines.Add('Expected a successfully decrypted S/MIME envelope.');
    Exit;
  end;
if ((email.ReceivedSigned <> 1) or (email.SignaturesValid <> 1)) then
  begin
    Memo1.Lines.Add('Expected a valid signature inside the envelope.');
    Exit;
  end;
//  This message has one signer. Examine its certificate after verification.
signer := TChilkatCert.Create(Self);
success := email.LastSignerCert(0,signer.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
Memo1.Lines.Add('Signer certificate: ' + signer.SubjectDN);
Memo1.Lines.Add('Recovered subject: ' + email.Subject);
Memo1.Lines.Add('Recovered attachments: ' + IntToStr(email.NumAttachments));