Lab 4: A Simple Enigma Model
At this point, we've finished our transition to Java, and can do everything in Java we could do in C++ (plus member functions! and constructors!). This lab should finish tying all that together.
History
Substitution ciphers that encode a message by substituting one character for another go back at least as far as Julius Caesar, who used a rotating character scheme to encode military orders. This simple type of encryption is extremely vulnerable to statistical attacks, however. In World War II, the Nazi military employed an encryption scheme that addressed this weakness of simple substitution ciphers. This scheme, implemented by typewriter-sized devices known as Enigma machines, was believed by the Nazis to be unbreakable. This belief was grounded in the fact that by the time the huge amount of computational work necessary to break the code was completed, the message itself would be irrelevant.
Polish mathematicians, followed by researchers at Bletchley Park, England (led by Alan Turing), built machines known as bombe (from the Polish "bomba kryptologiczna," or "cryptologic bomb") to run through the computation and allow for timely decryption of Nazi messages. This is hailed as one of the turning points of the war, and the first great success of computer science.
Simple Model of the Enigma
Enigma machines used interchangeable rotors that could be placed in different orientations to obtain different substitution patterns. More significantly, the rotors rotated after each character was encoded, changing the substitution pattern and making the code very difficult to break. The behavior of the rotating rotors can be modeled, in a simplified form, by a device consisting of labeled, concentric rings. For example, the model below has three rings labeled with the letters of the alphabet and '#' (representing a space).
To encrypt a character using this model, find the character on the inner rotor (i.e., the inside ring) and note the character aligned with it on the outer rotor (i.e., the outside ring), then find that character on the middle rotor (i.e., the middle ring) and output the one aligned with it on the outer rotor. After a character is encrypted, turn the inner rotor clockwise one step. Whenever the inner rotor returns to its original orientation, the middle rotor turns once in lock-step, just like the odometer in a car.
For example, in this configuration the character 'A' would be encrypted as 'N', since 'A' on the inner rotor is aligned with 'H' on the outer rotor, and 'H' on the middle rotor is aligned with 'N' on the outer rotor. After performing this encryption, the inner rotor is rotated clockwise, so the letter 'A' would next be encrypted as 'D'.
Note that decrypting a message requires following the same steps, only in reverse (i.e., find the character on the outer rotor, note the character aligned with it on the middle rotor, find that character on the outer rotor, then output the character aligned with it on the inner rotor).
Real Enigma Machine
A standard enigma machines used three rotors. Special units had extra for added complexity. You can see the rotors in the picture on the right. A special codebook told the operators which rotors to use and how to orient them to begin each message. The code changed at the same time every night. The job of Bletchley Park was largely to figure out which three rotors were in use that day. Once they did that, they could decode all German military traffic for the day. The channel would 'go dark' again at midnight when the codes changed again.
Prior to encoding or decoding a message, the operator would put the three rotors in and set them to the problem starting position. To encode, they would type the plaintext message on the keyboard and read the output from the 'lampboard'. The lampboard is a set of letters in qwerty layout that light up. So pressing the 'A' in the example above would cause the letter 'N' to light up on the lampboard. Decoding was done in a similar manner.
The plugboard was an additional feature that added complexity to the encoding algorithm. It allowed the machine to swap two letters before encoding. This made decrypting an Enigma transmission by hand much more difficult, and required the use of machines to encode or decode.
The Assignment
For this assignment, you are to design a Java class named Enigma and a Java class named Rotor to simulate this three-ring model. A Rotor consists of two things:
- A string or array of 27 characters that encode the order and placement of the letters. For example, in the picture above, the outer rotor would be stored with an array holding the characters #BDFHJLNPRTVXZACEGIKMOQSUWY. The initial settings of the inner and middle rotors in the above diagram are #GNUAHOVBIPWCJQXDKRYELSZFMT and #EJOTYCHMRWAFKPUZDINSXBGLQV, respectively.
- A character holding the character that began as the first one in the sequence. In the case of all three above rotors, this is '#'.
Your Rotor should be able to do the following things:
- Be constructed, using a String as input.
- Rotate one click. This should involve an alteration of the array of characters.
- Return the index in the array that a character appears.
- Return the character at a given index.
Your Enigma class should consist only of three Rotors. I'm not going to give as much guidance on what an Enigma should be able to do, but at the very least, it should have methods called "encrypt" and "decrypt" that accept a String as an argument, and return another String.
This file should be the main function of your program. You just have to build Enigma and Rotor.
Once you have your model working, test it by decoding the following message using the diagram settings:
XXMMJ#UBHRWSHYSSTGWIGMMMAKTFSWZD#PU#CZIQADCDQY#NHN#SJVJTKZVVHOZBABLYMWBWWLNGAWWXEBWNXQQBQQRNMJGYXRYBKISBBFHGOO
Turn in your completed program as Lab04.
This lab originally written by Prof. David Reed, Creighton University