Interactivate

Cryptography and Ciphers

Mentor: Good. Did you know that this kind of mathematics is used in defending the United States and
other countries during wars and other times when there is information that has to be kept
confidential?

Student: How would someone use modular arithmetic during a war?

Mentor: It was used during the U.S. Civil War in the 1860's, and even thousands of years before that,
in Caesar's Roman Empire. People who wanted to communicate with allies but not their enemies
would send encrypted messages back and forth.

Student: What is an encrypted message?

Mentor: Encrypted messages take the letters and numbers of a message and transform them into a
different series of letters and numbers that do not make sense unless you know the code to
unscramble them.

Student: And if you do know the code?

Mentor: Then you get to read the secret message. Amazingly complex scrambling procedures can stump
highly trained people and even computers. Right now I would like to talk about some simple
ciphers, so you can practice the basic idea.

Student: What is a cipher?

Mentor: A cipher is the method by which you encrypt a message. The ones that I want to teach you have
to do with numerical operations: multiplication, division, addition and subtraction, so we
must first create a numerical alphabet by assigning numbers, beginning with zero, to each
letter of our English alphabet.

Student: So A would be 0, B would be 1 ..... like this:

A B C D ... Z

0 1 2 3 ... 25

Mentor: Good. Most ciphers use modular arithmetic during some steps of the encryption and decryption
process. We have used the numbers 0 through 25 to represent 26 English letters, so we will use
mod 26 in all of our cipher examples.

Let's begin with what is called a shift cipher. First we must translate our message into our
numerical alphabet. For example, the code name 'James' looks like this:

J A M E S

9 0 12 4 18

Now choose the number you want to shift by.

Student: Let's choose 7.

Mentor: We need to remember this number, which we will call the 'B-shift' value, to both cipher and
later *de*cipher the message. We now 'shift' our cipher by adding B to each of the numbers
from our code word like this:

Mentor: Now if you wanted to send the message 'James is a spy.' to someone, but didn't want everyone
(like James!) to be able to read the message, you could send the message 'Qhtlz pz h zwf.'. A
good cryptographer, however, might use the size of the words and your punctuation to help them
figure out your cipher and your secret message! For example, what English words have only one
letter?

Student: 'I', like 'I went to the store' and 'a' as in 'for a loaf of bread'. But 'I' would be
capitalized, so 'h' in our code would have to be an 'a'!

Mentor: Good! That's exactly how a cryptographer would do it as well. We don't want just anyone to
figure out our cipher, so we make it harder by removing punctuation and grouping the letters
in 'words' that are usually five letters long, so our message might look something like this:

QHTLZ PZHZW F

In order for someone to translate the message, they would have to use our cipher backwards.

Student: So first they would translate the letters into numbers again, so QHTLZ would again be

16 7 19 11 25

Mentor: But in order to solve the cipher they would also need to know that we used a B-shift of 7...

Student: ...which, working backwards we would need to subtract! So 16 - 7 = 9, which is 'J', 7 - 7 =
0, which is 'A', and so on... Neat!

Mentor: What about the last letter?

Student: F becomes 5, 5 minus 7 is negative 2 ! What happened?

Mentor: You forgot to reverse the 'mod 26' calculation. If you remember, the mod 26 operation made us
subtract a multiple of 26, in this case just 26*1 = 26. Try adding it back.

Mentor: Shift ciphers can also work in the opposite order, where you subtract the B-shift first when
you are encrypting and then add it back when you are decrypting.

Let's try another kind of cipher. It is called a multiplication cipher. It is similar to the
shift cipher, except that you multiply and divide instead of add and subtract. Choose an easy
example word for us to use.

Student: How about S I M P L E which in the numerical alphabet is

18 8 12 15 11 4

Mentor: Good. We'll use the number 7 again, but this time we will call it 'A' so as not confuse it
with the shift. Now multiply the numeric alphabet letters each by the 'A-multiplier':

That works, because 18 is our original letter 'S', but that's not really so simple anymore.

Mentor: There is another method. Try this: multiply each of your numerical alphabet code numbers by
fifteen and then mod 26.

Student:

W is 22, times 15 is 330, mod 26 is 18, which is 'S'
E is 4, times 15 is 60, mod 26 is 8, which is 'I'
G is 6, times 15 is 90, mod 26 is 12, which is 'M'
B is 1, times 15 is 15, mod 26 is 15, which is 'P'
Z is 25, times 15 is 375, mod 26 is 11, which is 'L'
C is 2, times 15 is 30, mod 26 is 4, which is 'E'.

That worked, but where did the 15 come from?

Mentor: Modular arithmetic is not quite the same as regular arithmetic. In regular arithmetic the
inverse of a multiplier A would be 1/A, for example 7 and the fraction 1/7th. In modular
arithmetic a multiplier will only have an inverse if the multiplier (for example 7), and the
modular base (in our case, 26) have no common factors greater than one! The modular inverse -
we will call it A' (A-prime) - will be a whole number that also has no common factors with the
modular base. 7 and 15 are modular inverses in mod 26.

Student: Is there a way to figure out what the modular inverse of a number is?

Mentor: Just as in real arithmetic, a number and it's inverse will multiply in modular arithmetic to
give you 1, so (A * A') mod 26 = 1. Start by making a list of multiples of 26, and then add
one to each of them.

Student: But A and N are coded the same, as are B and O, and C and P. Does this pattern continue?

Mentor: Yes. Because 26 and 6 share the factor of two, each number will represent two letters. We
cannot reverse the process because we would not know which of the two letters to select. This
happens when the number we try to use as a multiplier shares a factor with 26.

Student: For a common factor of 13 there are thirteen letter possibilities for each number, like you
said! So 13 doesn't have an inverse, but 5 does.

Mentor: Look back at your 'multiples of 26 plus one' list and see if you can find it.

Student: Okay... 27... 53... 79... 105! 105 divided by 5 is 21 so 5 and 21 are inverses in mod 26. So
for mod 26 we have 7 with 15, 5 with 21, and 3 with 9, are there any others?

Mentor: Yes. These pairs all work for mod 26, but remember they won't apply to another system with a
different number of letters!

1 is it's own inverse
3 and 9
5 and 21
7 and 15
11 and 19
17 and 23
25 is also it's own inverse

Student: How hard can ciphers get?

Mentor: There are ciphers that stump even the best programmers with the newest computers. It has to
be that way in order to be able to stay ahead of the technology of the opposition. I am sure
that as technology gets better and faster, ciphers do the same. The ciphers we have been
covering were some of the first, dating to way before we had computers. In fact, Caesar liked
to use the shift cipher with 3 as the shift.

Student: I bet the newer ones have a lot of really hard math in them.

Mentor: Yes they do. Let's move on to the last cipher we are going to talk about. It is called an
affine cipher. Affine means linear, so this cipher takes on the same form as a line:

(A*x + B) mod 26

With this, you must have both the A-multiplier and B-shift to decode the message. Note that
if A = 1 you have a normal shift cipher, and when B = 0 you have a multiplication cipher.
Again here the greatest common factor for A and 26 has to be 1, in order to have a way to
decode the message.

Student: Can we use 7 again?

Mentor: Okay, let's use that for our A and we can use 5 for our B.

Student: So if I let someone know my A and B and what type of cipher then they can decrypt the
message.