Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. # Enigma Challenge
  2.  
  3. 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.
  4.  
  5. ## Step 1, Rotation
  6.  
  7. 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:
  8.  
  9. Rotor | Notch
  10. -------|---------
  11. 1 | Q
  12. 2 | E
  13. 3 | V
  14. 4 | J
  15. 5 | Z
  16.  
  17.  
  18. On keypress the following steps occur:
  19.  
  20. - The rotor in Slot 1 rotates
  21. - If the rotor in Slot 1 moves past its notch, then it rotates the rotor in Slot 2.
  22. - If the rotor in Slot 2 is in its notch (but didn't just move there), both rotor 2 and 3 rotate once.
  23. - 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`
  24.  
  25. ## Step 2, Substitution
  26.  
  27. Each of the rotors performs a simple character substitution. The following is a chart of each of the rotors in the `A` position:
  28.  
  29. ```
  30. ABCDEFGHIJKLMNOPQRSTUVWXYZ
  31. --------------------------
  32. 1 EKMFLGDQVZNTOWYHXUSPAIBRCJ
  33. 2 AJDKSIRUXBLHWTMCQGZNPYFVOE
  34. 3 BDFHJLCPRTXVZNYEIWGAKMUSQO
  35. 4 ESOVPZJAYQUIRHXLNFTGKDCMWB
  36. 5 VZBRGITYUPSDNHLXAWMJQOFECK
  37. R YRUHQSLDPXNGOKMIEBFZCWVJAT
  38. ```
  39.  
  40. Rotor `1` in position `T` is `PAIBRCJEKMFLGDQVZNTOWYHXUS`, which would substitute the letter `C` for `I`.
  41.  
  42. 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.
  43.  
  44. Reverse substitution means that instead of Rotor `1` substituting `A` with `E`, it substitutes `E` with `A`
  45.  
  46. 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`.
  47.  
  48. ## Input/Output
  49.  
  50. Your function is given:
  51.  
  52. - A list of 3 rotors (as integers)
  53. - A list of 3 starting rotor positions (as letters)
  54. - A string that needs to be encrypted.
  55.  
  56. You can assume that your input will be well formed, and all characters will be uppercase letters, no spaces.
  57.  
  58. A function signature, in Java would look like:
  59.  
  60. ```java
  61. String encrypt(int[] rotors, char[] startPos, String clearText)
  62. ```
  63.  
  64. You must return the encrypted string.
  65.  
  66. ## Test cases
  67.  
  68. ```
  69. encrypt([4, 1, 5], ['H', 'P', 'G'], "AAAAAAAAA") -> "RPWKMBZLN"
  70. encrypt([1, 2, 3], ['A', 'A', 'A'], "PROGRAMMINGPUZZLES") -> "RTFKHDOVZSXTRMVPFC"
  71. encrypt([1, 2, 3], ['A', 'A', 'A'], "RTFKHDOVZSXTRMVPFC") -> "PROGRAMMINGPUZZLES"
  72. encrypt([2, 5, 3], ['U', 'L', 'I'], "GIBDZNJLGXZ") -> "UNCRACKABLE"
  73. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement