|
| 1 | +/* |
| 2 | + * A Simple 3 Rotor Enigma Simulation |
| 3 | + * |
| 4 | + * 11/9/98 djm - Converted to z88dk |
| 5 | + * 27/1/02 djm - Added support for argc/argv for CPM |
| 6 | + */ |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <ctype.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <string.h> |
| 12 | + |
| 13 | + |
| 14 | +/* Rotor Wirings */ |
| 15 | + |
| 16 | +unsigned char rotor[] = |
| 17 | + "EKMFLGDQVZNTOWYHXUSPAIBRCJAJDKSIRUXBLHWTMCQGZNPYFVOEBDFHJLCPRTXVZNYEIWGAKMUSQOESOVPZJAYQUIRHXLNFTGKDCMWBVZBRGITYUPSDNHLXAWMJQOFECK"; |
| 18 | + |
| 19 | +unsigned char ref[] = "YRUHQSLDPXNGOKMIEBFZCWVJAT"; |
| 20 | + |
| 21 | +unsigned char notch[] = "QEVJZ"; |
| 22 | +unsigned int flag = 0; |
| 23 | + |
| 24 | +/*Encryption parameters */ |
| 25 | + |
| 26 | +unsigned char order[3] = { 3, 1, 2 }; |
| 27 | +unsigned char rings[3] = { 'W', 'X', 'T' }; |
| 28 | +unsigned char pos[3] = { 'A', 'W', 'E' }; |
| 29 | +unsigned char plug[] = "AMTE"; |
| 30 | + |
| 31 | +#ifdef CPM |
| 32 | +int main(int argc, char *argv[]) |
| 33 | +#else |
| 34 | +int main() |
| 35 | +#endif |
| 36 | +{ |
| 37 | + unsigned int i, j, n; |
| 38 | + unsigned int ch; |
| 39 | +#ifdef CPM |
| 40 | + int len, posn; |
| 41 | + |
| 42 | + printf("%d\n", argc); |
| 43 | + if (argc != 2) { |
| 44 | + puts("Usage: enigma [text to be encoded]"); |
| 45 | + exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + len = strlen(argv[1]); |
| 49 | + posn = 0; |
| 50 | +#else |
| 51 | + puts("Enter text to be (de)coded, finish with a ."); |
| 52 | +#endif |
| 53 | + |
| 54 | +#ifdef CPM |
| 55 | + while (posn++ < len) { |
| 56 | + ch = toupper(*(argv[1] + posn)); |
| 57 | +#else |
| 58 | + for (;;) { |
| 59 | + ch = getchar(); |
| 60 | + ch = toupper(ch); |
| 61 | +#endif |
| 62 | + if (ch == '.') |
| 63 | + exit(0); |
| 64 | + if (!isalpha(ch)) |
| 65 | + continue; |
| 66 | + |
| 67 | + |
| 68 | + /* Step up first rotor */ |
| 69 | + pos[0]++; |
| 70 | + if (pos[0] > 'Z') |
| 71 | + pos[0] -= 26; |
| 72 | + |
| 73 | + /* Check if second rotor reached notch last time */ |
| 74 | + if (flag) { |
| 75 | + /* Step up second and third rotors */ |
| 76 | + pos[1]++; |
| 77 | + if (pos[1] > 'Z') |
| 78 | + pos[1] -= 26; |
| 79 | + pos[2]++; |
| 80 | + if (pos[2] > 'Z') |
| 81 | + pos[2] -= 26; |
| 82 | + flag = 0; |
| 83 | + } |
| 84 | + |
| 85 | +/* Step up seconond rotor if first rotor reached notch */ |
| 86 | + if (pos[0] == notch[order[0] - 1]) { |
| 87 | + pos[1]++; |
| 88 | + if (pos[1] > 'Z') |
| 89 | + pos[1] -= 26; |
| 90 | + if (pos[1] == notch[order[1] - 1]) |
| 91 | + flag = 1; |
| 92 | + } |
| 93 | + |
| 94 | +/*Swap pairs of letters on plugboard */ |
| 95 | + |
| 96 | + for (i = 0; plug[i]; i += 2) { |
| 97 | + if (ch == plug[i]) |
| 98 | + ch = plug[i + 1]; |
| 99 | + else if (ch == plug[i + 1]) |
| 100 | + ch = plug[i]; |
| 101 | + } |
| 102 | + |
| 103 | +/* Rotors (forward) */ |
| 104 | + |
| 105 | + for (i = 0; i < 3; i++) { |
| 106 | + ch += pos[i] - 'A'; |
| 107 | + if (ch > 'Z') |
| 108 | + ch -= 26; |
| 109 | + |
| 110 | + ch -= rings[i] - 'A'; |
| 111 | + if (ch < 'A') |
| 112 | + ch += 26; |
| 113 | + |
| 114 | + ch = rotor[((order[i] - 1) * 26) + ch - 'A']; |
| 115 | + |
| 116 | + ch += rings[i] - 'A'; |
| 117 | + if (ch > 'Z') |
| 118 | + ch -= 26; |
| 119 | + |
| 120 | + ch -= pos[i] - 'A'; |
| 121 | + if (ch < 'A') |
| 122 | + ch += 26; |
| 123 | + } |
| 124 | + |
| 125 | +/* Reflecting rotor */ |
| 126 | + ch = ref[ch - 'A']; |
| 127 | + |
| 128 | +/*Rotors (Reverse) */ |
| 129 | + |
| 130 | + for (i = 3; i; i--) { |
| 131 | + ch += pos[i - 1] - 'A'; |
| 132 | + if (ch > 'Z') |
| 133 | + ch -= 26; |
| 134 | + |
| 135 | + ch -= rings[i - 1] - 'A'; |
| 136 | + if (ch < 'A') |
| 137 | + ch += 26; |
| 138 | + |
| 139 | + for (j = 0; j < 26; j++) |
| 140 | + if (rotor[(26 * (order[i - 1] - 1)) + j] == ch) |
| 141 | + break; |
| 142 | + |
| 143 | + ch = j + 'A'; |
| 144 | + |
| 145 | + ch += rings[i - 1] - 'A'; |
| 146 | + if (ch > 'Z') |
| 147 | + ch -= 26; |
| 148 | + |
| 149 | + ch -= pos[i - 1] - 'A'; |
| 150 | + if (ch < 'A') |
| 151 | + ch += 26; |
| 152 | + } |
| 153 | + |
| 154 | +/* Plugboard */ |
| 155 | + |
| 156 | + for (i = 0; plug[i]; i += 2) { |
| 157 | + if (ch == plug[i]) |
| 158 | + ch = plug[i + 1]; |
| 159 | + else if (ch == plug[i + 1]) |
| 160 | + ch = plug[i]; |
| 161 | + } |
| 162 | + |
| 163 | + n++; |
| 164 | + putchar(ch); |
| 165 | + if (n % 5 == 0) |
| 166 | + if (n % 55 == 0) |
| 167 | + putchar('\n'); |
| 168 | + else |
| 169 | + putchar(' '); |
| 170 | + } |
| 171 | +} |
0 commit comments