Create an implementation of the affine cipher, an ancient encryption system created in the Middle East.
The affine cipher is a type of monoalphabetic substitution cipher. 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. Although all monoalphabetic ciphers are weak, the affine cypher is much stronger than the atbash cipher, because it has many more keys.
the encryption function is:
E(x) = (ax + b) mod m
- where
xis the letter's index from 0 - length of alphabet - 1 mis the length of the alphabet. For the roman alphabetm == 26.- and
aandbmake the key
the decryption function is:
D(y) = a^-1(y - b) mod m
- where
yis the numeric value of an encrypted letter, ie.y = E(x) - it is important to note that
a^-1is the modular multiplicative inverse ofa mod m - the modular multiplicative inverse of
aonly exists ifaandmare coprime.
To find the MMI of a:
an mod m = 1
- where
nis the modular multiplicative inverse ofa mod m
More information regarding how to find a Modular Multiplicative Inverse and what it means can be found here.
Because automatic decryption fails if a is not coprime to m your
program should return status 1 and "Error: a and m must be coprime."
if they are not. Otherwise it should encode or decode with the
provided key.
The Caesar (shift) cipher is a simple affine cipher where a is 1 and
b as the magnitude results in a static displacement of the letters.
This is much less secure than a full implementation of the affine cipher.
Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.
- Encoding
testgivesybtywith the key a=5 b=7 - Decoding
ybtygivestestwith the key a=5 b=7 - Decoding
ybtygiveslqulwith the wrong key a=11 b=7 - Decoding
kqlfd jzvgy tpaet icdhm rtwly kqlon ubstx- gives
thequickbrownfoxjumpsoverthelazydogwith the key a=19 b=13
- gives
- Encoding
testwith the key a=18 b=13- gives
Error: a and m must be coprime. - because a and m are not relatively prime
- gives
- simple example:
9 mod 26 = 99 * 3 mod 26 = 27 mod 26 = 13is the MMI of9 mod 26
- a more complicated example:
15 mod 26 = 1515 * 7 mod 26 = 105 mod 26 = 17is the MMI of15 mod 26
Please notice that the % operator is not equivalent to the one described in the problem description
(see Wikipedia entry for Modulo operation).
Go through the setup instructions for Java to install the necessary dependencies:
https://exercism.io/tracks/java/installation
You can run all the tests for an exercise by entering the following in your terminal:
$ gradle testIn the test suites all tests but the first have been skipped.
Once you get a test passing, you can enable the next one by removing the
@Ignore("Remove to run test") annotation.
Wikipedia http://en.wikipedia.org/wiki/Affine_cipher
It's possible to submit an incomplete solution so you can see how others have completed the exercise.