home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / timer / tptimer / endecode.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-12-14  |  3.8 KB  |  98 lines

  1. UNIT Endecode;
  2.  
  3. {***************************************************************************
  4.  *                                                                         *
  5.  *                     Copyright 1988 Trevor J Carlsen                     *
  6.  *                   Rovert Software Consulting Services                   *
  7.  *                                PO Box 568                               *
  8.  *                   Port Hedland Western Australia 6721                   *
  9.  *                                                                         *
  10.  *                                                                         *
  11.  *                  Simple encryption decryption routines.                 *
  12.  *                                                                         *
  13.  ***************************************************************************}
  14.  
  15.  
  16. interface
  17.  
  18. FUNCTION MakeCodeStr(len : byte;st : string): string;
  19. FUNCTION EncryptStr(st : string): string;
  20. FUNCTION DecryptStr(st : string): string;
  21.  
  22. implementation
  23.  
  24. FUNCTION MakeCodeStr(len: byte;st : string): string;
  25. { Creates a "randomly" constructed string of a similar length as the string
  26.   which is to be encrypted. The ASCII code of the last character of that
  27.   string is used (along with the string length) to produce a number for the
  28.   random generator seed that will be almost unique. I say almost, because it
  29.   is obvious that two identical length strings that share a common last
  30.   character will produce the same code string. However this would be rare
  31.   enough for normal encryption needs.
  32.   This code string is then XORd with the original string to produce the
  33.   encrypted string. The last character however must be treated differently
  34.   so that it can be easily decoded in order to produce the coded string used
  35.   for decoding. This is done by XORing it with the length of the string. To
  36.   decrypt a string the last character must be decoded first and then the key
  37.   coded string produced in order to decrypt each character.}
  38.  
  39.   VAR x : word;
  40.   begin
  41.     RandSeed := (123456 * len) DIV ord(st[len]);
  42.     {This ensures that no two code strings will be similar UNLESS they are
  43.      of identical length and have identical last characters. Use any number
  44.      you wish as the numerical constant.}
  45.     MakeCodeStr[0] := chr(ord(len));
  46.     for x := 1 to len do
  47.       MakeCodeStr[x] := chr(32 + Random(95));
  48.       {Keeping the character between 32 and 127 ensures that the high bit
  49.        is never set on the original encrypted character and therefore allows
  50.        this to be used as flag to indicate that the coded char was < #32.
  51.        This will then permit the encrypted string to be printed without fear
  52.        of having embedded control codes play havoc with the printer.}
  53.   end;
  54.  
  55.  
  56. FUNCTION EncryptStr(st : string): string;
  57.   VAR
  58.     cnt,x,len : byte;
  59.     temp,CodeStr : string;
  60.   begin
  61.     len := length(st);
  62.     CodeStr := MakeCodeStr(len,st);
  63.     temp[0] := st[0];
  64.     temp[len] := st[len];
  65.     for x := 1 to len-1 do begin
  66.       cnt := ord(st[x]) xor ord(CodeStr[x]);
  67.       inc(cnt,128*ord(cnt < 32));
  68.       temp[x] := chr(cnt);
  69.       end;
  70.     cnt := ord(st[len]) xor (len and 127);
  71.     inc(cnt,128*ord(cnt < 32));
  72.     temp[len] := chr(cnt);
  73.     EncryptStr := temp;
  74.   end;
  75.  
  76. FUNCTION DecryptStr(st : string): string;
  77.   VAR
  78.     cnt,x,len : byte;
  79.     temp,CodeStr : string;
  80.     ch : char;
  81.   begin
  82.     len := ord(st[0]);
  83.     cnt := ord(st[len]) and 127;
  84.     st[len] := chr(cnt xor len);
  85.     CodeStr := MakeCodeStr(len,st);
  86.     temp[0] := st[0];
  87.     temp[len] := st[len];
  88.     for x := 1 to len-1 do begin
  89.       cnt := ord(st[x]);
  90.       dec(cnt,128*ord(cnt > 127));
  91.       st[x] := chr(cnt);
  92.       temp[x] := chr(ord(st[x]) xor ord(CodeStr[x]));
  93.       end;
  94.       DecryptStr := temp;
  95.   end;
  96.  
  97. end.
  98.