home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / CLDEC87.ZIP / CRYPTBAS.LTG < prev    next >
Encoding:
Text File  |  1987-11-23  |  4.4 KB  |  190 lines

  1. Crypt listing 1
  2.  
  3. 10 REM
  4. 20 REM A quick random number coding device
  5. 30 REM
  6. 40 INPUT "File to be coded:";C$
  7. 50 INPUT "File for output:";D$
  8. 60 OPEN C$ FOR INPUT AS #1
  9. 70 OPEN D$ FOR OUTPUT AS #2
  10. 80 INPUT "Key:";K
  11. 90 RANDOMIZE K
  12. 100 INPUT "Encode or Decode";A$
  13. 110 A$=LEFT$(A$,1)
  14. 120 IF A$="E" OR A$="e" THEN A=1: GOTO 180
  15. 130 IF A$="D" OR A$="d" THEN A=-1 ELSE 100
  16. 140 REM
  17. 150 REM This is the Q$, set up for text files. R is the length
  18. 160 REM R1 is the length minus 1 computed now to save repeated calculation
  19. 170 REM
  20. 180 Q$="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  21. 190 Q$=Q$+"1234567890!@#$%^&*()-=_+[]{};:`~,./<>?|\ '"+CHR$(34)
  22. 200 R=LEN(Q$)
  23. 210 R1=R-1
  24. 220 REM
  25. 230 REM Begin here
  26. 240 REM
  27. 250 IF EOF(1) THEN 490
  28. 260 X$=INPUT$(1,#1)
  29. 270 L=INSTR(Q$,X$)
  30. 280 IF L=0 THEN Y$=X$: GOTO 410
  31. 290 REM
  32. 300 REM Find a random number
  33. 310 REM
  34. 320 GOSUB 440
  35. 330 Z%=INT(R*Q/256)
  36. 340 T=L+(A*Z%)
  37. 350 REM
  38. 360 REM Adjust the parameters to be between 1 and r
  39. 370 REM
  40. 380 IF T<=0 THEN T=T+R1
  41. 390 IF T>=R THEN T=T-R1
  42. 400 Y$=MID$(Q$,T,1)
  43. 410 PRINT #2,Y$;
  44. 420 PRINT X$,X,Y$,T
  45. 430 GOTO 250
  46. 440 REM
  47. 450 REM Random number generator section. Output a value between 0-255 in Q
  48. 460 REM
  49. 470 Q=INT(256*RND)
  50. 480 RETURN
  51. 490 CLOSE #1,#2
  52. 500 END
  53.  
  54. crypt listing 2
  55.  
  56. è10 REM
  57. 20 REM A cellular automata machine
  58. 30 REM
  59. 40 DEFINT A-Z
  60. 50 W=60
  61. 60 DIM R(256)
  62. 70 DIM A(1000,2)
  63. 80 Y=1:Y1=0
  64. 90 INPUT "Key:";K$
  65. 100 REM
  66. 110 REM Place the key in array by converting it to ASCII code
  67. 120 REM
  68. 130 FOR X=1 TO LEN(K$)
  69. 140 H$=MID$(K$,X,1)
  70. 150 A(X,Y1)=ASC(H$)
  71. 160 NEXT X
  72. 170 REM
  73. 180 REM Main section of the program
  74. 190 REM
  75. 200 REM Begin by getting a random number between 0 and 255
  76. 210 REM
  77. 220 GOSUB 550
  78. 230 R(Q)=R(Q)+1
  79. 240 REM
  80. 250 REM Print out the array
  81. 260 REM
  82. 270 FOR X=0 TO W+1
  83. 280 Z=A(X,Y1)
  84. 290 IF Z<32 THEN Z=32
  85. 300 PRINT CHR$(Z);
  86. 310 NEXT X
  87. 320 PRINT
  88. 330 REM
  89. 340 REM Print out the statistics if a key is touched
  90. 350 REM
  91. 360 A$=INKEY$
  92. 370 IF A$="" THEN 200
  93. 380 REM First the individual occurences
  94. 390 E#=0!
  95. 400 FOR X=0 TO 255
  96. 410 E#=E#+R(X)
  97. 420 PRINT CHR$(X);R(X),
  98. 430 NEXT X
  99. 440 E1#=E#/256!
  100. 450 Q#=0!
  101. 460 REM Next calculate the chi-squared and print it out 
  102. 470 FOR X=0 TO 255
  103. 480 Q#=Q#+ABS(R(X)-E1#)^2
  104. 490 NEXT X
  105. 500 PRINT "Difference:";Q#
  106. 510 PRINT
  107. 520 PRINT "Difference per time step:";Q#/E#
  108. 540 GOTO 220
  109. 550 REM
  110. 560 REM Random number subroutine. Updates cells and outputs Q
  111. è570 REM
  112. 580 FOR X=1 TO W
  113. 590 A(X,Y)=A(X-1,Y1) XOR (A(X,Y1) OR A(X+1,Y1))
  114. 600 NEXT X
  115. 610 REM Complete the circle by updating the ends
  116. 620 A(0,Y)=A(W+1,Y1) XOR (A(0,Y1) OR A(1,Y1))
  117. 630 A(W+1,Y)=A(W,Y1) XOR (A(W+1,Y1) OR A(0,Y1))
  118. 640 Q=A(0,Y)
  119. 650 K=Y:Y=Y1:Y1=K
  120. 660 RETURN
  121.  
  122. crypt listing 3
  123.  
  124. 10 REM
  125. 20 REM A cellular automata coding machine
  126. 30 REM
  127. 40 DEFINT A-Z
  128. 50 W=30
  129. 60 DIM R(256)
  130. 70 DIM A(1000,2)
  131. 80 Y=1:Y1=0
  132. 90 INPUT "Key:";K$
  133. 100 REM
  134. 110 REM Place the key in array by converting it to ASCII code
  135. 120 REM
  136. 130 FOR X=1 TO LEN(K$)
  137. 140 H$=MID$(K$,X,1)
  138. 150 A(X,Y1)=ASC(H$)
  139. 160 NEXT X
  140. 170 INPUT "File to be coded:";C$
  141. 180 INPUT "File for output:";D$
  142. 190 OPEN C$ FOR INPUT AS #1
  143. 200 OPEN D$ FOR OUTPUT AS #2
  144. 210 INPUT "Encode or Decode";A$
  145. 220 A$=LEFT$(A$,1)
  146. 230 IF A$="E" OR A$="e" THEN A=1: GOTO 300
  147. 240 IF A$="D" OR A$="d" THEN A=-1 ELSE 210
  148. 250 REM
  149. 260 REM This is the Q$, set up for text files. R is the length
  150. 270 REM r1 is the length minus 1 computed now
  151. 280 REM to save repeated calculations
  152. 290 REM
  153. 300 Q$="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  154. 310 Q$=Q$+"1234567890!@#$%^&*()-=_+[]{};:`~,./<>?|\ '"+CHR$(34)
  155. 320 R=LEN(Q$)
  156. 330 R1=R-1
  157. 340 REM
  158. 350 REM Begin here
  159. 360 REM
  160. 370 IF EOF(1) THEN 560
  161. 380 X$=INPUT$(1,#1)
  162. 390 L=INSTR(Q$,X$)
  163. 400 IF L=0 THEN Y$=X$: GOTO 530
  164. 410 REM
  165. 420 REM Find a random number
  166. è430 REM
  167. 440 GOSUB 580
  168. 450 Z%=INT(R*Q/256)
  169. 460 T=L+(A*Z%)
  170. 470 REM
  171. 480 REM Adjust the parameters to be between 1 and r
  172. 490 REM
  173. 500 IF T<=0 THEN T=T+R1
  174. 510 IF T>=R THEN T=T-R1
  175. 520 Y$=MID$(Q$,T,1)
  176. 530 PRINT #2,Y$;
  177. 540 PRINT X$,L,Y$,T
  178. 550 GOTO 370
  179. 560 CLOSE #1,#2
  180. 570 END
  181. 580 REM
  182. 590 REM Random number subroutine. Updates cells and outputs Q
  183. 600 REM
  184. 610 FOR X=1 TO W
  185. 620 A(X,Y)=A(X-1,Y1) XOR (A(X,Y1) OR A(X+1,Y1))
  186. 630 NEXT X
  187. 640 REM Complete the circle by updating the ends
  188. 650 A(0,Y)=A(W+1,Y1) XOR (A(0,Y1) OR A(1,Y1))
  189. 660 A(W+1,Y)=A(W,Y1) XOR (A(W+1,Y1) OR A(0,Y1))
  190. 670 Q=A(0,Y)
  191. 680 K=Y:Y=Y1:Y1=K
  192. 690 RETURN
  193.