|
1 | | -Implement a simple shift cipher like Caesar and a more secure substitution cipher. |
| 1 | +# Description |
2 | 2 |
|
3 | | -## Step 1 |
| 3 | +Create an implementation of the [Vigenère cipher][wiki]. |
| 4 | +The Vigenère cipher is a simple substitution cipher. |
4 | 5 |
|
5 | | -"If he had anything confidential to say, he wrote it in cipher, that is, |
6 | | -by so changing the order of the letters of the alphabet, that not a word |
7 | | -could be made out. If anyone wishes to decipher these, and get at their |
8 | | -meaning, he must substitute the fourth letter of the alphabet, namely D, |
9 | | -for A, and so with the others." |
10 | | -—Suetonius, Life of Julius Caesar |
| 6 | +## Cipher terminology |
11 | 7 |
|
12 | | -Ciphers are very straight-forward algorithms that allow us to render |
13 | | -text less readable while still allowing easy deciphering. They are |
14 | | -vulnerable to many forms of cryptanalysis, but we are lucky that |
15 | | -generally our little sisters are not cryptanalysts. |
| 8 | +A cipher is an algorithm used to encrypt, or encode, a string. |
| 9 | +The unencrypted string is called the _plaintext_ and the encrypted string is called the _ciphertext_. |
| 10 | +Converting plaintext to ciphertext is called _encoding_ while the reverse is called _decoding_. |
16 | 11 |
|
17 | | -The Caesar Cipher was used for some messages from Julius Caesar that |
18 | | -were sent afield. Now Caesar knew that the cipher wasn't very good, but |
19 | | -he had one ally in that respect: almost nobody could read well. So even |
20 | | -being a couple letters off was sufficient so that people couldn't |
21 | | -recognize the few words that they did know. |
| 12 | +In a _substitution cipher_, each plaintext letter is replaced with a ciphertext letter which is computed with the help of a _key_. |
| 13 | +(Note, it is possible for replacement letter to be the same as the original letter.) |
22 | 14 |
|
23 | | -Your task is to create a simple shift cipher like the Caesar Cipher. |
24 | | -This image is a great example of the Caesar Cipher: |
| 15 | +## Encoding details |
25 | 16 |
|
26 | | -![Caesar Cipher][1] |
| 17 | +In this cipher, the key is a series of lowercase letters, such as `"abcd"`. |
| 18 | +Each letter of the plaintext is _shifted_ or _rotated_ by a distance based on a corresponding letter in the key. |
| 19 | +An `"a"` in the key means a shift of 0 (that is, no shift). |
| 20 | +A `"b"` in the key means a shift of 1. |
| 21 | +A `"c"` in the key means a shift of 2, and so on. |
27 | 22 |
|
28 | | -For example: |
| 23 | +The first letter of the plaintext uses the first letter of the key, the second letter of the plaintext uses the second letter of the key and so on. |
| 24 | +If you run out of letters in the key before you run out of letters in the plaintext, start over from the start of the key again. |
29 | 25 |
|
30 | | -Giving "iamapandabear" as input to the encode function returns the cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in transit. |
| 26 | +If the key only contains one letter, such as `"dddddd"`, then all letters of the plaintext are shifted by the same amount (three in this example), which would make this the same as a rotational cipher or shift cipher (sometimes called a Caesar cipher). |
| 27 | +For example, the plaintext `"iamapandabear"` would become `"ldpdsdqgdehdu"`. |
31 | 28 |
|
32 | | -When "ldpdsdqgdehdu" is put into the decode function it would return |
33 | | -the original "iamapandabear" letting your friend read your original |
34 | | -message. |
| 29 | +If the key only contains the letter `"a"` (one or more times), the shift distance is zero and the ciphertext is the same as the plaintext. |
35 | 30 |
|
36 | | -## Step 2 |
| 31 | +Usually the key is more complicated than that, though! |
| 32 | +If the key is `"abcd"` then letters of the plaintext would be shifted by a distance of 0, 1, 2, and 3. |
| 33 | +If the plaintext is `"hello"`, we need 5 shifts so the key would wrap around, giving shift distances of 0, 1, 2, 3, and 0. |
| 34 | +Applying those shifts to the letters of `"hello"` we get `"hfnoo"`. |
37 | 35 |
|
38 | | -Shift ciphers are no fun though when your kid sister figures it out. Try |
39 | | -amending the code to allow us to specify a key and use that for the |
40 | | -shift distance. This is called a substitution cipher. |
| 36 | +## Random keys |
41 | 37 |
|
42 | | -Here's an example: |
| 38 | +If no key is provided, generate a key which consists of at least 100 random lowercase letters from the Latin alphabet. |
43 | 39 |
|
44 | | -Given the key "aaaaaaaaaaaaaaaaaa", encoding the string "iamapandabear" |
45 | | -would return the original "iamapandabear". |
46 | | - |
47 | | -Given the key "ddddddddddddddddd", encoding our string "iamapandabear" |
48 | | -would return the obscured "ldpdsdqgdehdu" |
49 | | - |
50 | | -In the example above, we've set a = 0 for the key value. So when the |
51 | | -plaintext is added to the key, we end up with the same message coming |
52 | | -out. So "aaaa" is not an ideal key. But if we set the key to "dddd", we |
53 | | -would get the same thing as the Caesar Cipher. |
54 | | - |
55 | | -## Step 3 |
56 | | - |
57 | | -The weakest link in any cipher is the human being. Let's make your |
58 | | -substitution cipher a little more fault tolerant by providing a source |
59 | | -of randomness and ensuring that the key contains only lowercase letters. |
60 | | - |
61 | | -If someone doesn't submit a key at all, generate a truly random key of |
62 | | -at least 100 alphanumeric characters in length. |
63 | | - |
64 | | -## Extensions |
65 | | - |
66 | | -Shift ciphers work by making the text slightly odd, but are vulnerable |
67 | | -to frequency analysis. Substitution ciphers help that, but are still |
68 | | -very vulnerable when the key is short or if spaces are preserved. Later |
69 | | -on you'll see one solution to this problem in the exercise |
70 | | -"crypto-square". |
71 | | - |
72 | | -If you want to go farther in this field, the questions begin to be about |
73 | | -how we can exchange keys in a secure way. Take a look at [Diffie-Hellman |
74 | | -on Wikipedia][dh] for one of the first implementations of this scheme. |
75 | | - |
76 | | -[1]: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Caesar_cipher_left_shift_of_3.svg/320px-Caesar_cipher_left_shift_of_3.svg.png |
77 | | -[dh]: https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange |
| 40 | +[wiki]: https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher |
0 commit comments