Demonstration of Decryption

Programs ASM_Decrypt (Intel syntax) and ASM_DecryptA (AT&T syntax) decrypt the file Encrypted.txt produced by our encryption program. Check that the output file Decrypted.txt is the same as the original file before encryption. (Encryption and decryption require the same operations so some of you would prefer to use a single program for both. We thought that separate programs each with their own identifiers would be slightly easier to understand).

program ASM_Decrypt;
  {$APPTYPE CONSOLE}
  {$ASMMODE INTEL}  //Delete this line in Delphi
uses
  SysUtils;
const
  KEYS : array [0..7] of  byte = ($85, $A8, $FA, $D3, $C7, $8B, $E2, $9B);
  ENCRYPTED = 'Encrypted.txt';
  DECRYPTED = 'Decrypted.txt';
var
  DecryptedFile, EncryptedFile : textFile;
  CurrentString : shortString;
  StringLength, CharCount, Millisecs: integer;
  Start, Finish : TDateTime;
  ExecutionTime : real;
begin
  Start := now;
  CharCount := 0;
  assignFile(EncryptedFile, ENCRYPTED);
  assignFile(DecryptedFile, DECRYPTED);
  reset(EncryptedFile);
  rewrite(DecryptedFile);
  while not eof(EncryptedFile) do
    begin
      readln(EncryptedFile, CurrentString);
      StringLength := Length(CurrentString);
      asm
        MOV ECX, StringLength //ECX will count down
        CMP ECX, 0
        JE @Finish
        @Start: ADD CharCount, 1
          MOV EDX, CharCount //Move into EDX the next index of array
          AND EDX, 7 //Get an offset 0 to 7
          MOV AL, [ECX+CurrentString]
          XOR AL, [EDX+KEYS] //Decrypt
          MOV [ECX+CurrentString], AL //Replace original in CurrentString
          Loop @Start
        @Finish:
      end;
      writeln(DecryptedFile, CurrentString);
    end;
  closeFile(DecryptedFile);
  closeFile(EncryptedFile);
  writeln('Decryption complete');
  Finish := Now;
  ExecutionTime := Finish - Start;
  ExecutionTime := ExecutionTime * MSecsPerDay;
  Millisecs := Trunc(ExecutionTime);
  writeln('Execution time in ms: ', Millisecs);
  readln;
end.


program ASM_DecryptA;
  {$mode objfpc}{$H+}
  {$ASMMODE ATT}  
uses
  SysUtils;
const
  KEYS : array [0..7] of  byte = ($85, $A8, $FA, $D3, $C7, $8B, $E2, $9B);
  ENCRYPTED = 'Encrypted.txt';
  DECRYPTED = 'Decrypted.txt';
var
  DecryptedFile, EncryptedFile : textFile;
  CurrentString : shortString;
  StringLength, CharCount, Millisecs: integer;
  Start, Finish : TDateTime;
  ExecutionTime : real;
begin
  Start := now;
  CharCount := 0;
  assignFile(EncryptedFile, ENCRYPTED);
  assignFile(DecryptedFile, DECRYPTED);
  reset(EncryptedFile);
  rewrite(DecryptedFile);
  while not eof(EncryptedFile) do
    begin
      readln(EncryptedFile, CurrentString);
      StringLength := Length(CurrentString);
      asm
        MOV StringLength, %ECX //ECX will count down
        CMP $0, %ECX //Check for empty string
        JE .LFinish //If zero flag set, do not decrypt
        .LStart: ADD $1, CharCount
          MOV CharCount, %EDX //Move into EDX the character count
          AND $7, %EDX //get an offset 0 to 7
          MOV  CurrentString (%ECX), %AL //Load AL with code of current char
          XOR KEYS (%EDX), %AL //Decrypt
          MOV %AL, CurrentString (%ECX) //Replace original in CurrentString
          Loop .LStart //Go to start of loop is ECX is not 0
        .LFinish:
      end;
      writeln(DecryptedFile, CurrentString);
    end;
  closeFile(DecryptedFile);
  closeFile(EncryptedFile);
  writeln('Decryption complete');
  Finish := Now;
  ExecutionTime := Finish - Start;
  ExecutionTime := ExecutionTime * MSecsPerDay;
  Millisecs := Trunc(ExecutionTime);
  writeln('Execution time in ms: ', Millisecs);
  readln;
end.
  
Programming - a skill for life!

Demonstrations of encryption and decryption with and without assembler code