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
Refer to the exercism help page for Rust installation and learning resources.
Execute the tests with:
$ cargo testAll but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the tests directory
and remove the #[ignore] flag from the next test and get the tests to pass
again. Each separate test is a function with #[test] flag above it.
Continue, until you pass every test.
If you wish to run all ignored tests without editing the tests source file, use:
$ cargo test -- --ignoredTo run a specific test, for example some_test, you can use:
$ cargo test some_testIf the specific test is ignored use:
$ cargo test some_test -- --ignoredTo learn more about Rust tests refer to the online test documentation
Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.
After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.
To format your solution, inside the solution directory use
cargo fmtTo see, if your solution contains some common ineffective use cases, inside the solution directory use
cargo clippy --all-targetsGenerally you should submit all files in which you implemented your solution (src/lib.rs in most cases). If you are using any external crates, please consider submitting the Cargo.toml file. This will make the review process faster and clearer.
The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
If you want to know more about Exercism, take a look at the contribution guide.
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.