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