home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / 554 / JUIN / ENDECODE.PAS < prev    next >
Pascal/Delphi Source File  |  1993-10-07  |  2KB  |  67 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 334 of 374
  3. From : Bernie Pallek                       1:247/128.0          29 May 93  19:38
  4. To   : Bernie Wilt
  5. Subj : Menu Program!
  6. ────────────────────────────────────────────────────────────────────────────────
  7. BW> I am needing to write a menu program that will allow a user
  8. BW> (offline - nonBBS to view selected text files.  I'd like for the
  9. BW> text files to be encrypted, s BW>that they may only be viewed within
  10. BW> the program.
  11.  
  12. BW>~Bernie Wilt~
  13.  
  14. Hey, cool first name!  :^)
  15.  
  16. Of the trillions of encryption methods, one of the simplest is XOR.
  17.  
  18. Example:
  19. ~~~~~~~ }
  20. PROGRAM EncodeDecode;
  21.  
  22. USES Crt, Dos;
  23.  
  24. CONST
  25.      Key = #254;   { the "key" to the encypt/decrypt alogirthm }
  26.  
  27. VAR
  28.    ch        : Char;   { just a char for reading keys }
  29.    message   : String; { a message to be encrypted }
  30.    cryptFile : Text;   { this don't *have* to be global }
  31.                        { but it's easier this way, 'cuz }
  32.                        { you can use them anywhere...   }
  33.  
  34. PROCEDURE WriteEncrpytedFile;
  35. VAR
  36.    i : Integer;
  37. BEGIN
  38.      Rewrite(cryptFile);
  39.      FOR i := 1 TO Length(message) DO BEGIN
  40.         Write(cryptFile, Chr(Ord(message[i]) XOR Key));
  41.         { if the above line is confusing, quote it to me }
  42.         { and I'll explain it in detail                  }
  43.      END;
  44.      Close(cryptFile);
  45. END;
  46.  
  47. PROCEDURE ReadEncyptedFile;
  48. VAR
  49.    i : Integer;
  50.    c : Char;
  51. BEGIN
  52.      Reset(cryptFile);
  53.      REPEAT
  54.            Read(cryptFile, c);
  55.            Write(Chr(Ord(c) XOR Key));
  56.      UNTIL Eof(cryptFile);
  57.      WriteLn;
  58. END:
  59.  
  60. BEGIN
  61.      ClrScr;
  62.      Assign(cryptFile, 'CRYPTED.DAT');
  63.      message := 'You cannot read this message without decrypting it!');
  64.      WriteEncryptedFile;
  65.      ReadEncryptedFile;
  66.      WriteLn;  Write('Press any key...');  ch := ReadKey; WriteLn;
  67. END.