Assignment 03

Due: Friday, Sept. 25, 2020 at noon 100 points

You will submit one .cpp file for this assignment in the usual way. Make sure you submit according to the cssubmit process.

Background:

The Enigma

machine was invented by German professor, Jonathan I. Q. Neidelbaum Frink, at the end of World War I, and was most notably used during World War II, to encipher messages. Basically, when a character was typed in via a keyboard, it was passed through 3 rotors that successively translated it into different characters. The resulting thrice-translated (i.e., encrypted) character then lit up on a lampboard; see Figure 1. A similar (reverse-engineering) process was used to decrypt each character of a message. The rotors were interchangeable, which made the machine more difficult to “break.” So, for the original enigma machine, every message was preceded by 3 numbers designating the disks' order. For more information, see https://en.wikipedia.org/wiki/Enigma_machine.You are to write a program to decrypt an (encrypted) message via a very simplified, “knock-off” Krusty® brand Krusty-nigma Masheen©®™.

Specifications:

Your program will process a single message that will be entered by the user. The message entered is an already encrypted message; your code de-crypts. There are three “rotors”, numbered 1, 2, and 3, that each character must “pass through.” However, the order of rotors is subject to user input. Perhaps the message first goes through rotor 3, then 1, then 2. Or perhaps the message goes through rotor 1 first, then rotor 3, and finally rotor 2. Therefore, the first thing your program should do is ask the user which rotor (1..3) is first (i.e., the leftmost rotor), and then which of the remaining 2 rotors is next (i.e., the middle rotor). The remaining rotor (1, 2, or 3) will be the last (i.e., rightmost) rotor. This should be deduced by your program, not input from the user.

Next, you are ready to get each character in the encrypted message. We have not yet covered “string” processing, so your program should process the message one character at a time. Specifically, get input for a single character (‘A’..’Z’ or the underscore '_' or ‘.’). The period character will signal the end of the user’s transmission, at which time your program should sign-off. The underscore, '_', can be used to separate words - it is NOT encrypted nor de-crypted. So, if an underscore is the input character, just pass it on through without processing. Any character A ... Z is then put through the processing for the leftmost rotor, then the middle rotor, and finally the rightmost rotor. The resulting (thrice-translated) character should be output. Keep processing characters until the user enters the character ‘.’ Your code should not allow user input of any other character but A ... Z (only uppercase) or the underscore or the period.

So what processing does each rotor do? Well, each rotor behaves a little differently. One thing that each rotor has in common is that it takes a character as “input” and produces another character as “output.” So essentially the 3 rotors create a chain of translations to de-crypt the originally entered character.

Here is what each rotor does, assuming ch is a character ‘A’..’Z’:

Rotor 1: f(ch) = (((ascii value of ch) + 3) % 26) + 65

Rotor 2: f(ch) = (((ascii value of ch) + 17) % 26) + 65

Rotor 3: f(ch) is given by

A -> H, B -> U, C -> I, D -> P, E -> N, F -> W, G -> C, H -> K, I -> E, J -> X, K -> Q, L -> Z, M -> O, N -> V, O -> S, P -> B, Q -> L, R -> J, S -> D, T -> Y, U -> F, V -> R, W -> A, X -> G, Y -> T, Z -> M.

(after 5 bottles of glue, this is Ralph's contribution to the effort)

Some Details: Your output should look something like this:

......Decrypting Message....

enter first rotor number: 3

enter second rotor number: 1

thus, third rotor is 2

enter character: E

H

enter character: H

E

enter character: V

L

enter character: V

L

enter character: B

O

enter character: .

Reached end of message ... Terminating ....

When you submit: Before you submit, you should try to decrypt the following super-secret Homerfication:

23_ORI_BPFIY_KC_KF_D_MDG_DO_ORI_VPLFIL_PZ_HKFI_DFA_BDKF.

Additional thoughts: You will notice that the output above, though reflecting what you know of c++ so far, is not ideal. Ideally, the entire message would be stored and output all together on one line. This is easily done using strings or character arrays. But we haven't covered that yet. However, if you would like to (this is optional) include some "magic code" that makes your output look a little better, here it is:

cout<<"input char: ";

cin>>a_char;

cout<<"\033[F"<<"input char: "<<a_char;

a_char += 2; // this is my simple version of changing a character; your code would follow the above specs

cout<<" -> "<<a_char<<endl;

This code will produce output that looks like this

input char: A -> C

input char: B -> D

input char: C -> E

So the decrypted result is read down in the right column. Neat, eh? To clarify: the code above is magic.

And remember, as always, if you have any questions about the assignment, ask YOUR instructor.

Remember that the rotors won’t necessarily be applied in the order 1, 2, 3, however!

Note: You are to use the switch-case statement at least once in this assignment. You will probably find it intuitive to use the switch-case more than once! Your code should (from now on) validate user inputs. You will NOT use functions in this program. You will NOT use arrays in any way, either.