|
|
1.1 root 1: #include <stdio.h>
2:
3: unsigned int seed1, seed2;
4: unsigned char rotor[256];
5: unsigned char deck[256];
6: char key[12];
7: char padkey[] = {
8: 003, 022, 006, 007, 034,
9: 023, 031, 001, 027, 036
10: };
11: char *getpass();
12: char *crypt();
13:
14: main(ac, av)
15: int ac;
16: char *av[];
17: {
18: getkey(ac, av);
19: pervertkey();
20: makerotor();
21: cryptext();
22: return (0);
23: }
24:
25: /*
26: * Get the password and destroy all traces in the system.
27: */
28: getkey(ac, av)
29: int ac;
30: char *av[];
31: {
32: register char *ps;
33: register int i;
34:
35: if (ac == 2)
36: ps = av[1];
37: else if (ac == 1) {
38: ps = getpass("Password? ");
39: if (*ps == '\0')
40: derror("Empty password not allowed.");
41: }
42: else
43: derror("USAGE: crypt [password]");
44: for (i = 0; i < 10 && (key[i] = *ps) != '\0'; ++i)
45: *ps++ = '\0';
46: while (*ps)
47: *ps++ = '\0';
48: }
49:
50: /*
51: * Pad key to 10 chars, coerce the last two into `salt' range [./0-9A-Za-z].
52: * Feed this to crypt(3), and copy the last 11 chars of the result into key.
53: */
54: pervertkey()
55: {
56: register char *cp;
57: register char *kp = key;
58:
59: strncat(key, padkey, 10 - strlen(key));
60: key[10] = legal(key[9]);
61: key[9] = legal(key[8]);
62: key[11] = key[8] = '\0';
63: cp = crypt(&key[0], &key[9]);
64: cp += 2;
65: while ((*kp++ = *cp++) != 0)
66: ;
67: }
68:
69: /*
70: * Coerce a char into a legal salt character if it is not.
71: */
72: legal(c)
73: register int c;
74: {
75: if (c < 12)
76: return (c + '.');
77: else if (c < 38)
78: return (c - 12 + 'A');
79: else if (c < '.')
80: return (c - 38 + 'a');
81: else if ('9' < c && c < 'A')
82: return ('o' + 1 - ('A' - c));
83: else if ('Z' < c && c < 'a')
84: return ('u' + 1 - ('a' - c));
85: else if (c > 'z')
86: return ('z' + 1 - (0200 - c));
87: else
88: return (c);
89: }
90:
91:
92: /*
93: * Makerotor() uses key to initialize two seeds for rand(). Seed1 is used to
94: * build the rotor. Seed2 initializes the `random' sequence of offsets.
95: */
96: makerotor()
97: {
98: register int i, j;
99: register unsigned char uc;
100:
101: /* Initialize seeds. */
102: for (i = 0; i < 6; ++i) {
103: seed1 = (++seed1) * key[i];
104: seed2 = (++seed2) * key[i + 5];
105: }
106:
107: /* Initialize and shuffle deck. */
108: srand(seed1);
109: for (i = 0; i < 256; ++i)
110: deck[i] = i;
111: for (i = 0; i < 256; ++i) {
112: j = i + rand() % (256 - i);
113: uc = deck[i];
114: deck[i] = deck[j];
115: deck[j] = uc;
116: }
117:
118: /* Initialize rotor. */
119: for (i = 0; i < 256; i+=2) {
120: rotor[deck[i]] = deck[i + 1];
121: rotor[deck[i + 1]] = deck[i];
122: }
123: }
124:
125: cryptext()
126: {
127: register int c, j;
128:
129: srand(seed2);
130: while ((c = getchar()) != EOF) {
131: j = rand() % 256;
132: putchar((rotor[(c + j) % 256] + 256 - j) % 256);
133: }
134: }
135:
136: derror(s)
137: register char *s;
138: {
139: fprintf(stderr, "%s\n", s);
140: exit(1);
141: }
142:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.