You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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`
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
15
+
```text
16
+
E(x) = (ai + b) mod m
17
+
```
19
18
20
-
The decryption function is:
19
+
Where:
20
+
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 up the encryption key.
25
+
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.
29
+
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.
21
34
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.
35
+
## Decryption
28
36
29
-
To find the MMI of `a`:
37
+
The decryption function is:
38
+
39
+
```text
40
+
D(y) = (a^-1)(y - b) mod m
41
+
```
30
42
31
-
`an mod m = 1`
32
-
- where `n` is the modular multiplicative inverse of `a mod m`
43
+
Where:
33
44
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)
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.
36
48
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.
49
+
The MMI of `a` is `x` such that the remainder after dividing `ax` by `m` is `1`:
41
50
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.
51
+
```text
52
+
ax mod m = 1
53
+
```
45
54
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.
55
+
More information regarding how to find a Modular Multiplicative Inverse and what it means can be found in the [related Wikipedia article][mmi].
49
56
50
57
## General Examples
51
58
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
0 commit comments