|
| 1 | +I've left a secret message for you in a text file [here](media/secret.txt). Because it's very |
| 2 | +secret I've encrypted it using a Caesar cipher. |
| 3 | + |
| 4 | +Your task is to write a function to decrypt Caesar ciphertext. First let's understand what a Caesar |
| 5 | +cipher does with an example: |
| 6 | + |
| 7 | +Here is some text to encrypt: `"I CAN HAZ CHEESEBURGER"`. The Caesar cipher also needs a "key" which is a number |
| 8 | +between 1 and 26. This number is used to "rotate" the plaintext input characters. For this example, |
| 9 | +the key is `1`. |
| 10 | + |
| 11 | +The first letter is `"I"`. `"I"` "rotated" by `1` is the letter after `"I"` which is `"J"`. So the |
| 12 | +encrypted ciphertext is `"J"` so far. |
| 13 | + |
| 14 | +The next letter is `" "`. Spaces are not changed by encryption and decryption so the encrypted |
| 15 | +ciphertext is now: `"J "`. |
| 16 | + |
| 17 | +The same logic applies to all lettes in the string up until `"Z"`. At this point the plaintext, |
| 18 | +`"I CAN HA"` has been encryped to `"J DBO IB"`. To encrypt a `"Z"` you count back again from the |
| 19 | +beginning of the alphabet. So the plaintext `"Z"` is encrypted to `"A"`. |
| 20 | + |
| 21 | +This is why it's called a "rotation". If you write the latin alphabet, A...Z on a circular piece of |
| 22 | +paper. Then again on a slightly smaller piece of paper. Rotate one of the circular pieces of paper by |
| 23 | +the key and this gives the mapping between plaintext letters and ciphertext letters. |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +The final encryption of the string `"I CAN HAZ CHEESEBURGER"` with a key of `1` is then: |
| 28 | +`"J DBO IBA DIFFTFCVSHFS"`. |
| 29 | + |
| 30 | +Your task is to write a function to decode caesar ciphers called `decrypt`. Your function should accept |
| 31 | +2 arguments: |
| 32 | + |
| 33 | +- The encrypted ciphertext (as uninterpreted binary data type: `bytes`) |
| 34 | +- The key (type: `int`) |
| 35 | + |
| 36 | +Your function should return a `str`. |
| 37 | + |
| 38 | +You can use this template: |
| 39 | + |
| 40 | +```python |
| 41 | +def decrypt(ciphertext: bytes, key: int) -> str: |
| 42 | + ... |
| 43 | +``` |
| 44 | + |
| 45 | + |
| 46 | +You should also `print()` my message to the console using your `decrypt` function. |
| 47 | +You can use a key of `13` to decode my message. The message is in a file called, `"secret.txt"`. |
| 48 | + |
| 49 | +## Example |
| 50 | + |
| 51 | +```console?lang=python&prompt=>>> |
| 52 | +>>> decrypt(b'B NFTTBHF', 1) |
| 53 | +"A MESSAGE" |
| 54 | +>>> decrypt(b'', 12) |
| 55 | +"" |
| 56 | +>>> decrypt(b'QBUN CM SIOL HUGY', 6) |
| 57 | +"WHAT IS YOUR NAME" |
| 58 | +>>> decrypt(b'HELLO WORLD', 26) |
| 59 | +"HELLO WORLD" |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +Do not forget to include a docstring on your `decrypt` function and a test function called |
| 64 | +`test_decrypt` that returns `"Success"` if the `decrypt` function passes all tests. |
0 commit comments