Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Encrypt with RSA-OAEP and AES-128-CBC

See more Email Object Examples

Use this profile when the receiver requires AES-128-CBC content encryption and RSA-OAEP with SHA-256. The SHA-256 settings apply to OAEP and its mask generation function. Use the matching decryption example to inspect the result.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.MailMan,
  Chilkat.BinData,
  Chilkat.Cert,
  Chilkat.Email;

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

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;
  cert: TCert;
  mail: TMailMan;
  rendered: TBinData;

begin
  success := False;

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

  email := TEmail.Create;
  success := email.LoadEml('message.eml');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  //  Use the recipient's public encryption certificate. Its private key stays with the recipient.
  cert := TCert.Create;
  success := cert.LoadFromFile('recipient.cer');
  if (success <> True) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;
  success := email.SetEncryptCert(cert);
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  email.Pkcs7CryptAlg := 'aes';
  email.Pkcs7KeyLength := 128;
  email.OaepPadding := True;
  email.OaepHash := 'sha256';
  email.OaepMgfHash := 'sha256';
  email.SendEncrypted := True;
  mail := TMailMan.Create;
  rendered := TBinData.Create;
  success := mail.RenderToMimeBd(email,rendered);
  if (success <> True) then
    begin
      WriteLn(mail.LastErrorText);
      Exit;
    end;
  success := rendered.WriteFile('oaep-encrypted.eml');
  if (success <> True) then
    begin
      WriteLn('Failed: rendered.WriteFile.');
      Exit;
    end;
  WriteLn('Saved oaep-encrypted.eml.');


  email.Free;
  cert.Free;
  mail.Free;
  rendered.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.