home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / pascal / 7875 < prev    next >
Encoding:
Text File  |  1993-01-05  |  2.9 KB  |  85 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!usc!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!mksol!mksol!mavmav
  3. From: mavmav@mksol.dseg.ti.com (michael a vincze)
  4. Subject: Re: Scramble text files
  5. Message-ID: <1993Jan5.185410.3773@mksol.dseg.ti.com>
  6. Sender: mavmav@mksol (michael a vincze)
  7. Nntp-Posting-Host: localhost
  8. Organization: Texas Instruments
  9. References: <726193861.AA01015@contrast.wlink.nl> <1993Jan5.082329.13550@ncsu.edu>
  10. Date: Tue, 5 Jan 1993 18:54:10 GMT
  11. Lines: 72
  12.  
  13. In article <726193861.AA01015@contrast.wlink.nl> berend@contrast.wlink.nl
  14. (Berend de Boer) writes:
  15. |> 
  16. |> Can anyone point me to some code that will scramble a text
  17. |> file? It could something that woudl do it on the fly (my
  18. |> program is creating the text to be appended to said file),
  19. |> or something I can churn the text file through later on.
  20. |> This doesn't hafta be fancy (DES etc. would be overkill,) I
  21. |> only need to keep the average user from using a text editor
  22. |> to view/modify  this file.
  23.       
  24. Each character in a text file (or any DOS file for that matter) is represented
  25. by a single byte.  Therefore swapping low and high bytes would not work, unless
  26. you are talking about swapping adjacent characters that could easily be decoded
  27. by the user (just like pig Latin.)  Swapping low and high order nibbles (4
  28. bits) would work.  Also xor-ing with the file name would not work since the
  29. user may change the file name.  Also a file name is comprised of several bytes,
  30. whereas a character of only one, unless you are talking about xor-ing a
  31. number of bytes with the file name. But then you have to keep track of
  32. string lengths.
  33.  
  34. A solution I would like to propose is to add an incremental offset to each
  35. character.  The offset would start with 0 and increment to 255, then wrap-
  36. around back to 0.  The first character would recieve the first offset, the
  37. second character the second offset, etc.  Here is some psuedo code:
  38.  
  39. var I;Byte C: Byte;
  40. var InFile, OutFile: Binary;
  41.  
  42. /* Encrypt */
  43. begin
  44. I := 0;
  45. Open_Infile;
  46. Open_OutFile;
  47. while not End_of_File (InFile) do
  48.     begin
  49.     Read (InFile, C);
  50.     Inc (C, I);
  51.     Inc (I);
  52.     Write (OutFile, C);
  53.     end;
  54. end;
  55.  
  56. /* Decrpyt */
  57. begin
  58. I := 0;
  59. while not End_of_File (InFile) do
  60.     begin
  61.     Read (InFile, C);
  62.     Dec (C, I);
  63.     Dec (I);
  64.     Write (OutFile, C);
  65.     end;
  66. end;
  67.  
  68. Of course there are gaps in the psuedo code, but I believe you can fill them
  69. in.  Furhter I am assuming that a Byte type variable wraps (ie: 255+1 = 0, and
  70. 0-1 = 255.)  I do not have my Pascal compiler in front of me so I can not
  71. verify this.  If this is not the case then change Inc (C, I); Inc (I); to:
  72.  
  73.     if C+I > 255 then C := C+I-256 else Inc (C, I);
  74.     if I   = 255 then I := 0       else Inc (I);
  75.  
  76. and change Dec (C, I); Dec (I); to:
  77.  
  78.     if C-I < 0 then C := C-I+256 else Dec (C, I);
  79.     if I   = 0 then I := 255     else Dec (I);
  80.  
  81. Get in touch with me if you any other questions or comments.
  82.  
  83. Michael Vincze
  84. mav@asd470.dseg.ti.com
  85.