3 b855148c 2004-05-16 devnull * Adobe's encryption/decryption algorithm for eexec and show. Runs in
4 b855148c 2004-05-16 devnull * eexec mode unless told otherwise. Use,
6 b855148c 2004-05-16 devnull * pscrypt file.cypher > file.clear
8 b855148c 2004-05-16 devnull * to decrypt eexec input. Assumes file.cypher is hex with the key as the
9 b855148c 2004-05-16 devnull * first four bytes, and writes file.clear as binary (omitting the key).
12 b855148c 2004-05-16 devnull * pscrypt -e12ab34ef file.clear >file.cypher
14 b855148c 2004-05-16 devnull * to encrypt file.clear (for eexec) using 12ab34ef as the key. Input is
15 b855148c 2004-05-16 devnull * binary and output is hex. The key must be given as a hex number. Use
16 b855148c 2004-05-16 devnull * -sshow to encrypt or decrypt a CharString or Subr,
18 b855148c 2004-05-16 devnull * pscrypt -sshow file.cypher > file.clear
20 b855148c 2004-05-16 devnull * Use -b or -x to read binary or hex input, and -B or -X to output binary
21 b855148c 2004-05-16 devnull * or hex.
25 b855148c 2004-05-16 devnull #include <stdio.h>
26 b855148c 2004-05-16 devnull #include <ctype.h>
28 b855148c 2004-05-16 devnull #define ENCRYPT 0
29 b855148c 2004-05-16 devnull #define DECRYPT 1
31 b855148c 2004-05-16 devnull #define NOTSET -1
32 b855148c 2004-05-16 devnull #define BINARY 0
33 b855148c 2004-05-16 devnull #define HEX 1
34 b855148c 2004-05-16 devnull #define LINELENGTH 40
36 b855148c 2004-05-16 devnull #define CHARSTRING 4330
37 b855148c 2004-05-16 devnull #define EEXEC 55665
38 b855148c 2004-05-16 devnull #define MAGIC1 52845
39 b855148c 2004-05-16 devnull #define MAGIC2 22719
41 b855148c 2004-05-16 devnull int argc;
42 b855148c 2004-05-16 devnull char **argv;
44 b855148c 2004-05-16 devnull int mode = DECRYPT;
45 b855148c 2004-05-16 devnull int input = NOTSET;
46 b855148c 2004-05-16 devnull int output = NOTSET;
47 b855148c 2004-05-16 devnull int outoffset = NOTSET;
48 b855148c 2004-05-16 devnull int inoffset = NOTSET;
50 b855148c 2004-05-16 devnull int cryptkey = 0; /* encryption key set with -e */
51 b855148c 2004-05-16 devnull int linelength = LINELENGTH; /* only for hex output */
52 b855148c 2004-05-16 devnull int lastchar = 0;
54 b855148c 2004-05-16 devnull unsigned long seed = EEXEC;
55 b855148c 2004-05-16 devnull unsigned long key;
57 b855148c 2004-05-16 devnull FILE *fp_in;
59 b855148c 2004-05-16 devnull /*****************************************************************************/
61 b855148c 2004-05-16 devnull main(agc, agv)
64 b855148c 2004-05-16 devnull char *agv[];
70 b855148c 2004-05-16 devnull * Implementation of the encryption/decryption used by eexec and show.
74 b855148c 2004-05-16 devnull argc = agc;
75 b855148c 2004-05-16 devnull argv = agv;
77 b855148c 2004-05-16 devnull fp_in = stdin;
79 b855148c 2004-05-16 devnull options();
80 b855148c 2004-05-16 devnull initialize();
81 b855148c 2004-05-16 devnull arguments();
85 b855148c 2004-05-16 devnull } /* End of main */
87 b855148c 2004-05-16 devnull /*****************************************************************************/
89 b855148c 2004-05-16 devnull options()
94 b855148c 2004-05-16 devnull char *names = "bde:l:os:xBSX";
96 b855148c 2004-05-16 devnull extern char *optarg;
97 b855148c 2004-05-16 devnull extern int optind;
101 b855148c 2004-05-16 devnull * Command line options.
105 b855148c 2004-05-16 devnull while ( (ch = getopt(argc, argv, names)) != EOF )
106 b855148c 2004-05-16 devnull switch ( ch ) {
107 b855148c 2004-05-16 devnull case 'b': /* binary input */
108 b855148c 2004-05-16 devnull input = BINARY;
111 b855148c 2004-05-16 devnull case 'd': /* decrypt */
112 b855148c 2004-05-16 devnull mode = DECRYPT;
115 b855148c 2004-05-16 devnull case 'e': /* encrypt */
116 b855148c 2004-05-16 devnull mode = ENCRYPT;
117 b855148c 2004-05-16 devnull if ( *optarg == '0' && *optarg == 'x' )
118 b855148c 2004-05-16 devnull optarg += 2;
119 b855148c 2004-05-16 devnull sscanf(optarg, "%8x", &cryptkey);
122 b855148c 2004-05-16 devnull case 'l': /* line length hex output */
123 b855148c 2004-05-16 devnull linelength = atoi(optarg);
126 b855148c 2004-05-16 devnull case 'o': /* output all bytes - debugging */
127 b855148c 2004-05-16 devnull outoffset = 0;
130 b855148c 2004-05-16 devnull case 's': /* seed */
131 b855148c 2004-05-16 devnull if ( *optarg == 'e' )
132 b855148c 2004-05-16 devnull seed = EEXEC;
133 b855148c 2004-05-16 devnull else if ( *optarg == 's' )
134 b855148c 2004-05-16 devnull seed = CHARSTRING;
135 b855148c 2004-05-16 devnull else if ( *optarg == '0' && *(optarg+1) == 'x' )
136 b855148c 2004-05-16 devnull sscanf(optarg+2, "%x", &seed);
137 b855148c 2004-05-16 devnull else if ( *optarg == '0' )
138 b855148c 2004-05-16 devnull sscanf(optarg, "%o", &seed);
139 b855148c 2004-05-16 devnull else sscanf(optarg, "%d", &seed);
142 b855148c 2004-05-16 devnull case 'x': /* hex input */
143 b855148c 2004-05-16 devnull input = HEX;
146 b855148c 2004-05-16 devnull case 'B': /* binary output */
147 b855148c 2004-05-16 devnull output = BINARY;
150 b855148c 2004-05-16 devnull case 'X': /* hex output */
151 b855148c 2004-05-16 devnull output = HEX;
154 b855148c 2004-05-16 devnull case '?': /* don't understand the option */
155 b855148c 2004-05-16 devnull fprintf(stderr, "bad option -%c\n", ch);
156 b855148c 2004-05-16 devnull exit(1);
159 b855148c 2004-05-16 devnull default: /* don't know what to do for ch */
160 b855148c 2004-05-16 devnull fprintf(stderr, "missing case for option -%c\n", ch);
161 b855148c 2004-05-16 devnull exit(1);
163 b855148c 2004-05-16 devnull } /* End switch */
165 b855148c 2004-05-16 devnull argc -= optind; /* get ready for non-option args */
166 b855148c 2004-05-16 devnull argv += optind;
168 b855148c 2004-05-16 devnull } /* End of options */
170 b855148c 2004-05-16 devnull /*****************************************************************************/
172 b855148c 2004-05-16 devnull initialize()
178 b855148c 2004-05-16 devnull * Initialization that has to be done after the options.
182 b855148c 2004-05-16 devnull key = seed;
184 b855148c 2004-05-16 devnull if ( mode == DECRYPT ) {
185 b855148c 2004-05-16 devnull input = (input == NOTSET) ? HEX : input;
186 b855148c 2004-05-16 devnull output = (output == NOTSET) ? BINARY : output;
187 b855148c 2004-05-16 devnull inoffset = (inoffset == NOTSET) ? 0 : inoffset;
188 b855148c 2004-05-16 devnull outoffset = (outoffset == NOTSET) ? -4 : outoffset;
189 b855148c 2004-05-16 devnull } else {
190 b855148c 2004-05-16 devnull input = (input == NOTSET) ? BINARY : input;
191 b855148c 2004-05-16 devnull output = (output == NOTSET) ? HEX : output;
192 b855148c 2004-05-16 devnull inoffset = (inoffset == NOTSET) ? 4 : inoffset;
193 b855148c 2004-05-16 devnull outoffset = (outoffset == NOTSET) ? 0 : outoffset;
194 b855148c 2004-05-16 devnull } /* End else */
196 b855148c 2004-05-16 devnull if ( linelength <= 0 )
197 b855148c 2004-05-16 devnull linelength = LINELENGTH;
199 b855148c 2004-05-16 devnull } /* End of initialize */
201 b855148c 2004-05-16 devnull /*****************************************************************************/
203 b855148c 2004-05-16 devnull arguments()
209 b855148c 2004-05-16 devnull * Everything left is an input file. No arguments or '-' means stdin.
213 b855148c 2004-05-16 devnull if ( argc < 1 )
214 b855148c 2004-05-16 devnull crypt();
216 b855148c 2004-05-16 devnull while ( argc > 0 ) {
217 b855148c 2004-05-16 devnull if ( strcmp(*argv, "-") == 0 )
218 b855148c 2004-05-16 devnull fp_in = stdin;
219 b855148c 2004-05-16 devnull else if ( (fp_in = fopen(*argv, "r")) == NULL ) {
220 b855148c 2004-05-16 devnull fprintf(stderr, "can't open %s\n", *argv);
221 b855148c 2004-05-16 devnull exit(1);
222 b855148c 2004-05-16 devnull } /* End if */
223 b855148c 2004-05-16 devnull crypt();
224 b855148c 2004-05-16 devnull if ( fp_in != stdin )
225 b855148c 2004-05-16 devnull fclose(fp_in);
228 b855148c 2004-05-16 devnull } /* End while */
230 b855148c 2004-05-16 devnull } /* End of arguments */
232 b855148c 2004-05-16 devnull /*****************************************************************************/
238 b855148c 2004-05-16 devnull unsigned int cypher;
239 b855148c 2004-05-16 devnull unsigned int clear;
243 b855148c 2004-05-16 devnull * Runs the encryption/decryption algorithm.
247 b855148c 2004-05-16 devnull while ( lastchar != EOF ) {
248 b855148c 2004-05-16 devnull cypher = nextbyte();
249 b855148c 2004-05-16 devnull clear = ((key >> 8) ^ cypher) & 0xFF;
250 b855148c 2004-05-16 devnull key = (key + (mode == DECRYPT ? cypher : clear)) * MAGIC1 + MAGIC2;
251 b855148c 2004-05-16 devnull if ( ++outoffset > 0 && lastchar != EOF ) {
252 b855148c 2004-05-16 devnull if ( output == HEX ) {
253 b855148c 2004-05-16 devnull printf("%.2X", clear);
254 b855148c 2004-05-16 devnull if ( linelength > 0 && (outoffset % linelength) == 0 )
255 b855148c 2004-05-16 devnull putchar('\n');
256 b855148c 2004-05-16 devnull } else putchar(clear);
257 b855148c 2004-05-16 devnull } /* End if */
258 b855148c 2004-05-16 devnull } /* End while */
260 b855148c 2004-05-16 devnull } /* End of crypt */
262 b855148c 2004-05-16 devnull /*****************************************************************************/
264 b855148c 2004-05-16 devnull nextbyte()
268 b855148c 2004-05-16 devnull int val = EOF;
272 b855148c 2004-05-16 devnull * Returns the next byte. Uses cryptkey (i.e. what followed -e) while inoffset is
273 b855148c 2004-05-16 devnull * positive, otherwise reads (hex or binary) from fp_in.
277 b855148c 2004-05-16 devnull if ( inoffset-- > 0 )
278 b855148c 2004-05-16 devnull val = (cryptkey >> (inoffset*8)) & 0xFF;
279 b855148c 2004-05-16 devnull else if ( input == HEX ) {
280 b855148c 2004-05-16 devnull if ( (val = nexthexchar()) != EOF )
281 b855148c 2004-05-16 devnull val = (val << 4) | nexthexchar();
282 b855148c 2004-05-16 devnull } else if ( input == BINARY )
283 b855148c 2004-05-16 devnull val = Getc(fp_in);
285 b855148c 2004-05-16 devnull return(val);
287 b855148c 2004-05-16 devnull } /* End of nextbyte */
289 b855148c 2004-05-16 devnull /*****************************************************************************/
291 b855148c 2004-05-16 devnull nexthexchar()
299 b855148c 2004-05-16 devnull * Reads the next hex character.
303 b855148c 2004-05-16 devnull while ( (ch = Getc(fp_in)) != EOF && ! isxdigit(ch) ) ;
305 b855148c 2004-05-16 devnull if ( isdigit(ch) )
306 b855148c 2004-05-16 devnull ch -= '0';
307 b855148c 2004-05-16 devnull else if ( isupper(ch) )
308 b855148c 2004-05-16 devnull ch -= 'A' - 10;
309 b855148c 2004-05-16 devnull else if ( islower(ch) )
310 b855148c 2004-05-16 devnull ch -= 'a' - 10;
312 b855148c 2004-05-16 devnull return(ch);
314 b855148c 2004-05-16 devnull } /* End of nexthexchar */
316 b855148c 2004-05-16 devnull /*****************************************************************************/
318 b855148c 2004-05-16 devnull Getc(fp)
320 b855148c 2004-05-16 devnull FILE *fp;
326 b855148c 2004-05-16 devnull * Reads the next byte from *fp, sets lastchar, and returns the character.
330 b855148c 2004-05-16 devnull return(lastchar = getc(fp));
332 b855148c 2004-05-16 devnull } /* End of Getc */
334 b855148c 2004-05-16 devnull /*****************************************************************************/