Code

Try Every Key

Pip found a scrap of paper in a bottle: Wkh pds lv klgghq. It's a shift cipher, but nobody knows the key. Can we still read it?

Yes! A shift cipher only has 26 possible keys. That's tiny for a computer. We can simply try them all and look. Trying every possible key is called a brute-force attack.

for key in range(26):
    print(key, shift_text(secret, -key))

This is why shift ciphers are only for fun. Real security tools have so many possible keys that trying them all would take longer than the age of the universe.

Assignment

shift_text(text, shift) is ready for you (it's your cipher from chapter 1).

  1. all_rotations(secret) returns a list of 26 strings. The item at index key is shift_text(secret, -key). So index 0 is the secret itself.
  2. Loop over the list and print each line as key 3: ... (the key, a colon, then the text).