Question: THIS IS FOR C PROGRAMMING: Write code that encrypts and decrypts messages in a manner that is similar to the Nazi enigma machine. The program
THIS IS FOR C PROGRAMMING:
Write code that encrypts and decrypts messages in a manner that is similar to the Nazi enigma machine.
The program should have 4 command line arguments. The first 3 input arguments should set the positions for each rotor. The rotors act like a secret passcode. The same passcode (rotor positions) are needed for encrypting and decrypting the message. The last input argument should set to either e or d. The value e stands for encrypt and d stands for decrypt.
ENCRYPT LETTER: The encryption works by replacing each original unencrypted letter with a different encrypted letter. The key is used to determine the replacement letter. For example, if the user types in character A which is index 0. The key would be 0 + (9+3+6) = 18, so A would be transformed to letter S. If the user typed in B, the character would be 1 + (9+3+6) = 19, so the encrypted letter would be T. A key value that is greater than 26 wraps around. For example, for the same rotor positions and unencrypted letter Z should correspond to key: 25 + 18 - 27 = 16 (or encrypted letter Q).
DECRYPT LETTER: A person that seeks to decrypt the message needs to know the initial rotor position and the encrypted message. Instead of adding the letter index to the key, we now want to subtract. For example, if we started with the same rotor positions: 4, 1, and 7. We would have key 9+3+6= 18. If we had encrypted letter Z (which is index 25), we would have decrypted letter 25 - 18 = 7 which corresponds to letter H (i.e. LETTERS[7]).
Basically, you have to fill the " TO DO" spaces of the following given code :
#include#include #include #include const char LETTERS[27] = {'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N','O','P','Q','R','S','T', 'U','V','W','X','Y','Z', ' '}; const char ROTOR_VALUES[10] = {7, 3, 0, 1, 9, 5, 2, 6, 4, 8}; const int ROTOR_SIZE = 10; const int TEXT_SIZE = 256; int rotorPosition1, rotorPosition2, rotorPosition3; /* findCharIndex: Finds the location(index) of a character in the Letters array * Input: Character to find in Letters Array * Output: Index location in the Letters Array * Note: Returns -1 if not found */ int findCharIndex(char c) { //TO DO: Need to implement this function return -1; } /* mod: Implements a true modulus function * INPUT: original number (a) and dividor (b) * OUTPUT: remainder * NOTE: Required since in C (%) does not implement true modulus * function for negative remainders */ int mod(int a, int b) { int r = a % b; return r //TO DO: Need to implement this function return 'A'; } /* decryptLetter: Returns an decrypted letter (character) * INPUT: encrypted letter (character) and key * OUTPUT: decrypted letter */ char decryptLetter(char encrypted, int key){ //TO DO: Need to implement this function return 'A'; } /* getKey: returns key based on current positions of the rotors */ int getKey() { return ROTOR_VALUES[rotorPosition1]+ROTOR_VALUES[rotorPosition2]+ROTOR_VALUES[rotorPosition3]; } /* incrementRotors: Advances rotor positions by one * USAGE: Used after every letter is encrypted or decrypted */ void incrementRotors() { rotorPosition1++; if (rotorPosition1 == ROTOR_SIZE){ rotorPosition1 = 0; rotorPosition2++; } if (rotorPosition2 == ROTOR_SIZE){ rotorPosition2 = 0; rotorPosition3++; } if (rotorPosition3 == ROTOR_SIZE) rotorPosition3 = 0; } int main( int argc, char *argv[] ) { //STEP #1: Get and check Command Line Arguments //TO DO: Implement this step //STEP #2: Get text string from user char inputText[TEXT_SIZE]; char outputText[TEXT_SIZE]; scanf( "%[^ ]s" , inputText ); //%20 includes spaces printf("Input text supplied is: %s ", inputText); int input_text_size = strlen(inputText) - 1; //minus 1 to ignore char //STEP #3: For each letter, encrypt or decrypt int i=0; for (i=0;i //TO DO: Implement this step } //STEP #4: Print result outputText[input_text_size+1] = '\0'; printf("Output text: %s ", outputText); return EXIT_SUCCESS; }
Output Examples:
The encrypted message is shown at the bottom.

If we set d for decryption mode the program will take the encrypted text and convert it back to the original message. Notice that the rotor positions must be the same to successfully decrypt the message.


unix1% . /enigma 4 1 7 e Rotor #1 Position is set to 4 Rotor #2 Position is set to 1 Rotor #3 Position is set to 7 SECRET MESSAGE Input text supplied is: SECRET MESSAGE unencrypted letter: SIencrypted to letter: J unencrypted letter:EIencrypted to letter: S unencrypted letter: CI encrypted to letter: N unencrypted letter: RIencrypted to letter: F unencrypted letter:Eencrypted to letter: R unencrypted letter: I encrypted to letter: J unencrypted letterencrypted to letter: M unencrypted letter: MIencrypted to letter: V unencrypted letter:Eencrypted to letter: K unencrypted letter: SIencrypted to letter: Z unencrypted letter: SIencrypted to letter: G unencrypted letter: Aencrypted to letter: L unencrypted letter: GI encrypted to letter: O unencrypted letter:Eencrypted to letter: Q Output text: JSNFRJMVKZGLO
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
