Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!chinacat!woody From: woody@chinacat.Unicom.COM (Woody Baker @ Eagle Signal) Newsgroups: comp.lang.postscript Subject: Re: eexec? Summary: here are the decrypt/encrypt routines Message-ID: <1390@chinacat.Unicom.COM> Date: 10 Jul 90 02:59:37 GMT References: <1223@mtk.UUCP> Distribution: usa Organization: a guest of Unicom Systems Development, Austin Lines: 202 Here are the encrypt and decrypt routines. The versions that handle files, are an adaptation of the other two, as they really did not work correctly under MS-DOS. MS-DOS 'C' attempts to be "unix compatable" and as such, when it sees a 0x0D, it converts it to a linefeed. This has a way of completely messing up the conversion of a binary file that happens to have a 0x0d in it. The file versions avoid it by using the "b" specifier in the file opening code. This allows uncooked binary code. There is one other non-portablilty. the declaration of unsigned short int 0xhhhh; will fail on an 8 bit machine. Short will be a byte, and since we are working with 16 bits here..... #include "stdio.h" /* routine takes an encrypted eexec file and creates a decrypted eexec file. First cut is much simpler than the full decryption would be. Full decryption will handle multiple keys, and have the flag to encrypt or decrypt things. */ FILE *infile,*outfile; main(argc,argv) char *argv[]; int argc; { int first =4; int state=0xD971; int c; char decrptd; if(argc !=3) { printf("usage: decrpt infile outfile\n"); exit(0); }; infile=fopen(argv[1],"rb"); outfile=fopen(argv[2],"wb"); while(fscanf(infile,"%2x",&c)>0) { /* if(first !=0) { first--; } else { */ decrptd= c ^ (state >>8); if(first !=0) { first--; } else { fputc(decrptd,outfile); } state=state + c; state=state * 0xCE6D; state=state + 0x58BF; /* } */ } fclose(infile); fclose(outfile); } /* Quickie to convert postscript from stdin to eexec format on stdout */ #include "stdio.h" static unsigned short buffer = 0xd971; static unsigned long startup = 0xac7252f3; /* 00000000or whatever you want */ main(argc,argv) char *argv[]; int argc; { unsigned char input; unsigned char output; int init = 4; int i; int result; FILE *infile,*outfile; if(argc !=4) { printf("usage: encrypt startkey(long hex) infile outfile\n"); } infile=fopen(argv[2],"r"); outfile=fopen(argv[3],"wb"); sscanf(argv[1],"%lx",&startup); fprintf(outfile,"%08lx",startup); for (i=0;i<4;++i) { input = (startup >> ((3-i)*8)); buffer = (input + buffer) * 0xce6d + 0x58bf; } for(;;) { for (i=0;i<(32-init);++i) { input = result = fgetc(infile); if ((int)result == EOF) break; output = (input ^ (buffer>>8)); buffer = (output + buffer) * 0xce6d + 0x58bf; fprintf(outfile,"%02.2x",output); } init = 0; fprintf(outfile,"\n"); if ((int) result == EOF) break; } fclose(infile); fclose(outfile); } /* Quickie to convert postscript from stdin to eexec format on stdout */ #include "stdio.h" static unsigned short buffer = 0xd971; static unsigned long startup = 0xac7252f3; /* 00000000or whatever you want */ main() { unsigned char input; unsigned char output; int init = 4; int i; int result; printf("%08lx",startup); for (i=0;i<4;++i) { input = (startup >> ((3-i)*8)); buffer = (input + buffer) * 0xce6d + 0x58bf; } for(;;) { for (i=0;i<(32-init);++i) { input = result = getchar(); if ((int)result == EOF) break; output = (input ^ (buffer>>8)); buffer = (output + buffer) * 0xce6d + 0x58bf; printf("%02x",output); } init = 0; printf("\n"); if ((int) result == EOF) break; } } /* Quickie to convert eexec format from stdin to postscript on stdout */ #include /* Written by Carsten Wiethoff 1989 */ /* You may do what you want with this code, as long as this notice stays in it */ static unsigned short buffer = 0xd971; main() { unsigned int input; char output; int ignore = 4; int result; do { result = scanf(" %2x",&input); if ( (result == EOF) || (result == 0) ) break; output = input ^ (buffer>>8); buffer = (input + buffer) * 0xce6d + 0x58bf; if ( ignore > 0 ) { ignore--; } else { printf("%c",output); } } while (1); } Cheers Woody