Lesson 1 of 6
The single-layer key
- 18 min
- 5 checks
- 3 hints
- Go to task
Goal
Try every key and pick the one closest to real text
Explanation
This is the first challenge, and its hints give away less than the ones in the lessons. You have a message that was XORed with a single-byte key you don't know, and your job is to recover both the key and the message with your own code.
There are only 256 possible keys, so you can try them all. But many keys produce output made entirely of printable characters that still isn't readable text, so printable output alone isn't enough. Also note that chr(b).isalpha() is true for some bytes above 0x7f that look like letters, so count only lowercase English letters and spaces, not everything that method treats as a letter. The messages here are lowercase English sentences with spaces between the words, so the right key is the one whose output looks most like such a sentence.
Your task
Write a function crack(cipher) that returns the tuple (key, plaintext): the key, then the decrypted text as bytes. The checks try it on other messages with other keys, so copying the answer for the message in the code won't help.
Checks
5 checks- Recovers the key from a hidden message
- Decrypts another hidden message
- Tells an English sentence apart from letter-like symbols
- Recognizes a message that isn't encrypted
- Decrypts a message with digits into printable characters