home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
back2roots/padua
/
padua.7z
/
padua
/
misc
/
encrypt.lzh
/
Encrypt.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-03-07
|
8KB
|
248 lines
/*-----------------------------Encrypt V1.00-----------------------------*/
/*---------------------------by Dave Schreiber---------------------------*/
/* */
/*Encrypt V1.00 is Copyright (c)1990 by Dave Schreiber. All Rights Reserved*/
/*Encrypt must by freely distributed, except for costs associated with */
/*shipping, copying, media, labels, taxes, and other necessary costs. */
/*************************************************************************/
/*Compiled under Lattice C V5.02. To compile, use this command line: */
/* 1> lc -Lm Encrypt */
/*************************************************************************/
/*Encrypt is a file encryptor/decryptor. It will take any file (binary, */
/*text, IFF, whatever) and encrypts it using a user specified password. */
/*This renders the file unusable until it is decrypted with the same */
/*password. */
/*************************************************************************/
/*The algorithm goes something like this: */
/* Multiply the ASCII values of the user supplied password together */
/* Use as a seed for the drand48() function (Lattice's pseudo-random */
/* number generator) */
/* To encrypt, for each byte of the file: */
/* Read in the byte */
/* Add it to the value returned by drand48() (which is btw 0 & 255) */
/* Write the byte out */
/* To decrypt, follow the same procedure, but subtract the value */
/* returned by drand48() from the byte. */
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
/*System #include files*/
#include <math.h>
#include <exec/types.h>
#include <exec/exec.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <ctype.h>
void InterpretCommandLine();
void PutByte();
main(argc,argv)
int argc;
char *argv[];
{
ULONG seedvalue;
double value;
UBYTE final;
char filename[200],outfn[201];
struct FileHandle *outfile,*infile;
BYTE buffer[2],encrypt,replace;
if(argv[1][0]=='?') /*If the user requests help*/
{
puts("Encrypt V1.00 (c)1990 by Dave Schreiber");
puts("");
puts("Usage:");
puts(" 1> Encrypt [options] -p<passwd> <infile> [<outfile>]");
puts("Where the options are:");
puts(" -e -- Encrypt <infile> and store as <outfile> (default)");
puts(" -d -- Decrypt <infile> and store as <outfile>");
puts(" -r -- Replace: encrypt/decrypt <infile> and store");
puts(" as <infile> (<outfile> is not needed with this");
puts(" option)");
puts("");
puts(" -p<paswd> -- Use <passwd> as the password");
exit(0);
}
/*Process the command line arguments*/
InterpretCommandLine(argc,argv,filename,outfn,&encrypt,&replace,
&seedvalue);
/*Open the input file*/
if((infile=(struct FileHandle *)Open(filename,MODE_OLDFILE))==NULL)
{
puts("Couldn't open the read file!");
exit(500);
}
/*Open the output file*/
if((outfile=(struct FileHandle *)Open(outfn,MODE_NEWFILE))==NULL)
{
puts("Couldn't open the write file!");
Close(infile); /*Clean up*/
exit(600);
}
if(encrypt) /*Encrypt...*/
{
printf("Encrypting...\n");
while(!GetByte(infile,buffer))
{
value=drand48(); /*Get a random number*/
final=(value*256); /*Reduce it to >=0 and <=255*/
buffer[0]+=final; /*Add it to the read byte*/
PutByte(outfile,buffer,FALSE); /*Write to disk*/
}
}
else /*Decrypt...*/
{
printf("Decrypting...\n");
while(!GetByte(infile,buffer))
{
value=drand48();
final=(value*256);
buffer[0]-=final;
PutByte(outfile,buffer,FALSE);
}
}
PutByte(outfile,buffer,TRUE); /*End of file...*/
Close(outfile); /*Close the files*/
Close(infile);
if(replace) /*If the -r switch was used, delete the .tmp file*/
DeleteFile(filename);
exit(0);
}
/*These next two routines are buffers for the AmigaDOS Write() and */
/*Read() commands. Instead of writing out each byte at a time, bytes */
/*are written out in packets of 128. This can lead to tremendous */
/*speed increases (one program I wrote got a speedup of 400% by using */
/*GetByte alone). Use to your heart's content... */
/*P.S. The important thing with these routines is that I/O is buffered*/
/*not the amount of buffer. I tried increasing the buffer size to 8K */
/*and got no noticable speed increase... */
void PutByte(outfile,buffer,EOF)
char *buffer;
struct FileHandle *outfile;
BYTE EOF;
{
static char chip Buffer[128];
static curpos=0; /*Next space for byte*/
if(EOF) /*If its the end of the file*/
{
Write(outfile,Buffer,curpos); /*Flush the buffer*/
return;
}
Buffer[curpos++]=buffer[0]; /*Add a byte to the buffer*/
if(curpos==sizeof(Buffer)) /*If the buffer is full*/
{
curpos = 0;
Write(outfile,Buffer,sizeof(Buffer)); /*Write to disk*/
}
}
GetByte(infile,buffer)
char *buffer;
struct FileHandle *infile;
{
static char chip Buffer[128];
static curpos=0; /*Next byte to be returned*/
static end = 0; /*Last byte in buffer*/
if(curpos == end) /*If we've sent every byte*/
{ /*Get another 8K chunk*/
curpos = 0;
end=Read(infile,Buffer,sizeof(Buffer));
}
buffer[0]=Buffer[curpos++];
return(!end); /*Returns whether or not EOF*/
}
/*Interpets the command line arguments given to Encrypt*/
void InterpretCommandLine(argc,argv,in,out,encr,repl)
int argc;
char *argv[],*in,*out;
BYTE *encr,*repl;
{
ULONG seed;
BYTE seedhasvalue=FALSE;
BYTE c,i;
*encr=TRUE; /*Encrypt is the default*/
*repl=FALSE; /*Default is not to replace the original file*/
in[0]=out[0]=NULL;
for(c=1;(argv[c][0]=='-') ;c++)
{
switch(argv[c][1])
{
case 'e': /*Encrypt*/
case 'E':
*encr=TRUE;
break;
case 'd': /*Decrypt*/
case 'D':
*encr=FALSE;
break;
case 'p': /*Password*/
case 'P':
if(argv[c][2]==NULL)
break;
/*Multiply characters of the password together*/
for(i=2,seed=1;i < strlen(argv[c]);seed*=argv[c][i++]);
srand48(seed); /*Use the result and the random number seed*/
seedhasvalue=TRUE; /*We've got the seed*/
break;
case 'r':
case 'R': /*Replace un-encrypted version with*/
*repl=TRUE; /*encrypted version*/
break;
}
}
if(argc-c < 2 && !*repl) /*-r not set and filename(s) not given*/
{
puts("Please specify the input and output filenames!");
exit(310);
}
if(argc-c < 1 && *repl) /*-r set and filename not given*/
{
puts("Please specify the input filename!");
exit(320);
}
if(!seedhasvalue) /*If a password wasn't given*/
{
puts("Please specify a password!");
exit(100);
}
strcpy(in,argv[c]);
if(!*repl) /*If -r was used, use the filenames given*/
strcpy(out,argv[++c]);
else /*Otherwise add a .tmp to the original version's filename*/
{ /*and use the originals version's old filename as the output*/
strcpy(out,in); /*filename*/
strcat(in,".tmp"); /*The original, .tmp version, will be deleted*/
if(!Rename(out,in)) /*when the encrypted file has been written*/
{ /*sucessfully*/
puts("Couldn't access the file!");
exit(200);
}
}
}
/*End of Encrypt.c*/