#include #include #include #include"enigma.h" /*Input: Accepts no user input, gets 2 arrays from main * Output: NONE * Return: NONE * Notes: Initializes the rotor */ void rotor_init (char arr[], char rotor[]) { int x; int count; char I[3] = {'I'}; char II[3] = {'I','I'}; char III[3] = {'I','I','I'}; char IV[3] = {'I','V'}; char library[5][26] = { { 'E','K','M','F','L','G','D','Q','V','Z','N','T','O', 'W','Y','H','X','U','S','P','A','I','B','R','C','J' } , { 'A','J','D','K','S','I','R','U','X','B','L','H','W', 'T','M','C','Q','G','Z','N','P','Y','F','V','O','E' } , { 'B','D','F','H','J','L','C','P','R','T','X','V','Z', 'N','Y','E','I','W','G','A','K','M','U','S','Q','O' } , { 'E','S','O','V','P','Z','J','A','Y','Q','U','I','R', 'H','X','L','N','F','T','G','K','D','C','M','W','B' } , { 'V','Z','B','R','G','I','T','Y','U','P','S','D','N', 'H','L','X','A','W','M','J','Q','O','F','E','C','K' } }; if ( strcmp(rotor, I )) { x = 0; } else if ( strcmp(rotor, II )) { x = 1; } else if ( strcmp(rotor, III )) { x = 2; } else if ( strcmp(rotor, IV )) { x = 3; } else { x = 4; } for ( count = 0 ; count < 26 ; count++ ) { arr[count] = library[x][count]; } } /*Input: Accepts no user input, gets 1 array and a char from main * Output: NONE * Return: NONE * Notes: Initializes the reflector */ void reflector_init (char arr[], char * reflector) { int x; int count; char library[2][26] = { { 'Y','R','U','H','Q','S','L','D','P','X','N','G','O', 'K','M','I','E','B','F','Z','C','W','V','J','A','T' } , { 'F','V','P','J','I','A','O','Y','E','D','R','Z','X', 'W','G','C','T','K','U','Q','S','B','N','M','H','L' } }; if (*reflector == 'B' || *reflector == 'b') { x = 0; } else { x = 1; } for ( count = 0 ; count < 26 ; count++ ) { arr[count] = library[x][count]; } } /*Input: Accepts no user input, gets the spindle and an int from main * Output: NONE * Return: NONE * Notes: handles rotor stepping */ int rotor_step ( Spindle * spin, int * x) { int check; check = *x; spin->rotpos1++; if ( spin->rotpos1 == 26 ) { spin->rotpos1 = 0; check = 2; } if ( check > 0 ) { spin->rotpos2++; check--; } if ( spin->rotpos2 == 26 ) { spin->rotpos2 = 0; spin->rotpos3++; } if ( spin->rotpos3 == 26 ) { spin->rotpos3 = 0; } return check; } /*Input: Accepts no user input, gets the spindle and a char from main * Output: prints encrypted letter to stdout * Return: NONE * Notes: handles the letter substitutions to encrypt message */ void substitution ( Spindle * spin , char * letter ) { int x; int count; char y; x = char_to_int( &(*letter) ); y = spin->rotor1[ x ]; printf("%c\n", y); x = char_to_int ( &y ); y = spin->rotor2[ x ]; printf("%c\n", y); x = char_to_int ( &y ); y = spin->rotor3[ x ]; printf("%c\n", y); x = char_to_int ( &y ); y = spin->reflector[ x ]; printf("%c\n", y); x=1; for ( count = 0 ; count < 26 ; count++ ) { if ( spin->rotor3[count] == y ) { y = int_to_char( &count ); printf("%d-%c-%d\n", count, int_to_char( &count ), x); count=26; } } printf("%c\n", y); x++; for ( count = 0 ; count < 26 ; count++ ) { if ( spin->rotor2[count] == y ) { y = int_to_char( &count ); printf("%d-%c-%d\n", count, int_to_char( &count ), x); count=26; } } printf("%c\n", y); x++; for ( count = 0 ; count < 26 ; count++ ) { if ( spin->rotor1[count] == y ) { y = int_to_char( &count ); printf("%d-%c-%d\n", count, int_to_char( &count ), x); count=26; } } printf("%c\n", y); } /*Input: Accepts no user input, gets a char from main * Output: none * Return: int * Notes: converts a char to an int */ int char_to_int ( char * letter ) { int x; if ( *letter == 'A' || *letter == 'a' ) x = 0; else if ( *letter == 'B' || *letter == 'b' ) x = 1; else if ( *letter == 'C' || *letter == 'c' ) x = 2; else if ( *letter == 'D' || *letter == 'd' ) x = 3; else if ( *letter == 'E' || *letter == 'e' ) x = 4; else if ( *letter == 'F' || *letter == 'f' ) x = 5; else if ( *letter == 'G' || *letter == 'g' ) x = 6; else if ( *letter == 'H' || *letter == 'h' ) x = 7; else if ( *letter == 'I' || *letter == 'i' ) x = 8; else if ( *letter == 'J' || *letter == 'j' ) x = 9; else if ( *letter == 'K' || *letter == 'k' ) x = 10; else if ( *letter == 'L' || *letter == 'l' ) x = 11; else if ( *letter == 'M' || *letter == 'm' ) x = 12; else if ( *letter == 'N' || *letter == 'n' ) x = 13; else if ( *letter == 'O' || *letter == 'o' ) x = 14; else if ( *letter == 'P' || *letter == 'p' ) x = 15; else if ( *letter == 'Q' || *letter == 'q' ) x = 16; else if ( *letter == 'R' || *letter == 'r' ) x = 17; else if ( *letter == 'S' || *letter == 's' ) x = 18; else if ( *letter == 'T' || *letter == 't' ) x = 19; else if ( *letter == 'U' || *letter == 'u' ) x = 20; else if ( *letter == 'V' || *letter == 'v' ) x = 21; else if ( *letter == 'W' || *letter == 'w' ) x = 22; else if ( *letter == 'X' || *letter == 'x' ) x = 23; else if ( *letter == 'Y' || *letter == 'y' ) x = 24; else if ( *letter == 'Z' || *letter == 'z' ) x = 25; return x; } /*Input: Accepts no user input, gets an int from main * Output: none * Return: char * Notes: converts an int to a char */ char int_to_char ( int * x ) { char y; if (*x == 0) y = 'A'; else if (*x == 1) y = 'B'; else if (*x == 2) y = 'C'; else if (*x == 3) y = 'D'; else if (*x == 4) y = 'E'; else if (*x == 5) y = 'F'; else if (*x == 6) y = 'G'; else if (*x == 7) y = 'H'; else if (*x == 8) y = 'I'; else if (*x == 9) y = 'J'; else if (*x == 10) y = 'K'; else if (*x == 11) y = 'L'; else if (*x == 12) y = 'M'; else if (*x == 13) y = 'N'; else if (*x == 14) y = 'O'; else if (*x == 15) y = 'P'; else if (*x == 16) y = 'Q'; else if (*x == 17) y = 'R'; else if (*x == 18) y = 'S'; else if (*x == 19) y = 'T'; else if (*x == 20) y = 'U'; else if (*x == 21) y = 'V'; else if (*x == 22) y = 'W'; else if (*x == 23) y = 'X'; else if (*x == 24) y = 'Y'; else if (*x == 25) y = 'Z'; return y; } /*Input: Accepts user input for rotors and reflector, as well as initial * position, and encryption indicator * Output: none * Return: int * Notes: simulates the enigma machine */ int main() { /*initializes variables*/ int i; int check = 0; char initial[3]; char encrypt[3]; char rot1[3]; char rot2[3]; char rot3[3]; char reflec; char arr[26]; char message[80]; /*initializes the spindle*/ Spindle spin; /*sets default initial positions for rotors in spindle*/ spin.rotpos1 = 0; spin.rotpos2 = 0; spin.rotpos3 = 0; /*get rotors and reflector from the user, and initial position and * encryption indicator*/ scanf("%c %s %s %s", &reflec, rot1, rot2, rot3); scanf("%s %s", initial, encrypt); printf("%s %s", initial, encrypt); /*gets message from user to encrypt/decrypt*/ fgets(message, sizeof(message), stdin ); /*initializes first rotor in the sequence*/ rotor_init ( arr, rot3 ); /*sets up wiring for the rotor selected*/ for ( i = 0 ; i < 26 ; i++ ) { spin.rotor1[i] = arr[i]; } /*initializes second rotor in the sequence*/ rotor_init ( arr, rot2 ); /*sets up wiring for the rotor selected*/ for ( i = 0 ; i < 26 ; i++ ) { spin.rotor2[i] = arr[i]; } /*initializes third rotor in the sequence*/ rotor_init ( arr, rot1 ); /*sets up wiring for the rotor selected*/ for ( i = 0 ; i < 26 ; i++ ) { spin.rotor3[i] = arr[i]; } /*initializes reflector*/ reflector_init ( arr, &reflec ); /*sets up wiring for the reflector selected*/ for ( i = 0 ; i < 26 ; i++ ) { spin.reflector[i] = arr[i]; } /*for loop to step rotors, and run enigma letter substitution while * there are still letters to read from message */ for ( i = 0 ; i < strlen(message) ; i++ ) { check = rotor_step ( &spin, &check ); substitution( &spin, &(message[i]) ); } return 0; }