The Enigma machine is a fairly complex cipher machine used by the Germans and others to encrypt their messages. It is your job to implement this machine*.
Step 1, Rotation
Our enigma machine has 3 slots for rotors, and 5 available rotors for each of these slots. Each rotor has 26 different possible positions (from
Z). Each rotor has a predefined notch position:
Rotor Notch ------------ 1 Q 2 E 3 V 4 J 5 Z
On keypress the following steps occur:
- The rotor in Slot 1 rotates
- If the rotor in Slot 1 moves past its notch, then it rotates the rotor in Slot 2.
- If the rotor in Slot 2 is in its notch (but didn't just move there), both rotor 2 and 3 rotate once.
If we are using rotors 1,3,5 and they are in positions
P,U,H then the sequence of positions is:
Step 2, Substitution
Each of the rotors performs a simple character substitution. The following is a chart of each of the rotors in the
ABCDEFGHIJKLMNOPQRSTUVWXYZ -------------------------- 1 EKMFLGDQVZNTOWYHXUSPAIBRCJ 2 AJDKSIRUXBLHWTMCQGZNPYFVOE 3 BDFHJLCPRTXVZNYEIWGAKMUSQO 4 ESOVPZJAYQUIRHXLNFTGKDCMWB 5 VZBRGITYUPSDNHLXAWMJQOFECK R YRUHQSLDPXNGOKMIEBFZCWVJAT
Rotor 1 in position T is
PAIBRCJEKMFLGDQVZNTOWYHXUS, which would substitute the letter
After the three rotors perform their substitution, the reflector is hit (listed as
R above). It performs its own substitution, and then reflects the signal back through the rotors. The rotors then perform a reverse substitution in reverse order.
Reverse substitution means that instead of Rotor 1 substituting
E, it substitutes
Slots are filled with rotors 1,2,3 all in position
A. The letter
Q follows the path
Q>X>V>M through the rotors.
M reflects to
O, which then follows the reverse path of
A is substituted with
You are passed:
- A list of 3 rotors (as integers)
- A list of 3 starting rotor positions (as letters)
- A string that needs to be encrypted.
You can assume that your input will be well formed, and all characters will be uppercase letters, no spaces.
You must return the encrypted string.
You can optionally accept the rotors, notches, and reflectors as input. For those that don't can take off 95 bytes from their score, as
95 = ceil(log2(26 letters ^(26*6 rotors +5 notches))/8 bytes)
Rotor Position Input Output 4,1,5 H,P,G AAAAAAAAA RPWKMBZLN 1,2,3 A,A,A PROGRAMMINGPUZZLES RTFKHDOVZSXTRMVPFC 1,2,3 A,A,A RTFKHDOVZSXTRMVPFC PROGRAMMINGPUZZLES 2,5,3 U,L,I GIBDZNJLGXZ UNCRACKABLE
My implementation can be found on Github. I've tested it, but I may have bugs in my implementation (which would mean that my test cases are likely wrong).
*I've tried to make this as accurate as possible, but due to the variations between machines, I may have some details wrong. However, your task is to implement what I've described, even if I'm inaccurate. I'm not including the plugboard for simplicity