Encryption without MMX

In this section we demonstrate encryption without MMX for comparison with the previous MMX demonstrations. We use the same quick procedures for loading from file and saving to file. By including code to run each conversion 100 times we showed that, as expected, this 32-bit conversion takes about twice as long as the 64-bit MMX methods. As with the previous two programs, you need a large test file to test the speed of the program. A text file is suitable for checking quickly the encryption and decryption, but you do not need to use a text file. You need to change the constants INFILE, ENCRYPTED and DECRYPTED as necessary for your tests.

program Encrypt_32;
  {$APPTYPE CONSOLE}
uses
  SysUtils, Windows, Classes;
type
  TIntegers = array of integer;

const
  KEYS : array[0 .. 7] of integer = ($85A8FAD3, $C78BE29B,
                                     $81A2F6D0, $C28AE49C,
                                     $86A4F8D6, $CA82EB92,
                                     $84ABF3D9, $C081E296);
  INFILE = 'Programs.txt';
  ENCRYPTED = 'Encrypted.txt';
  DECRYPTED = 'Decrypted.txt';
var
  Ints : TIntegers;
  FileBytes, NumOfInts : integer;

procedure FileToArray(const strSource : string; var iFileBytes, iNumOfInts : integer);
var
  fsCurrent : TFileStream;
  iBytesRead : integer;
begin
  fsCurrent := TFileStream.Create(strSource, fmOpenRead);
  iFileBytes := fsCurrent.Size;
  iNumOfInts := (iFileBytes div 4) + 1;
  SetLength(Ints, iNumOfInts);
  iBytesRead := fsCurrent.Read(Ints[0], iFileBytes);
  if iBytesRead <> iFileBytes then
    writeln('Error reading from file');
  fsCurrent.Destroy;
end;

procedure ArrayToFile(const strDestination : string; var arraySource : TIntegers;
                      const iFileBytes : integer);
var
  fsCurrent : TFileStream;
  iBytesWritten : integer;
begin
  fsCurrent := TFileStream.Create(strDestination, fmCreate);
  iBytesWritten := fsCurrent.Write(arraySource[0], iFileBytes);
  if iBytesWritten <> iFileBytes then
    writeln('Error writing to file');
  fsCurrent.Destroy;
end;

procedure Convert(var Integers : TIntegers; iNumOfInts : integer);
var
  Index, i, StartTicks, Ticks : integer;
begin
  StartTicks := GetTickCount;
  for i := 0 to iNumOfInts - 1 do
    begin
      Index := i MOD 8;
      Integers[i] := Integers[i] XOR Keys[Index];
    end;
  Ticks := GetTickCount - StartTicks;
  writeln('Array converted in ', Ticks, ' ms. ');
end;

begin
  FileToArray(INFILE, FileBytes, NumOfInts);
  Convert(Ints, NumOfInts);
  ArrayToFile(ENCRYPTED, Ints, FileBytes);
  writeln('File encrypted. Press Enter to decrypt.');
  readln;
  FileToArray(ENCRYPTED, FileBytes, NumOfInts);
  Convert(Ints, NumOfInts);
  ArrayToFile(DECRYPTED, Ints, FileBytes);
  writeln('File decrypted. Press Enter to exit.');
  readln;
end.
Programming - a skill for life!

How to use the MMX capabilities of Pentium processors