home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume15 / codon / part01 next >
Encoding:
Text File  |  1990-10-05  |  4.6 KB  |  214 lines

  1. Newsgroups: comp.sources.misc
  2. X-UNIX-From: attcan!news
  3. organization: This is the most unorganized person in the world, Ltd
  4. X-moderator-comment: Oh, yeah? ;-)
  5. keywords: simple crypt XOR one time pad random
  6. subject: v15i021: simple encryption algorithm
  7. from: elevia!alain (W.A.Simon)
  8. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  9.  
  10. Posting-number: Volume 15, Issue 21
  11. Submitted-by: elevia!alain (W.A.Simon)
  12. Archive-name: codon/part01
  13.  
  14.     Several persons have requested on the net, or directly through
  15.     E-mail, simple encryption alogorithms.  Here are a few simple
  16.     routines, which for all practical purposes are very secure.
  17.     They will not resist trained crypto-analysts and/or computer
  18.     powered attacks, but casual snoopers will be baffled.  Just
  19.     use good keys.  Here goes :
  20.  
  21. #! /bin/sh
  22. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  23. # Contents:  codon.c
  24. echo extracting 'codon.c'
  25. if test -f 'codon.c' -a -z "$1"; then echo Not overwriting 'codon.c'; else
  26. sed 's/^X//' << \EOF > 'codon.c'
  27. X/*
  28. X
  29. X    codon    -    filter to encode text string;
  30. X            each byte is XOR'd with a byte from a key file.
  31. X            That's one kind of One Time Pad implementation.
  32. X            codon is linked to codoff.
  33. X            
  34. X        W. A. Simon
  35. X*/
  36. X        
  37. X#include <stdio.h>
  38. X#include <ctype.h>
  39. X#include <sys/types.h>
  40. X#include <sys/stat.h>
  41. X
  42. Xmain(argc, argv)
  43. Xint argc; char **argv[];
  44. X
  45. X    {
  46. X        FILE *in;
  47. X        register int c;
  48. X        c = 0;
  49. X        if (argc < 2)
  50. X            exit(1);
  51. X        /*    
  52. X        a file name must be specified on command line,
  53. X        that's the file containing the key stream.
  54. X        for security reasons: no error message.
  55. X        */
  56. X
  57. X        if ((in = fopen(argv[1], "r")) == NULL)
  58. X            exit(1);
  59. X        /*
  60. X        for security reasons: no error message.
  61. X        */
  62. X
  63. X        while (( c = getchar() ) != EOF) {
  64. X            putchar( c ^ getc(in) ); /* ouput coded c */
  65. X        }
  66. X        
  67. X        exit(0);
  68. X    }
  69. X
  70. X
  71. X/*
  72. X    decode    -    filter to decode text encoded with encode;
  73. X            see encode for details.
  74. X            
  75. X        W. A. Simon
  76. X        
  77. X*/
  78. X
  79. X#include <stdio.h>
  80. Xmain()
  81. X    {
  82. X        int c, i;
  83. X        c = 0;
  84. X        i = 153; /* seed is yours to decide - can be anything */
  85. X        while (scanf("%c", &c) != EOF) {
  86. X            c = c ^ i;
  87. X            putchar(c);
  88. X            i = c;
  89. X        }
  90. X        exit(0);
  91. X    }
  92. X
  93. X
  94. X/*
  95. X    encode    -    filter to encode text string.
  96. X            each byte is XOR'd with previous byte.
  97. X            seed is arbitrary.
  98. X            
  99. X        W. A. Simon
  100. X        
  101. X*/
  102. X
  103. X#include <stdio.h>
  104. Xmain()
  105. X    {
  106. X        register int c, i;
  107. X        i = 153;
  108. X        c = 0;
  109. X        while ((c = getchar()) != EOF) {
  110. X            printf("%c",c ^ i);
  111. X            i = c;
  112. X        }
  113. X        exit(0);
  114. X    }
  115. X
  116. X    ... and let's cap it with a simple way to generate healthy
  117. X    random keys :
  118. X
  119. X/*
  120. X
  121. X    confuse
  122. X    
  123. X            For the purpose of cryptological use
  124. X            a good random generator
  125. X            must be unpredictable.
  126. X            A uniform distribution is not
  127. X            necessarily wanted but this
  128. X            distribution must also be unpredictable
  129. X            which in the long run is equivalent to
  130. X            being uniform.
  131. X
  132. X            UNPREDICTABILITY is due to input file
  133. X            which can be anything at all;
  134. X            use of a binary file is advised
  135. X            for optimum results.
  136. X
  137. X            Tendancy towards UNBIASED distribution
  138. X            is due to mixed congruential treatment of matrix
  139. X            (see Knuth's holy book).
  140. X            Any peak in the distribution would be
  141. X            smoothed out by this treatment.
  142. X
  143. X                        W. A. Simon - 1986
  144. X        
  145. X                                    */
  146. X#include <stdio.h>
  147. X#include <ctype.h>
  148. X
  149. Xint matrix[256];
  150. X
  151. Xint a, c, s;
  152. X
  153. Xmain()
  154. X
  155. X{
  156. X
  157. X/*                initialize variables            */
  158. X
  159. X    a = 0;
  160. X    c = 0;
  161. X    s = 1;
  162. X
  163. X/*                initialize matrix            */
  164. Xwhile ( a < 256 ) {
  165. X        c = ( ( c * 21 ) + 13 ) % 256 ;
  166. X        matrix[a] = c ;
  167. X        a++;
  168. X    }
  169. X
  170. X/*
  171. X    UNBIASED:
  172. X        the matrix contains one occurence only
  173. X        of each of the 256 possible 8 bit characters.
  174. X
  175. X    UNPREDICTABLE:
  176. X        read standard input,
  177. X        index into rotated matrix,
  178. X        output entry to stdout.
  179. X        Akin to a Vigeneres table.
  180. X
  181. X    Matrix is rotated at every turn in order to prevent
  182. X    statistically biased input from affecting the results.
  183. X    In fact, repeating the same character over and over,
  184. X    into the stdin, would result in a constant repetition
  185. X    of the matrix on stdout.  A non rotated matrix would
  186. X    result in an output that has the same statistical feel
  187. X    as the input (like a substitution cipher).
  188. X
  189. X    All of this stuff has the same statistical impact as doing
  190. X    an XOR between a data stream and a recurring string of 256
  191. X    non repeating bytes.  I'll let you do the formal proof.
  192. X
  193. X                                    */
  194. X    while ( ( a = getchar() ) != EOF ) {
  195. X        putchar( matrix[ ( a + s ) % 256 ] );
  196. X        s = ( s + 1 ) % 256 ;
  197. X    }
  198. X    exit(0);
  199. X}
  200. EOF
  201. chars=`wc -c < 'codon.c'`
  202. if test $chars !=     3244; then echo 'codon.c' is $chars characters, should be     3244 characters!; fi
  203. fi
  204. exit 0
  205.  
  206.     enjoy,
  207.  
  208.  
  209. -- 
  210.  
  211. Alain
  212.         Home+Office: (514) 934 6320            UUCP: alain@elevia.UUCP
  213.  
  214.