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

Encrypt / Decrypt a File and Verify it has not Changed

See more Encryption Examples

Demonstrates how to encrypt and decrypt a file, and verify it has not changed.

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.FileAccess;

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

procedure RunDemo;
var
  success: Boolean;
  crypt: TCrypt2;
  ivHex: string;
  keyHex: string;
  dataFile: string;
  outFile: string;
  outFile2: string;
  fac: TFileAccess;
  bEqual: Boolean;

begin
  success := False;

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

  crypt := TCrypt2.Create;

  crypt.CryptAlgorithm := 'aes';
  crypt.CipherMode := 'cbc';
  crypt.KeyLength := 128;
  crypt.PaddingScheme := 0;

  ivHex := '000102030405060708090A0B0C0D0E0F';
  crypt.SetEncodedIV(ivHex,'hex');

  keyHex := '00010203040506071011121314151617';
  crypt.SetEncodedKey(keyHex,'hex');

  dataFile := 'qa_data/zips/HBIQ040615300005.ZIP';
  outFile := 'qa_output/HBIQ040615300005.enc';
  outFile2 := 'qa_output/HBIQ040615300005.ZIP';

  success := crypt.CkEncryptFile(dataFile,outFile);
  success := crypt.CkDecryptFile(outFile,outFile2);

  fac := TFileAccess.Create;
  bEqual := fac.FileContentsEqual(dataFile,outFile2);
  if (bEqual <> True) then
    begin
      WriteLn('Decrypted file not equal to the original.');
    end
  else
    begin
      WriteLn('Success.');
    end;


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