home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tcsel003.zip / ENDECODE.PAS < prev    next >
Pascal/Delphi Source File  |  1992-10-16  |  4KB  |  112 lines

  1. UNIT Endecode;
  2.  { Simple encryption/decryption routines.                  }
  3.  
  4. interface
  5.  
  6. type
  7.   starray = array[0..255] of byte;
  8.  
  9. function Key(var s): longint;
  10. function EncryptStr(key : longint; s: string): string;
  11. function DecryptStr(key : longint; s: string): string;
  12.  
  13. implementation
  14.  
  15. function MakeCodeStr(key : longint; var s): string;
  16.   {--------------------------------------------------------}
  17.   { Creates a "randomly" constructed string of a similar   }
  18.   { length as the string which is to be encrypted. A       }
  19.   { longint key is used to permit "password" type          }
  20.   { encryption. This key may be passed as a literal or     }
  21.   { some password used to calculate it. Using this key,    }
  22.   { the last character of the string to be encrypted and   }
  23.   { the length of the string, a "code string" is produced. }
  24.   { This code string is then XORd with the original string }
  25.   { to produce the encrypted string. The last character    }
  26.   { however must be treated differently so that it can be  }
  27.   { easily decoded in order to reproduce the coded string  }
  28.   { used for decoding. This is done by XORing it with the  }
  29.   { length of the string. To decrypt a string the last     }
  30.   { character must be decoded first and then the key coded }
  31.   { string produced in order to decrypt each character.    }
  32.   {--------------------------------------------------------}
  33.  
  34.   var
  35.     x   : word;
  36.     len : byte absolute s;
  37.     st  : array[0..255] of byte absolute s;
  38.  
  39.   begin
  40.     RandSeed := (key * len) DIV st[len];
  41.     {This ensures that no two code strings will be similar UNLESS they are
  42.      of identical length, have identical last characters and the same
  43.      key is used.}
  44.     MakeCodeStr[0] := chr(len);
  45.     for x := 1 to len do
  46.       MakeCodeStr[x] := chr(32 + Random(95));
  47.       {Keeping the character between 32 and 127 ensures that the high bit
  48.        is never set on the original encrypted character and therefore allows
  49.        this to be used as flag to indicate that the coded char was < #32.
  50.        This will then permit the encrypted string to be printed without fear
  51.        of having embedded control codes play havoc with the printer.}
  52.   end;
  53.  
  54. function Key(var s): longint;
  55.   { Creates a key for seeding the random number generator. st can be a
  56.     password }
  57.   var
  58.     x     : byte;
  59.     temp  : longint;
  60.     c     : array[1..64] of longint absolute s;
  61.     len   : byte absolute s;
  62.   begin
  63.     temp  := 0;
  64.     for x := 1 to len div 4 do
  65.       temp := temp xor c[x];
  66.     Key := Abs(temp);
  67.   end;
  68.  
  69. function EncryptStr(key : longint; s: string): string;
  70.   var
  71.     cnt,x          : byte;
  72.     len            : byte absolute s;
  73.     st             : array[0..255] of byte absolute s;
  74.     CodeStr        : starray;
  75.     temp           : string absolute CodeStr;
  76.   begin
  77.     temp           := MakeCodeStr(key,st);
  78.     EncryptStr[0]  := chr(len);
  79.     EncryptStr[len]:= chr(st[len]);
  80.     for x := 1 to len-1 do begin
  81.       cnt := st[x] xor CodeStr[x];
  82.       inc(cnt,128 * ord(cnt < 32));
  83.       EncryptStr[x]:= chr(cnt);
  84.     end;  { for }
  85.     cnt := st[len] xor (len and 127);
  86.     inc(cnt,128 * ord(cnt < 32));
  87.     EncryptStr[len]:= chr(cnt);
  88.   end;
  89.  
  90. function DecryptStr(key : longint; s: string): string;
  91.   var
  92.     cnt,x        : byte;
  93.     st           : starray absolute s;
  94.     len          : byte absolute s;
  95.     CodeStr      : starray;
  96.     temp         : string absolute CodeStr;
  97.     ch           : char;
  98.   begin
  99.     cnt          := st[len] and 127;
  100.     st[len]      := cnt xor len;
  101.     temp         := MakeCodeStr(key,st);
  102.     DecryptStr[0]:= chr(len);
  103.     DecryptStr[len]:= chr(st[len]);
  104.     for x := 1 to len-1 do begin
  105.       cnt        := st[x];
  106.       dec(cnt,128 * ord(st[x] > 127));
  107.       DecryptStr[x] := chr(cnt xor CodeStr[x]);
  108.     end;  { for }
  109.   end;
  110.  
  111. end.
  112.