|
| 1 | +## Affine Cipher |
| 2 | + |
| 3 | +Create an implementation of the affine cipher, |
| 4 | +an ancient encryption system created in the Middle East. |
| 5 | + |
| 6 | +The affine cipher is a type of monoalphabetic substitution cipher. |
| 7 | +Each character is mapped to its numeric equivalent, encrypted with |
| 8 | +a mathematical function and then converted to the letter relating to |
| 9 | +its new numeric value. Although all monoalphabetic ciphers are weak, |
| 10 | +the affine cypher is much stronger than the atbash cipher, |
| 11 | +because it has many more keys. |
| 12 | + |
| 13 | +the encryption function is: |
| 14 | + |
| 15 | + `E(x) = (ax + b) mod m` |
| 16 | + - where `x` is the letter's index from 0 - length of alphabet - 1 |
| 17 | + - `m` is the length of the alphabet. For the roman alphabet `m == 26`. |
| 18 | + - and `a` and `b` make the key |
| 19 | + |
| 20 | +the decryption function is: |
| 21 | + |
| 22 | + `D(y) = a^-1(y - b) mod m` |
| 23 | + - where `y` is the numeric value of an encrypted letter, ie. `y = E(x)` |
| 24 | + - it is important to note that `a^-1` is the modular multiplicative inverse |
| 25 | + of `a mod m` |
| 26 | + - the modular multiplicative inverse of `a` only exists if `a` and `m` are |
| 27 | + coprime. |
| 28 | + |
| 29 | +To find the MMI of `a`: |
| 30 | + |
| 31 | + `an mod m = 1` |
| 32 | + - where `n` is the modular multiplicative inverse of `a mod m` |
| 33 | + |
| 34 | +More information regarding how to find a Modular Multiplicative Inverse |
| 35 | +and what it means can be found [here.](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) |
| 36 | + |
| 37 | +Because automatic decryption fails if `a` is not coprime to `m` your |
| 38 | +program should return status 1 and `"Error: a and m must be coprime."` |
| 39 | +if they are not. Otherwise it should encode or decode with the |
| 40 | +provided key. |
| 41 | + |
| 42 | +The Caesar (shift) cipher is a simple affine cipher where `a` is 1 and |
| 43 | +`b` as the magnitude results in a static displacement of the letters. |
| 44 | +This is much less secure than a full implementation of the affine cipher. |
| 45 | + |
| 46 | +Ciphertext is written out in groups of fixed length, the traditional group |
| 47 | +size being 5 letters, and punctuation is excluded. This is to make it |
| 48 | +harder to guess things based on word boundaries. |
| 49 | + |
| 50 | +## Examples |
| 51 | + |
| 52 | + - Encoding `test` gives `ybty` with the key a=5 b=7 |
| 53 | + - Decoding `ybty` gives `test` with the key a=5 b=7 |
| 54 | + - Decoding `ybty` gives `lqul` with the wrong key a=11 b=7 |
| 55 | + - Decoding `kqlfd jzvgy tpaet icdhm rtwly kqlon ubstx` |
| 56 | + - gives `thequickbrownfoxjumpsoverthelazydog` with the key a=19 b=13 |
| 57 | + - Encoding `test` with the key a=18 b=13 |
| 58 | + - gives `Error: a and m must be coprime.` |
| 59 | + - because a and m are not relatively prime |
| 60 | + |
| 61 | +### Examples of finding a Modular Multiplicative Inverse (MMI) |
| 62 | + |
| 63 | + - simple example: |
| 64 | + - `9 mod 26 = 9` |
| 65 | + - `9 * 3 mod 26 = 27 mod 26 = 1` |
| 66 | + - `3` is the MMI of `9 mod 26` |
| 67 | + - a more complicated example: |
| 68 | + - `15 mod 26 = 15` |
| 69 | + - `15 * 7 mod 26 = 105 mod 26 = 1` |
| 70 | + - `7` is the MMI of `15 mod 26` |
| 71 | + |
| 72 | +## Setup |
| 73 | + |
| 74 | +Go through the setup instructions for Javascript to install the necessary |
| 75 | +dependencies: |
| 76 | + |
| 77 | +[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation) |
| 78 | + |
| 79 | +## Requirements |
| 80 | + |
| 81 | +Please `cd` into exercise directory before running all below commands. |
| 82 | + |
| 83 | +Install assignment dependencies: |
| 84 | + |
| 85 | +```bash |
| 86 | +$ npm install |
| 87 | +``` |
| 88 | + |
| 89 | +## Making the test suite pass |
| 90 | + |
| 91 | +Execute the tests with: |
| 92 | + |
| 93 | +```bash |
| 94 | +$ npm test |
| 95 | +``` |
| 96 | + |
| 97 | +In the test suites all tests but the first have been skipped. |
| 98 | + |
| 99 | +Once you get a test passing, you can enable the next one by changing `xtest` to |
| 100 | +`test`. |
| 101 | + |
| 102 | + |
| 103 | +## Submitting Solutions |
| 104 | + |
| 105 | +Once you have a solution ready, you can submit it using: |
| 106 | + |
| 107 | +```bash |
| 108 | +exercism submit allergies.js |
| 109 | +``` |
| 110 | + |
| 111 | +## Submitting Incomplete Solutions |
| 112 | + |
| 113 | +It's possible to submit an incomplete solution so you can see how others have |
| 114 | +completed the exercise. |
| 115 | + |
| 116 | +## Exercise Source Credits |
| 117 | + |
| 118 | +Jumpstart Lab Warm-up [http://jumpstartlab.com](http://jumpstartlab.com) |
0 commit comments