Skip to content

Instantly share code, notes, and snippets.

@vasilescur
Last active July 12, 2019 14:55
Show Gist options
  • Save vasilescur/31a79ad9ff3b4894c7aa0e57af041925 to your computer and use it in GitHub Desktop.
Save vasilescur/31a79ad9ff3b4894c7aa0e57af041925 to your computer and use it in GitHub Desktop.

Enigma Challenge

The Enigma machine is a fairly complex cipher machine used by the Germans and others during World War II to encrypt their messages. The Enigma code was famously broken by Alan Turing during the war, using one of the world's first "computers". 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 A to 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: P,U,H > Q,U,H > R,V,H > S,W,I

Step 2, Substitution

Each of the rotors performs a simple character substitution. The following is a chart of each of the rotors in the A position:

  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 C for I.

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 A with E, it substitutes E with A

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 O>Z>S>S. Therefore, Q is substituted with S.

Input/Output

Your function is given:

  • 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.

A function signature, in Java would look like:

String encrypt(int[] rotors, char[] startPos, String clearText)

You must return the encrypted string.

Test cases

encrypt([4, 1, 5], ['H', 'P', 'G'], "AAAAAAAAA") -> "RPWKMBZLN"
encrypt([1, 2, 3], ['A', 'A', 'A'], "PROGRAMMINGPUZZLES") -> "RTFKHDOVZSXTRMVPFC"
encrypt([1, 2, 3], ['A', 'A', 'A'], "RTFKHDOVZSXTRMVPFC") -> "PROGRAMMINGPUZZLES"
encrypt([2, 5, 3], ['U', 'L', 'I'], "GIBDZNJLGXZ") -> "UNCRACKABLE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment