|
| 1 | +# Simple Cipher |
| 2 | + |
| 3 | +Implement a simple shift cipher like Caesar and a more secure substitution cipher. |
| 4 | + |
| 5 | +## Step 1 |
| 6 | + |
| 7 | +"If he had anything confidential to say, he wrote it in cipher, that is, |
| 8 | +by so changing the order of the letters of the alphabet, that not a word |
| 9 | +could be made out. If anyone wishes to decipher these, and get at their |
| 10 | +meaning, he must substitute the fourth letter of the alphabet, namely D, |
| 11 | +for A, and so with the others." |
| 12 | +—Suetonius, Life of Julius Caesar |
| 13 | + |
| 14 | +Ciphers are very straight-forward algorithms that allow us to render |
| 15 | +text less readable while still allowing easy deciphering. They are |
| 16 | +vulnerable to many forms of cryptoanalysis, but we are lucky that |
| 17 | +generally our little sisters are not cryptoanalysts. |
| 18 | + |
| 19 | +The Caesar Cipher was used for some messages from Julius Caesar that |
| 20 | +were sent afield. Now Caesar knew that the cipher wasn't very good, but |
| 21 | +he had one ally in that respect: almost nobody could read well. So even |
| 22 | +being a couple letters off was sufficient so that people couldn't |
| 23 | +recognize the few words that they did know. |
| 24 | + |
| 25 | +Your task is to create a simple shift cipher like the Caesar Cipher. |
| 26 | +This image is a great example of the Caesar Cipher: |
| 27 | + |
| 28 | +![Caesar Cipher][1] |
| 29 | + |
| 30 | +For example: |
| 31 | + |
| 32 | +Giving "iamapandabear" as input to the encode function returns the cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in transit. |
| 33 | + |
| 34 | +When "ldpdsdqgdehdu" is put into the decode function it would return |
| 35 | +the original "iamapandabear" letting your friend read your original |
| 36 | +message. |
| 37 | + |
| 38 | +## Step 2 |
| 39 | + |
| 40 | +Shift ciphers are no fun though when your kid sister figures it out. Try |
| 41 | +amending the code to allow us to specify a key and use that for the |
| 42 | +shift distance. This is called a substitution cipher. |
| 43 | + |
| 44 | +Here's an example: |
| 45 | + |
| 46 | +Given the key "aaaaaaaaaaaaaaaaaa", encoding the string "iamapandabear" |
| 47 | +would return the original "iamapandabear". |
| 48 | + |
| 49 | +Given the key "ddddddddddddddddd", encoding our string "iamapandabear" |
| 50 | +would return the obscured "lpdsdqgdehdu" |
| 51 | + |
| 52 | +In the example above, we've set a = 0 for the key value. So when the |
| 53 | +plaintext is added to the key, we end up with the same message coming |
| 54 | +out. So "aaaa" is not an ideal key. But if we set the key to "dddd", we |
| 55 | +would get the same thing as the Caesar Cipher. |
| 56 | + |
| 57 | +## Step 3 |
| 58 | + |
| 59 | +The weakest link in any cipher is the human being. Let's make your |
| 60 | +substitution cipher a little more fault tolerant by providing a source |
| 61 | +of randomness and ensuring that the key is not composed of numbers or |
| 62 | +capital letters. |
| 63 | + |
| 64 | +If someone doesn't submit a key at all, generate a truly random key of |
| 65 | +at least 100 characters in length, accessible via Cipher#key (the # |
| 66 | +syntax means instance variable) |
| 67 | + |
| 68 | +If the key submitted has capital letters or numbers, throw an |
| 69 | +ArgumentError with a message to that effect. |
| 70 | + |
| 71 | +## Extensions |
| 72 | + |
| 73 | +Shift ciphers work by making the text slightly odd, but are vulnerable |
| 74 | +to frequency analysis. Substitution ciphers help that, but are still |
| 75 | +very vulnerable when the key is short or if spaces are preserved. Later |
| 76 | +on you'll see one solution to this problem in the exercise |
| 77 | +"crypto-square". |
| 78 | + |
| 79 | +If you want to go farther in this field, the questions begin to be about |
| 80 | +how we can exchange keys in a secure way. Take a look at [Diffie-Hellman |
| 81 | +on Wikipedia][dh] for one of the first implementations of this scheme. |
| 82 | + |
| 83 | +[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 |
| 84 | +[dh]: http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange |
| 85 | + |
| 86 | + |
| 87 | +## Getting Started |
| 88 | + |
| 89 | +For installation and learning resources, refer to the |
| 90 | +[exercism help page](http://exercism.io/languages/haskell). |
| 91 | + |
| 92 | +## Running the tests |
| 93 | + |
| 94 | +To run the test suite, execute the following command: |
| 95 | + |
| 96 | +```bash |
| 97 | +stack test |
| 98 | +``` |
| 99 | + |
| 100 | +#### If you get an error message like this... |
| 101 | + |
| 102 | +``` |
| 103 | +No .cabal file found in directory |
| 104 | +``` |
| 105 | + |
| 106 | +You are probably running an old stack version and need |
| 107 | +to upgrade it. |
| 108 | + |
| 109 | +#### Otherwise, if you get an error message like this... |
| 110 | + |
| 111 | +``` |
| 112 | +No compiler found, expected minor version match with... |
| 113 | +Try running "stack setup" to install the correct GHC... |
| 114 | +``` |
| 115 | + |
| 116 | +Just do as it says and it will download and install |
| 117 | +the correct compiler version: |
| 118 | + |
| 119 | +```bash |
| 120 | +stack setup |
| 121 | +``` |
| 122 | + |
| 123 | +## Running *GHCi* |
| 124 | + |
| 125 | +If you want to play with your solution in GHCi, just run the command: |
| 126 | + |
| 127 | +```bash |
| 128 | +stack ghci |
| 129 | +``` |
| 130 | + |
| 131 | +## Feedback, Issues, Pull Requests |
| 132 | + |
| 133 | +The [exercism/xhaskell](https://github.com/exercism/xhaskell) repository on |
| 134 | +GitHub is the home for all of the Haskell exercises. |
| 135 | + |
| 136 | +If you have feedback about an exercise, or want to help implementing a new |
| 137 | +one, head over there and create an issue. We'll do our best to help you! |
| 138 | + |
| 139 | +## Source |
| 140 | + |
| 141 | +Substitution Cipher at Wikipedia [http://en.wikipedia.org/wiki/Substitution_cipher](http://en.wikipedia.org/wiki/Substitution_cipher) |
| 142 | + |
| 143 | +## Submitting Incomplete Solutions |
| 144 | +It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
| 145 | + |
0 commit comments