home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sources.misc
- X-UNIX-From: attcan!news
- organization: This is the most unorganized person in the world, Ltd
- X-moderator-comment: Oh, yeah? ;-)
- keywords: simple crypt XOR one time pad random
- subject: v15i021: simple encryption algorithm
- from: elevia!alain (W.A.Simon)
- Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
-
- Posting-number: Volume 15, Issue 21
- Submitted-by: elevia!alain (W.A.Simon)
- Archive-name: codon/part01
-
- Several persons have requested on the net, or directly through
- E-mail, simple encryption alogorithms. Here are a few simple
- routines, which for all practical purposes are very secure.
- They will not resist trained crypto-analysts and/or computer
- powered attacks, but casual snoopers will be baffled. Just
- use good keys. Here goes :
-
- #! /bin/sh
- # This file was wrapped with "dummyshar". "sh" this file to extract.
- # Contents: codon.c
- echo extracting 'codon.c'
- if test -f 'codon.c' -a -z "$1"; then echo Not overwriting 'codon.c'; else
- sed 's/^X//' << \EOF > 'codon.c'
- X/*
- X
- X codon - filter to encode text string;
- X each byte is XOR'd with a byte from a key file.
- X That's one kind of One Time Pad implementation.
- X codon is linked to codoff.
- X
- X W. A. Simon
- X*/
- X
- X#include <stdio.h>
- X#include <ctype.h>
- X#include <sys/types.h>
- X#include <sys/stat.h>
- X
- Xmain(argc, argv)
- Xint argc; char **argv[];
- X
- X {
- X FILE *in;
- X register int c;
- X c = 0;
- X if (argc < 2)
- X exit(1);
- X /*
- X a file name must be specified on command line,
- X that's the file containing the key stream.
- X for security reasons: no error message.
- X */
- X
- X if ((in = fopen(argv[1], "r")) == NULL)
- X exit(1);
- X /*
- X for security reasons: no error message.
- X */
- X
- X while (( c = getchar() ) != EOF) {
- X putchar( c ^ getc(in) ); /* ouput coded c */
- X }
- X
- X exit(0);
- X }
- X
- X
- X/*
- X decode - filter to decode text encoded with encode;
- X see encode for details.
- X
- X W. A. Simon
- X
- X*/
- X
- X#include <stdio.h>
- Xmain()
- X {
- X int c, i;
- X c = 0;
- X i = 153; /* seed is yours to decide - can be anything */
- X while (scanf("%c", &c) != EOF) {
- X c = c ^ i;
- X putchar(c);
- X i = c;
- X }
- X exit(0);
- X }
- X
- X
- X/*
- X encode - filter to encode text string.
- X each byte is XOR'd with previous byte.
- X seed is arbitrary.
- X
- X W. A. Simon
- X
- X*/
- X
- X#include <stdio.h>
- Xmain()
- X {
- X register int c, i;
- X i = 153;
- X c = 0;
- X while ((c = getchar()) != EOF) {
- X printf("%c",c ^ i);
- X i = c;
- X }
- X exit(0);
- X }
- X
- X ... and let's cap it with a simple way to generate healthy
- X random keys :
- X
- X/*
- X
- X confuse
- X
- X For the purpose of cryptological use
- X a good random generator
- X must be unpredictable.
- X A uniform distribution is not
- X necessarily wanted but this
- X distribution must also be unpredictable
- X which in the long run is equivalent to
- X being uniform.
- X
- X UNPREDICTABILITY is due to input file
- X which can be anything at all;
- X use of a binary file is advised
- X for optimum results.
- X
- X Tendancy towards UNBIASED distribution
- X is due to mixed congruential treatment of matrix
- X (see Knuth's holy book).
- X Any peak in the distribution would be
- X smoothed out by this treatment.
- X
- X W. A. Simon - 1986
- X
- X */
- X#include <stdio.h>
- X#include <ctype.h>
- X
- Xint matrix[256];
- X
- Xint a, c, s;
- X
- Xmain()
- X
- X{
- X
- X/* initialize variables */
- X
- X a = 0;
- X c = 0;
- X s = 1;
- X
- X/* initialize matrix */
- Xwhile ( a < 256 ) {
- X c = ( ( c * 21 ) + 13 ) % 256 ;
- X matrix[a] = c ;
- X a++;
- X }
- X
- X/*
- X UNBIASED:
- X the matrix contains one occurence only
- X of each of the 256 possible 8 bit characters.
- X
- X UNPREDICTABLE:
- X read standard input,
- X index into rotated matrix,
- X output entry to stdout.
- X Akin to a Vigeneres table.
- X
- X Matrix is rotated at every turn in order to prevent
- X statistically biased input from affecting the results.
- X In fact, repeating the same character over and over,
- X into the stdin, would result in a constant repetition
- X of the matrix on stdout. A non rotated matrix would
- X result in an output that has the same statistical feel
- X as the input (like a substitution cipher).
- X
- X All of this stuff has the same statistical impact as doing
- X an XOR between a data stream and a recurring string of 256
- X non repeating bytes. I'll let you do the formal proof.
- X
- X */
- X while ( ( a = getchar() ) != EOF ) {
- X putchar( matrix[ ( a + s ) % 256 ] );
- X s = ( s + 1 ) % 256 ;
- X }
- X exit(0);
- X}
- EOF
- chars=`wc -c < 'codon.c'`
- if test $chars != 3244; then echo 'codon.c' is $chars characters, should be 3244 characters!; fi
- fi
- exit 0
-
- enjoy,
-
-
- --
-
- Alain
- Home+Office: (514) 934 6320 UUCP: alain@elevia.UUCP
-
-