Community solutions for Affine Cipher in Ruby on Exercism
It says…
E(x) = (ai + b) mod m
Where:

i
is the letter’s index from 0
to the length of the alphabet  1

m
is the length of the alphabet. For the Roman alphabet m
is 26
.

a
and b
are integers which make the encryption key
i and x are being used interchangeably, but I think they are the same thing.
x
isn’t explicitly defined, but it should be something like

x
is the letter being encoded.

i
is the x
’s index from 0 to (the length of the alphabet  1
i
is a function of x
. They are not the same. x
is a letter. i
is a number from 0 through 25.
I think there’s still an inconsistency here because the decryption doesn’t distinguish between the letter y and the number in y. I think it should have been D(y) = (a^1)(j  b) to talk about it that way, but it seems easier to just use x,y instead.
D(y) = (a^1)(y  b) mod m
Where:
D(y) is the numeric value of an encrypted letter, i.e., y = E(x)
Based on the description, the encryption function E(x)
turns the letter x
into an integer value, y
. The encryption makes use of the integer i
which is the index of x
in the alphabet. The decryption function D(y)
turns the integer y
back into a letter. The decryption description is pretty explicit about that:
y
is the numeric value of an encrypted letter, i.e., y = E(x)
The reason there is an inconsistency is because the description describes how a letter is encrypted as a number then decrypted from a number back to a letter.
What is glossed over in the description is that the solution you’re expected to write is supposed to use E(x)
to compute an integer which is used to create a string. And, conversely, for the decryption, you need to convert a string to a series of integers which needs to be decrypted. The functions describe one portion of the exercise.
encrypt(intput) ==> apply E(x) for x in input ==> convert int to letter => combine to form an output
decrypt(intput) ==> convert letters to ints ==> apply D(y) for y in ints==> combine to form an output
The examples should make it clear that the encrypt and decrypt functions are not the full algorithm needed to solve this exercise.
1 Like
Thank you for the kind explanation.
The examples should make it clear that the encrypt and decrypt functions are not the full algorithm needed to solve this exercise.
This seems to be the most important part.
With many of the exercises, the description doesn’t even attempt to provide the comprehensive requirements and details. It aims to give some context and the required background to understand what’s going on. The tests implement the requirements.