The Enigma Machine - Part 3: The Reflector
Description:
In this series of Kata, we will be implementing a software version of the Enigma Machine.
The Enigma Machine was a message enciphering/deciphering machine used during the Second World War for disguising the content of military communications. Alan Turing - the father of computing - formulated and developed concepts that are the basis of all computers in use today, he did this in response to the vital need to break those military communications. Turing and his colleagues at Bletchley Park are generally recognised as being responsible for shortening WWII by two years and saving an estimated 22 Mi`llion lives.
The Enigma Machine consisted of a number of parts: Keyboard for input, rotors and plugboard for enciphering, and lampboard for output.
We will simulate input and output with strings, and build the rotors, plugboard and mechanism that used them in software. As we progress the code will become more complex, so you are advised to attempt them in order.
Step 1: The Plugboard (Kata)
Step 2: A Rotor (Kata)
Step 3: The Reflector (Wikipedia)
In this Kata, you must implement the reflector component. The reflector is a device that acts like a plugboard, and can act like a rotor all at the same time. It cross-wires all letters together in pairs, so that an input of one letter always generated output as a different letter. The reflector was placed to the left of the left-most rotor in the Enigma Machine, modified the incoming signal and then reflected it back through the rotors in reverse order.
The reflector meant that with the same initial settings typing a plaintext message generated cipher-text on the lampboard and typing cipher-text generated plaintext. However, it would never allow a letter to be enciphered to itself, and it was this critical cryptographic flaw that enabled the reverse engineering of the Enigma Machine by Turing and his cohort.
Physical Description
The reflector was initially a very simple device. It had 13 fixed connections between 26 input/output terminals and did not move. However in later versions it could be configured in a number of ways, and its cryptographic value evolved with certain iterations of the Enigma Machine as it was improved.
The second version allowed the reflector to be inserted into the machine in either of two positions, the third version allowed for 26 positions, the fourth iteration allowed for additional rotation like the rotors and finally in a the fifth version, the reflector could be rewired internally, like a miniature 13-wire plugboard.
Nomenclature
For the sake of clarity we will denote:
- a reflector input/output with i, and,
- a rotational position of the reflector with p.
Initial Position Effect
If we have wiring so that in position PA, there is a mapping rA <-> rB, then inserting the same reflector in position pN would mean there is a mapping rN <-> rO.
Rotation Effect
If we have wiring so that in position pA, there is a mapping rA <-> rB, then after the first rotation we have a mapping rB <-> rC, after the second rotation we have a mapping rC <-> rD, and so on. As with the rotors, rotation occurs before encipherment.
Note
In the actual usage of the original Enigma Machine, punctuation was encoded as words transmitted in the stream, in our code, anything that is not in the range A-Z will be returned unchanged.
Kata
You will attempt to support all the different types of reflector, so the constructor inputs will allow us to define the internal wiring, the valid starting positions, the actual start position and whether the rotation input from the external mechanism would cause the reflector to rotate.
The Reflector
class you will implement, will:
- Construct with:
- A list of wire pairs in the form of a 26 character string where each pair member maps to its opposite, e.g. "ABCD..." would map rA <-> rB, rC <-> rD, etc.,
- A list a of valid starting positions, e.g. "AC" meaning the first and third positions,
- The starting position, e.g. "C" meaning the third position
- A boolean flag indicating if the reflector supports rotation.
2. Validate the state is legitimate. If it is not, raise an exception.
3. Implement the method to translate a single character of mechanism input into a mechanism output, and simulate rotation if supported.
Python example:
print "Simpler"
# A Non-rotating 2 position rotor, starting in the 2nd position
reflector = Reflector("AYBRCUDHEQFSGLIPJXKNMOTZVW", "AN", "N", False)
print reflector.process("A", True) # Rotation is ignored, this rotor does not rotate
print reflector.process("B", True)
print reflector.process("X", True)
print reflector.process("!", True)
print "More complex"
# A Rotating 26 position rotor starting in the 8th position
reflector = Reflector("AYBRCUDHEQFSGLIPJXKNMOTZVW", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "H", True)
print reflector.process("A", True)
print reflector.process("B", True)
print reflector.process("X", True)
print reflector.process("!", True)
Expected output:
Simpler
X
Z
A
!
More complex
H
E
T
!
Similar Kata:
Stats:
Created | Apr 7, 2015 |
Published | Apr 7, 2015 |
Warriors Trained | 70 |
Total Skips | 12 |
Total Code Submissions | 137 |
Total Times Completed | 10 |
Python Completions | 10 |
Total Stars | 17 |
% of votes with a positive feedback rating | 100% of 3 |
Total "Very Satisfied" Votes | 3 |
Total "Somewhat Satisfied" Votes | 0 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 4 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 6 kyu |