home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / programming / misc_programming / batnmore.z!p / CAESAR.BTM < prev    next >
Text File  |  1992-11-15  |  3KB  |  92 lines

  1. *REM This batch file is Freeware, free to use and redistribute unmodified *
  2. *REM Jouni Miettunen * jon@stekt.oulu.fi * Oulu * Finland * Europe * 1992 *
  3.  
  4. REM Caesar Cipher encryption, default rot13. Look at the end of this file!
  5. REM Timestamp 15-Nov-1992
  6.  
  7. break on^setlocal^unalias *^if %#==0 goto info^set rot=13
  8.  
  9. REM Is there rotation key or do we use default 13
  10. iff %@ascii[%1]==45 then
  11.  if %#==1 goto start
  12.  iff not exist %1 then^set rot=%@eval[%@substr[%1,1,%@len[%1]]%%26]^shift^endiff
  13. endiff
  14.  
  15. :start
  16. iff exist %1 then^gosub work^else^set a=%1^gosub loop^endiff
  17. shift^if "%1" != "" goto start^goto end
  18.  
  19. :work
  20. iff "%@search[pre4rot.exe]"=="" then^echo ----- Cannot find PRE4ROT.EXE^return
  21. set b=%@unique[%temp]^type %1|pre4rot>%b^for %a in (@%b) gosub loop^del/q %b^return
  22.  
  23. :loop
  24. set index=%@eval[%index+1]
  25.  
  26. REM %a has to be in quotation marks in case it includes commas
  27. REM space has to follow %a in case %a is only CR/LF or NL
  28. REM Problem now: double and single quotes, procents in %a (C code at EOF)
  29.  
  30. set x=%@ascii[%@substr["%a ",%index,1]]
  31. iff %x ge 65 .and. %x le 90 then
  32. set x=%@eval[%x+%rot]^if %x gt 90 set x=%@eval[%x-26]
  33.  
  34. elseiff %x ge 97 .and. %x le 122 then
  35. set x=%@eval[%x+%rot]^if %x gt 122 set x=%@eval[%x-26]
  36. endiff
  37.  
  38.     iff %x==32  then^echos ` `
  39. elseiff %x==60  then^echos `<`
  40. elseiff %x==62  then^echos `>`
  41. elseiff %x==94  then^echos `^`
  42. elseiff %x==124 then^echos `|`
  43. elseiff %x==0 then^echos %@substr["%a ",%index,2]^set index=%@eval[%index+1]
  44. else^echos %@char[%x]^endiff
  45.  
  46. if %index lt %@len[%a] goto loop^set index=0^echo.^return
  47.  
  48. :info
  49. echo Usage: %@lower[%@name[%0]] [-key] [string | filename ...]
  50. text
  51.  
  52. One of the simplest and oldest encryption methods: Caesar Cipher
  53.  
  54. -key    integer (1..26) for key rotation
  55.     Default key is 13, when Caesar Cipher is also known as rot13
  56.  
  57. Input can be a string on command line or in a file. If input has single
  58. quotes, double quotes or procent characters, it *has* to be in file.
  59.  
  60. For files uses pre4rot.exe filter (c code included) to place 4DOS escape
  61. character (by default ctrl-x) in front of single quote, double quote and
  62. procent characters. Creates temperature file in TEMP directory.
  63. endtext
  64.  
  65. :end
  66. break off^quit
  67.  
  68. REM Damn it! (Damnant quod non intelligunt)
  69. REM I can change _output_ of _pressed_ char, can do nothing to _input_
  70.  
  71. REM Have to add some C code :-(  And if u need some, then u could have the
  72. REM whole thing written in C.. Well, it almost worked w/only 4DOS commands!
  73.  
  74. : /* pre4rot.c - add 4DOS escape character in front of single quotes, */
  75. : /* double quotes and procent characters - ANSI C code               */
  76. :
  77. : #include <stdio.h>
  78. : void main(void)
  79. : {
  80. : int c;
  81. :     while ((c=getchar()) != EOF) {
  82. :         if ((c==34) || (c==37) || (c==96)) putchar(24);
  83. :         putchar(c);
  84. :     } /*while*/
  85. : } /*void main*/
  86.  
  87. REM problem: what if escape character is something else than ctrl-x?
  88. REM ...I had to use c, so it really doesn't matter any more...  :-(
  89. REM Note: getenv() doesn't find COMPOUND. Has to use 4DOS commands..
  90.