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

Decrypt a Password-Encrypted Email with AES

See more Email Object Examples

Demonstrates the Chilkat Email.AesDecrypt method, which decrypts and restores an email message previously encrypted with AesEncrypt. The password must be the same one used to encrypt. This is Chilkat's password-based email-object decryption — it is not S/MIME certificate-based decryption. This example encrypts an email and then decrypts it to recover the original body.

Background: Symmetric encryption is reversible with the same secret: the identical password that scrambled the message unscrambles it. There is no separate "public" and "private" key as in S/MIME — which makes the scheme simple but means anyone holding the password can both encrypt and decrypt. The round trip shown here (encrypt then decrypt) is a common way to verify the password and confirm the content is faithfully restored.

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 AesDecrypt method, which decrypts and restores an email that was
  //  previously encrypted with AesEncrypt.  The password must match the one used to encrypt.
  //  This example encrypts an email, then decrypts it to restore the original content.

  email := TEmail.Create;
  email.Subject := 'Confidential';
  email.Body := 'Secret contents.';

  //  Encrypt with a password...
  email.AesEncrypt('secret_password');

  //  ...then decrypt with the same password to restore the original email.
  email.AesDecrypt('secret_password');

  WriteLn('Body after decrypt = ' + email.Body);


  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.