Sample code for 30+ languages & platforms
Lazarus Pascal

Encrypt / Decrypt Secure Strings

See more Encryption Examples

Demonstrates how to use the EncryptSecureENC and DecryptSecureENC methods to encrypt/decrypt secure strings. These methods were added in Chilkat v9.5.0.71 (released January 2018).

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

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

procedure RunDemo;
var
  success: Boolean;
  secStr1: TSecureString;
  crypt: TCrypt2;
  encryptedStr: string;
  secStr2: TSecureString;

begin
  success := False;

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

  //  Load the secure string with some text.
  secStr1 := TSecureString.Create;
  success := secStr1.LoadFile('qa_data/txt/helloWorld.txt','utf-8');
  if (success <> True) then
    begin
      WriteLn('Failed to load helloWorld.txt');
      Exit;
    end;

  crypt := TCrypt2.Create;

  crypt.CryptAlgorithm := 'aes';
  crypt.CipherMode := 'cbc';
  crypt.KeyLength := 128;
  crypt.SetEncodedKey('000102030405060708090A0B0C0D0E0F','hex');
  crypt.SetEncodedIV('000102030405060708090A0B0C0D0E0F','hex');
  crypt.EncodingMode := 'base64';

  //  Return the base64 encoded encrypted contents of secStr1.
  encryptedStr := crypt.EncryptSecureENC(secStr1);
  WriteLn('Encrypted string: ' + encryptedStr);

  //  Output:
  //  Encrypted string: qiq+IFhcjTkEIkZyf31V/g==

  //  Decrypt to secStr2:
  secStr2 := TSecureString.Create;
  crypt.DecryptSecureENC(encryptedStr,secStr2);

  //  Access the contents of secStr2
  WriteLn('Decrypted string: ' + secStr2.Access());

  //  Output:
  //  Decrypted string: Hello World!


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