DESCRIPTION
Before looking at this cipher, we need to look at numerical values for
letters. We start at A equal to
0. See the image below. The Affine cipher is a substitution cipher where letters are mapped to
new letters using the function y = Ax + B mod 26 where
This may seem complicated (it really isn’t) so let’s look at an example.
MATH
THEORY PART 1 – ONE-TO-ONE The transformation caused by the function y = Ax + B mod 26 requires one
important attribute. It needs to be a
one-to-one function. A one-to-one function is when
every value of x leads a to unique value of y. There cannot be two values of x that lead
to the same value of y. An example of a function that is not one-to-one is y=x2. It is not one-to-one because x=2 and x=-2
both give the value y=4. Why is one-to-one important?
Well, when we encrypt, we need to be able to decrypt that value. If two letters lead the same new letter,
there would be no way to figure out which of those two letters was the
original letter in the plaintext.
While we won’t go into the math detail here, a mathematician discovered
a long time ago that for y=Ax + B mod 26 to be one-to-one, there was one
requirement: The size of the alphabet (in our case, 26) and the value of
A must be coprime. That simply means that 26 and A cannot have any common factors. Since 26 only has the factors 2 and 13, we can chose any A that does not
have a factor of 2 (so not even numbers) or of 13. So A can be 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Note that A can continue on to bigger
numbers greater than the alphabet size but it would give the same result as
another lower value of A. MATH
THEORY 3 – INVERSE FUNCTIONS So, we know that y = Ax + B mod 26 will be one-to-one (as long as 26 and
A are coprimes). The function y = Ax + B mod 26 can therefore be undone or reversed. A function that undoes another function is
called an inverse function. Note that only functions that are
one-to-one can have inverse functions. For example, let’s consider the function y = 2x. Applying this function to a set of numbers
will essentially double all numbers.
The inverse function, which will half all numbers is y=x/2. DECRYPTING
FUNCTION The decrypting function (aka the inverse function) for y = Ax + B mod 26
is as follows: x = A1(y-b)
mod 26 where A1 is
such that A times A1 mod 26
= 1.
FULL
ENCRYPTING AND DECRYPTING EXAMPLE
WORK QUESTION 1
The line print(inverse_of(9)) will output: 3 The line print(inverse_of(11)) will output: 19
Write the enc and dec functions for Affine ciphers. They only need to work with lowercase
letters. enc (word, A, B) dec (word, A, B) Note that
the dec function will have to call the inverse_of function to get A1 before doing its
work. Test your
code. QUESTION 3 Decrypt the
following sentences that have been encrypted using the Affine cipher xjgostyhxxjujijovhkxflltxxkfiivqdwjbdojqbdqdxotghjckjt icjxdyyghejzdotivosgttoshfxdqcxtwtqsfqcgtcdqcontqovohhqt b)
with A=23 and B=1 opqpcwpuurpwgplssz |