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

Select the S/MIME Encryption Algorithm

See more Email Object Examples

Demonstrates the Chilkat Email.Pkcs7CryptAlg property, which selects the underlying symmetric encryption algorithm used when an email is sent encrypted with PKCS#7 public-key (S/MIME) encryption. Possible values are aes, aes-gcm, des, 3des, and rc2; the default is aes. This example selects AES and pairs it with a 256-bit key length.

Background: S/MIME encryption is hybrid. The message body is encrypted with a fast symmetric algorithm (the one this property chooses, such as AES) using a randomly generated one-time key; then that one-time key is itself encrypted with each recipient's public key. This gives the speed of symmetric encryption with the key-distribution convenience of public-key cryptography. Modern choices like aes are strongly preferred — older options such as des and rc2 are considered weak.

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;

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

procedure RunDemo;
var
  email: TEmail;

begin
  //  Demonstrates the Email.Pkcs7CryptAlg property, which selects the symmetric encryption
  //  algorithm used when an email is sent encrypted with PKCS#7 (S/MIME) public-key
  //  encryption.  Possible values: aes, aes-gcm, des, 3des, rc2.  The default is aes.

  email := TEmail.Create;
  email.Subject := 'Encrypted email';
  email.Body := 'This message will be S/MIME encrypted when sent.';

  //  Select AES with a 256-bit key for the symmetric encryption.
  email.Pkcs7CryptAlg := 'aes';
  email.Pkcs7KeyLength := 256;

  WriteLn('Pkcs7CryptAlg = ' + email.Pkcs7CryptAlg);
  WriteLn('Pkcs7KeyLength = ' + email.Pkcs7KeyLength);


  email.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.