Encryption without Assembler

Programs Encrypt and Decrypt show how you can encrypt and decrypt files without assembler using the same algorithms as our in-line assembler programs.

program Encrypt;
  {$APPTYPE CONSOLE}
uses
  SysUtils;
const
  KEYS : array [0..7] of  byte = ($85, $A8, $FA, $D3, $C7, $8B, $E2, $9B);
  INFILE = 'DelphiManager.txt';
  ENCRYPTED = 'Encrypted.txt';
var
  TestFile, EncryptedFile : textFile;
  CurrentString : shortstring;
  StringLength, CharCount, i, Index : integer;
  CurrentByte, Key: byte;

begin
  CharCount := 0;
  assignFile(TestFile, INFILE);
  assignFile(EncryptedFile, ENCRYPTED);
  reset(TestFile);
  rewrite(EncryptedFile);
  while not eof(TestFile) do
    begin
      readln(TestFile, CurrentString);
      StringLength := Length(CurrentString);
      for i :=  StringLength downto 1 do
        begin
           inc(CharCount);
           Index := CharCount MOD 8;
           Key := KEYS[Index];
           CurrentByte := ord(CurrentString[i]);
           CurrentByte := CurrentByte XOR Key;
           CurrentString[i] := chr(CurrentByte);
        end;
      writeln(EncryptedFile, CurrentString);
    end;
  closeFile(TestFile);
  closeFile(EncryptedFile);
  writeln('Encryption complete');
  readln;
end.
 

program Decrypt;
  {$APPTYPE CONSOLE}
uses
  SysUtils;
const
  KEYS : array [0..7] of  byte = ($85, $A8, $FA, $D3, $C7, $8B, $E2, $9B);
  ENCRYPTED = 'Encrypted.txt';
  DECRYPTED = 'Decrypted.txt';
var
  EncryptedFile, DecryptedFile : textFile;
  CurrentString : shortstring;
  StringLength, CharCount, i, Index : integer;
  CurrentByte, Key: byte;
begin
  CharCount := 0;
  assignFile(EncryptedFile, ENCRYPTED);
  assignFile(DecryptedFile, DECRYPTED);
  reset(EncryptedFile);
  rewrite(DecryptedFile);
  while not eof(EncryptedFile) do
    begin
      readln(EncryptedFile, CurrentString);
      StringLength := Length(CurrentString);
      for i :=  StringLength downto 1 do
        begin
           inc(CharCount);
           Index := CharCount MOD 8;
           Key := KEYS[Index];
           CurrentByte := ord(CurrentString[i]);
           CurrentByte := CurrentByte XOR Key;
           CurrentString[i] := chr(CurrentByte);
        end;
      writeln(DecryptedFile, CurrentString);
    end;
  closeFile(EncryptedFile);
  closeFile(DecryptedFile);
  writeln('Decryption complete');
  readln; 
end.
  
Programming - a skill for life!

Demonstrations of encryption and decryption with and without assembler code