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

Encrypt MIME for Multiple Recipients

See more MIME Examples

Demonstrates encrypting the current MIME entity for several recipients at once. A certificate is added for each recipient with AddEncryptCert, and EncryptN then produces a single CMS/PKCS #7 message that each recipient can independently decrypt.

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.

Background. When a message targets more than one recipient, each recipient's certificate is added to the internal list and EncryptN wraps the message key separately for each one. Any single recipient can then decrypt with their own private key.

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

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  cert1: TCert;
  cert2: TCert;

begin
  success := False;

  mime := TMime.Create;
  success := mime.SetBodyFromPlainText('This message will be encrypted.');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Add a certificate for each intended recipient.  Every recipient receives an independently
  //  wrapped copy of the message key.
  cert1 := TCert.Create;
  success := cert1.LoadFromFile('qa_data/recipient1.cer');
  if (success = False) then
    begin
      WriteLn(cert1.LastErrorText);
      Exit;
    end;
  success := mime.AddEncryptCert(cert1);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  cert2 := TCert.Create;
  success := cert2.LoadFromFile('qa_data/recipient2.cer');
  if (success = False) then
    begin
      WriteLn(cert2.LastErrorText);
      Exit;
    end;
  success := mime.AddEncryptCert(cert2);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Encrypt for every added recipient at once.
  success := mime.EncryptN();
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  success := mime.SaveMime('qa_output/encrypted_multi.eml');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;
  WriteLn('Message encrypted for multiple recipients.');


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