Introduction to Mathematica - TR solutions
Math 202 Cryptography
September 5, 2018

T. Ratliff, Wheaton College
ratliff_thomas@wheatoncollege.edu

A few basics

Mathematica commands begin with capital letters
Arguments are contained in square brackets [ ]
To execute a Mathematica command, press SHIFT-ENTER
For example, to graph y=cos(x) from x=0 to 2 π

Plot[ Cos[x], {x,0,2Pi}]

Or to find 15*11 mod 26

Mod[ 15*11, 26]

Or to find the factors of a really big integer like 13432423432414332

FactorInteger[13432423432414332]

A shift cipher with k=7

Let’s look at encrypting “E” using a shift cipher with k=7
LetterNumber[ ] returns the position of a letter in the alphabet

LetterNumber["E"]

Oops. Since we’re in Z_26, we want to start with “A” -> 0, so we should have “E” -> 4
We need to subtract one from the result of LetterNumber[ ]

x=LetterNumber["E"] - 1

To encrypt, y = x+k mod 26

y=Mod[x+7, 26]

We can put this all together in one group by entering just RETURN, not SHIFT-RETURN between the lines. You’ll need to use SHIFT-RETURN to execute.

x=LetterNumber["E"]-1
y=Mod[x+7,26]

If we receive y=3, we can decrypt by subtracting 7 and converting to a character

y=3
x=Mod[y-7,26]
FromLetterNumber[x+1]

Thus, we were sent “W”

Using lists

A really powerful feature of Mathematica is that it will also work with lists rather than just single values.
For example, we can encrypt multiple letters at the same time.

x=LetterNumber[{"M","E","E","T"}]-1
y=Mod[x+7,26]

Use the built-in documentation!

Help is available through the Help - Wolfram Documentation menu

I’d suggest looking up the command Characters[ ] to help with today’s inclass work

Here’s a small sample of what it can do for you

Characters["FOURSCOREANDSEVEN"]

My solutions to #1, k=11

Encrypt #1a

x1a=LetterNumber[Characters["IFEVERYPORKCHOPWEREPERFECT"]]-1;
y1a=Mod[x1a+11,26]

Create the ciphertext for #1b

x1b=LetterNumber[Characters["WEWOULDNTHAVEHOTDOGS"]]-1;
y1b=Mod[x1+11,26]

Verify the ciphertext for #1b

y1b={7,15,7,25,5,22,14,24,4,18,11,6,15,18,25,4,14,25,17,3}
FromLetterNumber[Mod[y1b-11,26]+1]

My solutions to #2, k=(9,11)

Encrypt #2a

x2a=LetterNumber[Characters["IFEVERYPORKCHOPWEREPERFECT"]]-1;
y2a=Mod[9*x2a+11,26]

Check the ciphertext by decrypting

FromLetterNumber[Mod[ 3*({5,4,21,18,21,8,19,16,7,8,23,3,22,7,16,1,21,8,21,16,21,8,4,21,3,0}-11),26]+1]

Create the ciphertext for #2b

x2b=LetterNumber[Characters["WEWOULDNTHAVEHOTDOGS"]]-1;
y2b= Mod[9*x2b+11,26]

Verify the ciphertext for #2b

y2b = {1,21,1,7,9,6,12,24,0,22,11,18,21,22,7,0,12,7,13,17};
FromLetterNumber[Mod[ 3*(y2b-11),26]+1]