Eric Roberts Handout #31 CS 106A February 3, 2016 The Machine

Cryptography— Park The

Bletchley Eric Roberts CS 106A I’ve twice had the opportunity to teach at Stanford February 3, 2016 in Oxford, which made it possible to visit Bletchley Park—the home of the Government Code and School (GCCS), where the Enigma code was broken.

Cryptography—Bletchley Park Overview of Assignment #4 Part 1: Check that a key is legal In a letter-, the key must be a permutation of the alphabet, which means that it contains all the letters of the alphabet in some order. Your job in Part 1 is to add code to check that the key entered in the LetterSubstitutionCipher program is legal, as in the following sample run:

LetterSubstitutionCipher Letter-substitution cipher. Enter 26-letter key: SHORTKEY That key is illegal. Enter 26-letter key: AABCDEFGHIJKLMNOPQRSTUVWXY That key is illegal. Enter 26-letter key: BCDEFGHIJKLMNOPQRSTUVWXYZA Plaintext: HAL 9000 In our field trips to Bletchley Park, our tour was led by Jean Ciphertext: IBM 9000 Valentine, who worked with the decryption machine shown in these pictures.

Overview of Assignment #4 Overview of Assignment #4 Part 2: Invert a key Part 3: Simulate the Enigma Machine In a letter-substitution cipher, the sender and receiver must use different keys, one for encryption and one for decryption. Your job in Part 2 is to write an invertKey method that determines the decryption key from the encryption key by going through the alphabet and, for each letter, figuring out what plaintext letter would have produced that letter in the ciphertext. Encryption key: ABCDEFGH I JKLMNOPQRSTUVWXYZ |||||||||||||||||||||||||| LZDRXPEAJYBQWFVI HCTGNOMKSU

Decryption key: A B C D E F G H I J K L MN O P Q RWS T U V X Y Z | | | | | | | | | | | | | | | | ||| | | ||| | | H (you should practice filling in the rest) – 2 –

The Enigma Rotors

The Enigma Structure Operation of the Enigma Machine • Whenever the operator types a letter key on the Enigma, the following things happen: 1. The force of the key press advances the fast rotor one position. If the indicator on the fast rotor wraps around from Z to A, that action “carries” to the medium rotor, just like the digits on an odometer. Similarly, if the medium rotor passes Z, the slow rotor advances one position. 2. An electrical signal is fed into the wire corresponding to the key, which then flows through seven letter-substitution steps: – Through the fast rotor from right to left. – Through the medium rotor from right to left. – Through the slow rotor from right to left. – Through the reflector, which turns the signal around. – Through the slow rotor from left to right. – Through the medium rotor from left to right. – Through the fast rotor from left to right and then on to the lamp.

Encoding the Letter “A” The Next “A” Is Different – 3 –

What Made Enigma Vulnerable? Breaking the Enigma Code • Early in the war, mathematicians working for the Polish • The most common technique used at Bletchley Park was the Resistance were able to smuggle Enigma machines to France known-plaintext attack, in which the codebreakers guess that and along with a strategy for . a particular sequence of characters exists somewhere in the • The British government established a top-secret cryptography decoded message. A sequence of characters that you guess is center at Bletchley Park and staffed it with the top British part of the plaintext is called a crib. mathematicians. • Breaking an Enigma message required the following steps: • The decryption team at Bletchley was able to exploit the – Align the crib with the ciphertext to eliminate crashes in which following facts about the Enigma machine: a letter appears to map to itself. – The encoding is symmetrical. – Create a menu recording the links between letter pairs in the – The Enigma machine can never map a character into itself. crib and ciphertext. • The codebreakers were also helped by the fact that the – Identify loops in the menu at which a chain of letter pairs Germans were both careless and overconfident. In believing cycles back to the original letter. they had an unbreakable encoding machine, they failed to – Use the loops in the menu to create a wiring pattern for an take adequate measures to safeguard the integrity of their electromechanical device called a Bombe that searches for communications. settings of the Enigma rotors that produce the observed pattern.

Step 1: Align the Crib and Ciphertext Step 2: Construct the Menu Offset 0: V R L B Z P W M E P M I H F S R J X F M J K W R A K E I N E B E S O N D E R E N E R E I G N I S S E U A E N F V R L B Z P W M E P M I H F S R J X F M J K W R A X Q E Z 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 K E I N E B E S O N D E R E N E R E I G N I S S E

This offset crashes at positions 3 and 13. 0 L K V

Offset 1: 2 21 U A E N F V R L B Z P W M E P M I H F S R J X F M J K W R A X Q E Z 18 5 K E I N E B E S O N D E R E N E R E I G N I S S E O F I H P B

This offset crashes at position 1. 8 3 13 11 12 9

Offset 5: 4 1, 15 16 20 Z E R J N G U A E N F V R L B Z P W M E P M I H F S R J X F M J K W R A X Q E Z

6 23 K E I N E B E S O N D E R E N E R E I G N I S S E 24 17 14 19

No crashes exist in this alignment, so it is a feasible solution. 22 7 10 A X W S M D

Step 3: Find the Loops V R L B Z P W M E P M I H F S R J X F M J K W R A K E I N E B E S O N D E R E N E R E I G N I S S E 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

0 L K V

2 21

18 5 O F I H P B

8 3 13 11 12 9

4 1, 15 16 20 Z E R J N G

6 23 24 17 14 19

22 7 10 A X W S M D