[52579] in Cypherpunks

home help back first fref pref prev next nref lref last post

apology - here is C source code

daemon@ATHENA.MIT.EDU (Jack Mott)
Sun Mar 24 17:53:58 1996

Date: Sun, 24 Mar 1996 18:00:36 -0500
From: Jack Mott <thecrow@iconn.net>
To: cypherpunks@toad.com

I apologize to everyone for the binary posting, I figured it was small=20
enough to not cause problems.  Here is the source.  I do not claim to be=20
either a good programer, or any good at cryptography.  I am a junior in=20
high school and I am interested in it, and I think I have some good=20
ideas here.  The code may be kinda hellish, any CONSTRUCTIVE criticism=20
would be appreciated. I have been programming about a year.  I can=20
assure you though, this isn't some stupid XOR encryption...at least it=20
is better than WordPerfect ecryption (I hope to god :) =20
	I originally wrote this in Pascal, and ported it to Turbo C++, I=20
think the only C++ in it is the comments and inline declarations, might=20
want to double check though. I think the only non-portable code is the=20
gotoXY statements, and the random function works differently under GCC=20
if I remember correctly (I have had Linux for a little while) Oh yeah, I=20
kinda stretched the definition of HASH in my code a lot... here goes.

---------------
// encryption program - constructive flames appreciated

//=20
***************************INCLUDES************************************	#=
include <stdio.h>
        #include <sys\stat.h>  // <-- dont need this in Unix
	#include <fcntl.h>
	#include <io.h>
	#include <math.h>
	#include <conio.h>
	#include <stdlib.h>
	#include <string.h>
	#define BLOCK 16384
	#define HASHSIZE 256
//=20
***************************PROTOTYPES************************************
	void input(void);    		// Input KEY, FILENAMES, etc.
	void openfiles(void);		// Open files
	void closefiles(void);		// Close em
	void encrypt(void);		   // Encrypt file
	void decrypt(void);
	void superhash(void);	   // Time/System/File/Specifics
	void initblocks(void);
	void grabhash(void);	      // Grabs encrypted hash value from=20
file
	void CopyBack(void);
//=20
***************************VAR*****************************************	c=
har 		key[80];
	char 		ifn[12];
	char 		ed[1];
	int  		infile,outfile,backfile;
	int  		x,y;
	int  		i;
	float 	Havg,Kavg,BKHavg;
	char     SuperHash[HASHSIZE],ESuperHash[HASHSIZE];
	char     BSH[BLOCK],KEY[BLOCK];


//***********************************************************************=
***//
//                        Main Program                                 =20
    //
//***********************************************************************=
***//
main(int argc,char* argv[]) {
randomize();

if (argc < 4)
	{
		input();
	}
else
	{
	  strcpy(ifn,argv[1]);
	  strcpy(ed,argv[2]);
	  strcpy(key,argv[3]);
	}

openfiles();

if (ed[0] =3D=3D (('e') | ('E')) )
	{
		superhash();
		initblocks();
		encrypt();
	}
else if (ed[0] =3D=3D (('d') | ('D')) )
  {
		grabhash();
		initblocks();
		decrypt();
		CopyBack();
  }
else
	printf("%s was not an option",ed[0]);

closefiles();
}

//***********************************************************************=
***//
// input()              Takes in all inputs                            =20
    //
//***********************************************************************=
***//
void input()
{
	char 	  vkey[80];
	char    c;

	for (i =3D 0; i < 80; i++)
		{
			key[i] =3D '';
			vkey[i] =3D '';
		}

	printf("\nEnter Filename:");
	gets(ifn);
	printf("(E)ncrypt or (D)ecrypt?:");
	gets(ed);

	printf("Enter Key:");
	i =3D 0;
	int startx =3D wherex();
	do {
		if (kbhit())  {
			 c =3D getch();

			 if (c =3D=3D 8)
			 {
				if (wherex() !=3D startx)
					{
						key[i] =3D '';
						i -=3D 1;
						key[i] =3D '';
					=09
gotoxy(wherex()-1,wherey());
						printf(" ");
					=09
gotoxy(wherex()-1,wherey());

					}
			 }
			 else
				{
					if (c !=3D 13)
						{
							key[i] =3D c;
							i++;
							printf("=FE");
						}
					else
						break;
				}

			 }
		} while (c !=3D 13);

	flushall();
	printf("\nVerify key:");
	startx =3D wherex();
	i =3D 0;
	c =3D 0;
	do {
		if (kbhit())  {
			 c =3D getch();

			 if (c =3D=3D 8)
			 {
				if (wherex() !=3D startx)
					{
                  vkey[i] =3D '';
						i -=3D 1;
                  vkey[i] =3D '';
					=09
gotoxy(wherex()-1,wherey());
						printf(" ");
					=09
gotoxy(wherex()-1,wherey());

					}
			 }
			 else
				{
					if (c !=3D 13)
						{
							vkey[i] =3D c;
							i++;
							printf("=FE");
						}
					else
						break;
				}

			 }
		} while (c !=3D 13);

	if (strcmp(key,vkey) !=3D 0)
		{
			printf("\nKeys were not the same. They need to=20
be");
			exit(EXIT_SUCCESS);
		}
	if (strlen(key) <=3D 0)
		{
			printf("\nThe key was blank, it needs to have=20
some characters.");
			exit(EXIT_SUCCESS);
		}
printf("\n");
}

//***********************************************************************=
***//
// openfiles(void) --       Opens all files                            =20
    //
//***********************************************************************=
***//
void openfiles(void)
{
infile  =3D open(ifn,O_BINARY | O_RDONLY);
if (ed[0] =3D=3D (('e') | ('E')) )
	{
	  outfile =3D open(ifn,O_BINARY | O_WRONLY);
	}
else
	{
		  chmod("KRYPT000.TMP",S_IWRITE);
		  unlink("KRYPT000.TMP");
		  outfile =3D open("KRYPT000.TMP", O_BINARY | O_RDWR |=20
O_CREAT | O_TRUNC | S_IWRITE);
	}

}

//***********************************************************************=
***//
//                       Closes All Files                              =20
    //
//***********************************************************************=
***//
void closefiles(void)
{
if (ed[0] =3D=3D (('e') | ('E')) )
  {
	close(outfile);
	close(infile);
  }
else
	{
		close(backfile);
		close(infile);
	}

}


//***********************************************************************=
***//
//                      Main Encryption Routine                        =20
    //
//***********************************************************************=
***//
void encrypt(void)
{
char buf[BLOCK];
float blocks =3D 0;
long sizeoffile =3D filelength(infile);
long numread;
long maxblocks =3D sizeoffile / BLOCK;
if (maxblocks =3D=3D 0) maxblocks =3D 1;
printf("\nEncrypting:");
x =3D wherex();

while ((numread =3D read(infile,buf,BLOCK)) > 0 )
	{
		BKHavg =3D (blocks * Kavg) + Havg;
		for(i =3D 0; i < numread; i++)
			{
			  buf[i] =3D buf[i] + floor(9845845*cos((KEY[i]+
				BSH[i]) * (i+BKHavg)));
			}

		gotoxy(x,wherey());

		int pd =3D floor(blocks/maxblocks*100 + 1);
		printf("%d%",pd);

		write(outfile,buf,numread);
		blocks++;
	}
gotoxy(x,wherey());
printf("100%");
write(outfile,ESuperHash,HASHSIZE);
}
//***********************************************************************=
***//
//                      Copy Back For Decryption                       =20
    //
//***********************************************************************=
***//
void CopyBack(void) {

backfile =3D open(ifn,O_BINARY | O_WRONLY | O_TRUNC);
char buf[BLOCK];

long pos;
long size =3D filelength(outfile) - HASHSIZE;
long blocks =3D 0;
int numread;
long maxblocks =3D size / BLOCK;
if (maxblocks =3D=3D 0) maxblocks =3D 1;

printf("\nRemoving encryption block:");
x =3D wherex();
lseek(outfile, 0L, SEEK_SET);
  do {
	 numread =3D read(outfile,buf,BLOCK);
	 pos =3D tell(outfile);

	 if (pos > size)
		{
		  write(backfile, buf,numread - (pos - size));
		  break;
		}
	 else
		{
		  write(backfile, buf, numread);
		}

	 blocks =3D blocks+1;
	 gotoxy(x,y);
	 int pd =3D floor(blocks/maxblocks*100 + 1);
	 printf("%d%   ",pd);

  } while (pos !=3D size);
  close(outfile);
  chmod("KRYPT000.TMP",S_IWRITE);
  unlink("KRYPT000.TMP");


}
//***********************************************************************=
***//
//                      Main Decryption Routine                        =20
    //
//***********************************************************************=
***//
void decrypt(void) {
char buf[BLOCK];
float blocks =3D 0;
long sizeoffile =3D filelength(infile);
long numread;
long maxblocks =3D sizeoffile / BLOCK;
if (maxblocks =3D=3D 0) maxblocks =3D 1;
printf("\nDecrypting:");
x =3D wherex();

while ((numread =3D read(infile,buf,BLOCK)) > 0 )
	{
		BKHavg =3D (blocks * Kavg) + Havg;
		for(i =3D 0; i < numread; i++)
			{
				buf[i] =3D buf[i] -=20
floor(9845845*cos((KEY[i]+
				  BSH[i])*(i+BKHavg)));

			}

		gotoxy(x,wherey());

		int pd =3D floor(blocks/maxblocks*100 + 1);
		printf("%d%",pd);

		write(outfile,buf,numread);
		blocks++;
	}
gotoxy(x,wherey());
printf("100%");


}
//***********************************************************************=
***//
//                         SuperHash Procedure                         =20
    //
//								=09
								=09
							 //
// the idea here is to get a block of totally unpredictable bytes,     =20
    //
// if anyone has any 'true random number generators' stick em here=09
       //
//	this just pulls in HASHSIZE bytes from random positions from the=20
file,   //
//	the idea being that they would have to know what the whole file=20
was      //
//	in the first place to guess these values                       =20
         //
//								=09
								=09
							 //
//***********************************************************************=
***//
void superhash(void)
{
printf("Generating System/Time/File specific SuperHash:");
x =3D wherex();

	long MaxPos =3D filelength(infile) - 1;
	char hbuf;
	int keylen =3D strlen(key);
Havg =3D 0;
for (i =3D 0; i < HASHSIZE; i++)
	{
			long SeekValue =3D ((93617583 * random(32000)) *=20
key[i % keylen]) % MaxPos;
			if (SeekValue < 0) SeekValue *=3D -1;

			lseek(infile,SeekValue, SEEK_SET);
			read(infile,&hbuf,1);

			SuperHash[i] =3D floor(923723723 * cos(random(256)=20
* hbuf * key[i % keylen]));
			ESuperHash[i] =3D SuperHash[i] + floor(989898989 *=20
sin(key[i % keylen])+i);
			Havg =3D Havg + SuperHash[i];

			printf("%d     ",random(256));
			gotoxy(x, wherey());
	}
if ((key[SuperHash[5] % keylen] + (key[SuperHash[3] % keylen] / 10)) !=3D=
=20
0)
	{
		Havg =3D Havg / (key[SuperHash[5] % keylen] +
			(key[SuperHash[3] % keylen] / 1000));
	}
else
	{
		Havg =3D Havg / 7.2;
	}


lseek(infile, 0L, SEEK_SET);
}

//***********************************************************************=
***//
//grabhash()            Grab ESuperHash from file                      =20
    //
//***********************************************************************=
***//
void grabhash()  {
printf("Grabbing SuperHash Values...");
long sizeoffile =3D filelength(infile);
int keylen =3D strlen(key);

lseek(infile,sizeoffile - HASHSIZE,SEEK_SET);
read(infile,ESuperHash,HASHSIZE);
Havg =3D 0;
for (i =3D 0; i < HASHSIZE; i++)
  {
	 SuperHash[i] =3D ESuperHash[i] - floor(989898989 * sin(key[i %=20
keylen])+i);
	 Havg =3D Havg + SuperHash[i];
  }
Havg =3D Havg / key[SuperHash[5] % keylen];
lseek(infile, 0L, SEEK_SET);
}

//***********************************************************************=
***//
//initblocks()       Dumps small hash arrays into big BLOCK arrays     =20
    //
//***********************************************************************=
***//
void initblocks(void)  {
long total =3D 0;
int keylen =3D strlen(key);

for (i =3D 0; i < keylen; i++)
	{
		total +=3D key[i];
	}

Kavg =3D total / keylen + total;

for (i =3D 0; i < BLOCK; i++)
  {
	 KEY[i] =3D  key[i % keylen];
	 BSH[i]  =3D SuperHash[i % HASHSIZE];
  }



}
----------

seeya - thanks

--=20
thecrow@iconn.net
"It can't rain all the time"

home help back first fref pref prev next nref lref last post