/*must be linked with the math library, i.e   lc -Lm crypt.c*/
#include <stdio.h>
#include <math.h>
#include <fcntl.h>

void setcrypt(string)
	/*not a horribly intelligent way of making a seed, but it'll do*/ 
	unsigned char *string;
	{
	long seed = 0;
	unsigned char *ptr;
	for (ptr=string; *ptr; ptr++) seed = seed*2 + *ptr;
	srand48(seed);
	}

unsigned char next()
	/*get next random character*/
	{
	union res
		{
		long x;	
		unsigned char c[4];
		} result;
	unsigned char nextchar;
	result.x = mrand48();
	nextchar = result.c[0] + result.c[1] + result.c[2] + result.c[3];
	return(nextchar);
	}


void main(argc,argv)
	int argc;
	char *argv[];
	{
	char c,key[21];
	int infile, outfile;
	if (argc < 3) {puts("crypt: usage is   crypt infile outfile"); exit(20);}
	infile = open(argv[1],O_RDONLY,0);
	if(poserr(argv[1])) exit(20);
	outfile = creat(argv[2],0);
	if(poserr(argv[2])) exit(20);
	key[20] = '\0'; /*just in case he types too long a string*/
	puts("Enter encryption/decryption key");
	fflush(stdout);
	if (!fgets(key,20,stdin) || key[0] == '\n') exit(10);
	setcrypt(key);
	while(1 == read(infile,&c,1))
		{
		c ^= next();
		write(outfile,&c,1);
		}
	close(infile);			
	close(outfile);
	}
