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

Extract PKCS7 from MIME and Decrypt

See more MIME Examples

Extracts the base64-encoded PKCS7 body of a MIME message to a file, and then decrypts using Chilkat Crypt2.

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

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  crypt: TCrypt2;
  inPath: string;
  outPath: string;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  mime := TMime.Create;

  success := mime.LoadMimeFile('c:/aaworkarea/EmailInBytes.txt');
  if (success <> True) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  success := mime.SaveBody('c:/aaworkarea/smime.p7m');
  if (success <> True) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  crypt := TCrypt2.Create;

  success := crypt.AddPfxSourceFile('c:/aaworkarea/my.pfx','pfxPassword');
  if (success = False) then
    begin
      WriteLn(crypt.LastErrorText);
      Exit;
    end;

  //  Indicate the public-key (PKCS7) encryption/decryption should be used:
  crypt.CryptAlgorithm := 'pki';

  inPath := 'c:/aaworkarea/smime.p7m';
  outPath := 'c:/aaworkarea/decrypted.dat';

  success := crypt.CkDecryptFile(inPath,outPath);
  if (success = False) then
    begin
      WriteLn(crypt.LastErrorText);
      Exit;
    end;

  WriteLn('Success.');


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