Code:
#include <stdio.h>
#include <sys\stat.h> // fucking slashes just HAVE to go the wrong way
#include <sys\types.h>
#include <fcntl.h>
// #include <unistd.h> // dos doesn't use this shit
// uncomment all the includes below this if not dos
#include <io.h>
#include <string.h>
#include <stdlib.h>
#define DEBUG 0
#define VERSION "1.0"
int dc(char *opt,char *ifile,char *ofile,int rwsize,int keys,char *keystr);
int main(int argc,char *argv[])
{
int status;
int keys;
printf("This is haxor41789's crappy little encryption program v%s (3/19/01).\n\n",VERSION);
if(argc!=6)
{
printf("Usage: %s [e|d] infile outfile rwsize key-string\n",argv[0]);
return -1;
}
keys=((strlen(argv[5])+1)/4);
if(keys>5000)
{
printf("More than 5000 keys are unsupported in the DOS version... how surprising.\n");
return -5;
}
if(DEBUG==1)
{
printf("Keys found: %i\n",keys);
}
if(atoi(argv[4])<2)
{
printf("Warning: rwsize too small, setting to minimum of 2 bytes.\n");
strcpy(argv[4],"2");
}
else if(atoi(argv[4])>1024)
{
printf("rwsizes of greater than 1024 bytes are unsupported in the DOS version, setting to max of 1024.\n");
strcpy(argv[4],"1024");
printf("Attempting to encode %s with %i keys...\n",argv[2],keys);
status=dc(argv[1],argv[2],argv[3],atoi(argv[4]),keys,argv[5]);
if(status == 0)
{
printf("[%s] %s->%s [ok]\n",argv[1],argv[2],argv[3]);
return 0;
}
else if(status==-2)
{
printf("Can't open infile %s.\n",argv[2]);
return -2;
}
else if(status==-3)
{
printf("Can't open outfile %s.\n",argv[3]);
return -3;
}
else
{
printf("Unknown error code, exiting.\n");
return -4;
}
}
int dc(char *opt,char *ifile,char *ofile,int rwsize,int keys,char *keystr)
{
char *keyptr;
int k,x,i=1,fiptr,foptr;
char indata[1024],outdata[1024];
int key_array[5000],rrs=rwsize-1;
unsigned char c;
if(DEBUG==1)
{
printf("RRS: %i\n",rrs);
}
fiptr=_open(ifile,_O_RDONLY|_O_BINARY);
if(fiptr==-1)
{
return -2;
}
foptr=_open(ofile,_O_CREAT|_O_WRONLY|_O_BINARY,_S_IWRITE);
if(foptr==-1)
{
return -3;
}
keyptr=strtok(keystr,"-");
for(k=0;k<keys;k++)
{
key_array[k]=atoi(keyptr);
if(DEBUG==1)
{
printf("Assigning %i to key_array[%i]\n",key_array[k],k);
}
keyptr=strtok(NULL,"-");
}
k=0;
while(i)
{
memset(indata,0,rwsize);
memset(outdata,0,rwsize);
i=_read(fiptr,indata,rrs);
if(i==0)
{
break;
}
indata[i+1]='\0';
if(DEBUG==1)
{
printf("Read %i bytes\n",i);
}
if(DEBUG==1)
{
printf("Indata: %s\n",indata);
}
for(x=0;x<i;x++)
{
c=indata[x];
if(strncmp(opt,"e",1)==0)
{
if(k>keys-1)
{
k=0;
}
if(DEBUG==1)
{
printf("ENC(B): c=%i k=%i\n",c,k);
}
c+=key_array[k];
if(DEBUG==1)
{
printf("ENC(A): c=%i k=%i key_array[%i] = %i\n",c,k,k,key_array[k]);
}
k++;
}
else if(strncmp(opt,"d",1)==0)
{
if(k>keys-1)
{
k=0;
}
if(DEBUG==1)
{
printf("DEC(B): c=%i k=%i\n",c,k);
}
c-=key_array[k];
if(DEBUG==1)
{
printf("DEC(A): c=%i k=%i key_array[%i] = %i\n",c,k,k,key_array[k]);
}
k++;
}
outdata[x]=c;
if(DEBUG==1)
{
printf("Outdata: %s\n",outdata);
}
}
_write(foptr,outdata,i);
}
_close(fiptr);
_close(foptr);
return 0;
}
Bookmarks