/*
* engima simulation
*
* author: Henry Tieman
*
* references:
* "How Polish Mathematicians Deciphered the Enigma", Marian Rejewski,
* Annals of the History of Computing, Vol 3, no 3, July 1981, Pg 213 ff
* appendix by C. A. Devours.
*
* "Machine Cryptography and Modern Cryptanalysis", C.A. Deavours and L.
* Kurth, Artech House, Dedham, Mass 1985.
*/
/*
* rotor data
* reference "Machine Cryptography and Modern Cryptanalysis" pg. 100
*
* note: rotor stepping is associated with each @PROGCODE = rotor instead
* of position or being constant.
*/
c -= 'A'; /* start to encipher */
if (n_plugs != 0)
c = data[0][c];
for (j=0;j<3;j++) /* do rotors forward */
{
idx = (c + pos[j]) % 26;
c = (c + data[j+1][idx]) % 26;
}
c = (data[4][c]) % 26; /* reflecting rotor */
for (j=0;j<3;j++) /* do rotors reverse */
{
idx = (c + pos[2-j]) % 26;
c = (c + data[j+5][idx]) % 26;
}
if (n_plugs != 0)
c = data[0][c];
c += 'A';
}
return(c);
}
/*
* encipher_file - open and encipher a file
*/
void
encipher_file(char *file_name)
{
FILE *fp; /* plaintext/ciphertext FILE pointer */
char line[LINE_LEN + 1]; /* input data line, inc. '\n' */
char *ret_val; /* value from fgets(), used for EOF check */
char c; /* character from data line */
int len; /* length of data line */
int idx; /* index/counter */
fp = fopen(file_name, "r");
ret_val = fgets(line, LINE_LEN, fp);
while(ret_val != NULL)
{
len = strlen(line);
for (idx=0;idx<len;idx++)
{
c = line[idx];
if (isalpha(c))
{
c = encipher((int)(toupper(c)));
line[idx] = c;
}
}
printf("%s", line);
ret_val = fgets(line, LINE_LEN, fp);
}
fclose(fp);
}
/*
* init_mach - set up data according to the input data
*/
void
init_mach( void )
{
int i, j; /* indexes */
int ds; /* used during ring settings */
int u, v; /* temps for plugboard input */
/* setup rotor data */
for (j=0;j<26;j++)
data[4][j] = ((int)ref_rotor[j]-'A'+26)%26;
/*
* usage - function to print out the correct usage of the program
*/
void
usage( char *str )
{
fprintf(stderr, "usage: %s [<keyfile>] <infile>\n\n", str);
fprintf(stderr, "\tkeyfile has the form:\n");
fprintf(stderr, "\t\tn n n\t\t- for rotor order, 1 <= n <= 5\n");
fprintf(stderr, "\t\tx x x\t\t- for ring settings, x alpha\n");
fprintf(stderr, "\t\tn\t\t- number of plugs, 0 <= n <= 13\n");
fprintf(stderr, "\t\txx xx xx ...\t- plug letter pairs, one pair"
" for each n\n");
fprintf(stderr, "\t\tx x x\t\t- initial rotor position, x alpha\n\n");
fprintf(stderr, "\toutput is stdout\n");
exit(0);
}
/*
* main - the main function, nothing special here
*/
void
main( int argc, char ** argv )
{
char *infile; /* plaintext/ciphertext file name ptr */