Introduction to Mathematica
Math 202 Cryptography
August 31, 2020

T. Ratliff, Wheaton College
ratliff_thomas@wheatoncollege.edu

A few basics

Mathematica notebooks are composed of different types of cells. This is a text cell where you can enter comments and descriptions. The text cells in this notebook have a dark red font.  
Commands are contained in input cells, like the one below, where the font is black. 

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 π, move anywhere in the following input cell and press SHIFT-ENTER

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]

This means  the number has 4 prime factors and that 13432423432414332 = 2^2 * 3 * 10429 * 107332306009

A Caesar cipher with shift k=7

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

LetterNumber["D"]

Oops. Since we’re in Z_26, we want to start with “A” -> 0, so we should have “E” -> 4
LetterNumber[ ] returns values in {1, 2, . . ., 26}. 
We need to subtract one from the result of LetterNumber[ ] to shift the value to {0, 1, . . ., 25}

x=LetterNumber["D"] - 1

We’ve now encoded the message “E” to the plaintext x=4.
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["D"]-1
y=Mod[x+7,26]

We could then convert the ciphertext y=11 to a letter by

FromLetterNumber[y+1]

Notice that we need the +1 since y ∈ (Subscript[ℤ, 26]) but FromLetterNumber[ ] expects values in {1, 2, . . ., 25}

Suppose we receive a message “B”. How do we decrypt to find the plaintext?
First, we find encode it as a numeric, then apply the decryption formula.

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

Thus, we were sent “U”. 

So, in summary
	To encrypt the letter “D”

x=LetterNumber["D"]-1;  (* This is how to put comments in Mathematica input cells *)
y=Mod[x+7,26]; (* The ; prevents Mathematica from printing the output from the command. *)
FromLetterNumber[y+1]

To decrypt a message “B”

y=LetterNumber["B"]-1;
x=Mod[y-7,26];
FromLetterNumber[x+1]

Using lists

This seems like a whole lot of work to automate encrypting and decrypting a single letter. 

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[{"I","F","E","V","E","R","Y"}]-1
y=Mod[x+7,26]
FromLetterNumber[y+1]

If you were to dig around the Help - Wolfram Documentation menu (which you certainly should!) you’d find the extremely handy Characters[ ] command:

Characters["IFEVERYPORKCHOPWEREPERFECT"]

This is where the real payoff comes in! 
Let’s encrypt “IFEVERYPORKCHOPWEREPERFECT” using a shift of 11

message = "IFEVERYPORKCHOPWEREPERFECT";
x=LetterNumber[message]-1; 
y=Mod[x+7,26]; 
FromLetterNumber[y+1]

To combine the list of characters into a single string, the StringJoin[ ] command is handy:

StringJoin[{"p","m","l","c","l","y","f","w","v","y","r","j","o","v","w","d","l","y","l","w","l","y","m","l","j","a"}]

"pmlclyfwvyrjovwdlylwlymlja"

And let’s decrypt “HPHZFWOYESLGPSZEOZRD” using a shift of 11

messageReceived = "HPHZFWOYESLGPSZEOZRD";
y=LetterNumber[messageReceived]-1;
x=Mod[y-11,26];
StringJoin[FromLetterNumber[x+1]]

Implementing an affine cipher with k = (9, 11)

Take a look at this code and you should be able to follow the syntax

message="MYCABBAGES";
x=LetterNumber[message]-1; 
y=Mod[9*x+11,26]; 
StringJoin[FromLetterNumber[y+1]]

messageReceived = "FPHYAVLPCJALIL";
y=LetterNumber[messageReceived]-1;
x=Mod[3*(y-11),26];
StringJoin[FromLetterNumber[x+1]]