|
1 | 1 | # Instructions |
2 | 2 |
|
3 | | -Create an implementation of the affine cipher, |
4 | | -an ancient encryption system created in the Middle East. |
| 3 | +Create an implementation of the affine cipher, an ancient encryption system created in the Middle East. |
5 | 4 |
|
6 | 5 | 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. |
| 6 | +Each character is mapped to its numeric equivalent, encrypted with a mathematical function and then converted to the letter relating to its new numeric value. |
| 7 | +Although all monoalphabetic ciphers are weak, the affine cipher is much stronger than the atbash cipher, because it has many more keys. |
| 8 | + |
| 9 | +[//]: # ' monoalphabetic as spelled by Merriam-Webster, compare to polyalphabetic ' |
| 10 | + |
| 11 | +## Encryption |
12 | 12 |
|
13 | 13 | The encryption function is: |
14 | 14 |
|
15 | | -`E(x) = (ax + b) mod m` |
| 15 | +```text |
| 16 | +E(x) = (ai + b) mod m |
| 17 | +``` |
16 | 18 |
|
17 | | -- where `x` is the letter's index from 0 - length of alphabet - 1 |
18 | | -- `m` is the length of the alphabet. For the roman alphabet `m == 26`. |
19 | | -- and `a` and `b` make the key |
| 19 | +Where: |
20 | 20 |
|
21 | | -The decryption function is: |
| 21 | +- `i` is the letter's index from `0` to the length of the alphabet - 1 |
| 22 | +- `m` is the length of the alphabet. |
| 23 | + For the Roman alphabet `m` is `26`. |
| 24 | +- `a` and `b` are integers which make the encryption key |
22 | 25 |
|
23 | | -`D(y) = a^-1(y - b) mod m` |
| 26 | +Values `a` and `m` must be _coprime_ (or, _relatively prime_) for automatic decryption to succeed, i.e., they have number `1` as their only common factor (more information can be found in the [Wikipedia article about coprime integers][coprime-integers]). |
| 27 | +In case `a` is not coprime to `m`, your program should indicate that this is an error. |
| 28 | +Otherwise it should encrypt or decrypt with the provided key. |
24 | 29 |
|
25 | | -- where `y` is the numeric value of an encrypted letter, ie. `y = E(x)` |
26 | | -- it is important to note that `a^-1` is the modular multiplicative inverse |
27 | | - of `a mod m` |
28 | | -- the modular multiplicative inverse of `a` only exists if `a` and `m` are |
29 | | - coprime. |
| 30 | +For the purpose of this exercise, digits are valid input but they are not encrypted. |
| 31 | +Spaces and punctuation characters are excluded. |
| 32 | +Ciphertext is written out in groups of fixed length separated by space, the traditional group size being `5` letters. |
| 33 | +This is to make it harder to guess encrypted text based on word boundaries. |
30 | 34 |
|
31 | | -To find the MMI of `a`: |
| 35 | +## Decryption |
| 36 | + |
| 37 | +The decryption function is: |
32 | 38 |
|
33 | | -`an mod m = 1` |
| 39 | +```text |
| 40 | +D(y) = (a^-1)(y - b) mod m |
| 41 | +``` |
34 | 42 |
|
35 | | -- where `n` is the modular multiplicative inverse of `a mod m` |
| 43 | +Where: |
36 | 44 |
|
37 | | -More information regarding how to find a Modular Multiplicative Inverse |
38 | | -and what it means can be found [here.](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) |
| 45 | +- `y` is the numeric value of an encrypted letter, i.e., `y = E(x)` |
| 46 | +- it is important to note that `a^-1` is the modular multiplicative inverse (MMI) of `a mod m` |
| 47 | +- the modular multiplicative inverse only exists if `a` and `m` are coprime. |
39 | 48 |
|
40 | | -Because automatic decryption fails if `a` is not coprime to `m` your |
41 | | -program should return status 1 and `"Error: a and m must be coprime."` |
42 | | -if they are not. Otherwise it should encode or decode with the |
43 | | -provided key. |
| 49 | +The MMI of `a` is `x` such that the remainder after dividing `ax` by `m` is `1`: |
44 | 50 |
|
45 | | -The Caesar (shift) cipher is a simple affine cipher where `a` is 1 and |
46 | | -`b` as the magnitude results in a static displacement of the letters. |
47 | | -This is much less secure than a full implementation of the affine cipher. |
| 51 | +```text |
| 52 | +ax mod m = 1 |
| 53 | +``` |
48 | 54 |
|
49 | | -Ciphertext is written out in groups of fixed length, the traditional group |
50 | | -size being 5 letters, and punctuation is excluded. This is to make it |
51 | | -harder to guess things based on word boundaries. |
| 55 | +More information regarding how to find a Modular Multiplicative Inverse and what it means can be found in the [related Wikipedia article][mmi]. |
52 | 56 |
|
53 | 57 | ## General Examples |
54 | 58 |
|
55 | | -- Encoding `test` gives `ybty` with the key a=5 b=7 |
56 | | -- Decoding `ybty` gives `test` with the key a=5 b=7 |
57 | | -- Decoding `ybty` gives `lqul` with the wrong key a=11 b=7 |
58 | | -- Decoding `kqlfd jzvgy tpaet icdhm rtwly kqlon ubstx` |
59 | | - - gives `thequickbrownfoxjumpsoverthelazydog` with the key a=19 b=13 |
60 | | -- Encoding `test` with the key a=18 b=13 |
61 | | - - gives `Error: a and m must be coprime.` |
62 | | - - because a and m are not relatively prime |
63 | | - |
64 | | -## Examples of finding a Modular Multiplicative Inverse (MMI) |
65 | | - |
66 | | -- simple example: |
67 | | - - `9 mod 26 = 9` |
68 | | - - `9 * 3 mod 26 = 27 mod 26 = 1` |
69 | | - - `3` is the MMI of `9 mod 26` |
70 | | -- a more complicated example: |
71 | | - - `15 mod 26 = 15` |
72 | | - - `15 * 7 mod 26 = 105 mod 26 = 1` |
73 | | - - `7` is the MMI of `15 mod 26` |
| 59 | +- Encrypting `"test"` gives `"ybty"` with the key `a = 5`, `b = 7` |
| 60 | +- Decrypting `"ybty"` gives `"test"` with the key `a = 5`, `b = 7` |
| 61 | +- Decrypting `"ybty"` gives `"lqul"` with the wrong key `a = 11`, `b = 7` |
| 62 | +- Decrypting `"kqlfd jzvgy tpaet icdhm rtwly kqlon ubstx"` gives `"thequickbrownfoxjumpsoverthelazydog"` with the key `a = 19`, `b = 13` |
| 63 | +- Encrypting `"test"` with the key `a = 18`, `b = 13` is an error because `18` and `26` are not coprime |
| 64 | + |
| 65 | +## Example of finding a Modular Multiplicative Inverse (MMI) |
| 66 | + |
| 67 | +Finding MMI for `a = 15`: |
| 68 | + |
| 69 | +- `(15 * x) mod 26 = 1` |
| 70 | +- `(15 * 7) mod 26 = 1`, ie. `105 mod 26 = 1` |
| 71 | +- `7` is the MMI of `15 mod 26` |
| 72 | + |
| 73 | +[mmi]: https://en.wikipedia.org/wiki/Modular_multiplicative_inverse |
| 74 | +[coprime-integers]: https://en.wikipedia.org/wiki/Coprime_integers |
0 commit comments