BSD 4 release
[unix-history] / usr / src / cmd / crypt.c
CommitLineData
31cef89c 1static char *sccsid = "@(#)crypt.c 4.1 (Berkeley) 10/1/80";
9c79dca0
BJ
2/*
3 * A one-rotor machine designed along the lines of Enigma
4 * but considerably trivialized.
5 */
6
7#define ECHO 010
8#include <stdio.h>
9#define ROTORSZ 256
10#define MASK 0377
11char t1[ROTORSZ];
12char t2[ROTORSZ];
13char t3[ROTORSZ];
14char *getpass();
15
16setup(pw)
17char *pw;
18{
19 int ic, i, k, temp, pf[2];
20 unsigned random;
21 char buf[13];
22 long seed;
23
24 strncpy(buf, pw, 8);
25 while (*pw)
26 *pw++ = '\0';
27 buf[8] = buf[0];
28 buf[9] = buf[1];
29 pipe(pf);
30 if (fork()==0) {
31 close(0);
32 close(1);
33 dup(pf[0]);
34 dup(pf[1]);
35 execl("/usr/lib/makekey", "-", 0);
36 execl("/lib/makekey", "-", 0);
37 exit(1);
38 }
39 write(pf[1], buf, 10);
40 wait((int *)NULL);
41 if (read(pf[0], buf, 13) != 13) {
42 fprintf(stderr, "crypt: cannot generate key\n");
43 exit(1);
44 }
45 seed = 123;
46 for (i=0; i<13; i++)
47 seed = seed*buf[i] + i;
48 for(i=0;i<ROTORSZ;i++)
49 t1[i] = i;
50 for(i=0;i<ROTORSZ;i++) {
51 seed = 5*seed + buf[i%13];
52 random = seed % 65521;
53 k = ROTORSZ-1 - i;
54 ic = (random&MASK)%(k+1);
55 random >>= 8;
56 temp = t1[k];
57 t1[k] = t1[ic];
58 t1[ic] = temp;
59 if(t3[k]!=0) continue;
60 ic = (random&MASK) % k;
61 while(t3[ic]!=0) ic = (ic+1) % k;
62 t3[k] = ic;
63 t3[ic] = k;
64 }
65 for(i=0;i<ROTORSZ;i++)
66 t2[t1[i]&MASK] = i;
67}
68
69main(argc, argv)
70char *argv[];
71{
72 register i, n1, n2;
73
74 if (argc != 2){
75 setup(getpass("Enter key:"));
76 }
77 else
78 setup(argv[1]);
79 n1 = 0;
80 n2 = 0;
81
82 while((i=getchar()) >=0) {
83 i = t2[(t3[(t1[(i+n1)&MASK]+n2)&MASK]-n2)&MASK]-n1;
84 putchar(i);
85 n1++;
86 if(n1==ROTORSZ) {
87 n1 = 0;
88 n2++;
89 if(n2==ROTORSZ) n2 = 0;
90 }
91 }
92}