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

Set the OAEP MGF Hash for Encrypted Email

See more Email Object Examples

Demonstrates the Chilkat Email.OaepMgfHash property, which selects the mask-generation function (MGF) hash algorithm used by OAEP padding when encrypting email with RSAES-OAEP. Valid values are sha1, sha256, sha384, and sha512; the default is sha256. It is used only when OaepPadding is true, and some recipients require the MGF hash to match OaepHash.

Background: OAEP internally needs a "mask generation function" (MGF) — a way to stretch a hash into a longer pseudo-random mask — and that MGF is itself built on a hash algorithm. In principle the MGF hash can differ from the main OAEP hash, but many implementations assume the two are the same, so setting both to sha256 is the safest interoperable choice. Mismatched settings between sender and recipient are a common cause of "decryption failed" errors.

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.OaepMgfHash property, which selects the mask-generation
  //  function (MGF) hash algorithm used by OAEP padding when encrypting with RSAES-OAEP.
  //  Valid values: sha1, sha256, sha384, sha512.  The default is sha256.  It is used only
  //  when OaepPadding is true.

  email := TEmail.Create;
  email.Subject := 'Encrypted with RSAES-OAEP';

  email.OaepPadding := True;
  email.OaepHash := 'sha256';

  //  Some recipients require the MGF hash to match the OAEP hash.
  email.OaepMgfHash := 'sha256';

  WriteLn('OaepMgfHash = ' + email.OaepMgfHash);


  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.