# Can You Break Julius Caesar’s Cryptosystem?

Can You Break Julius Caesar’s Cryptosystem?

In this module, students will be introduced to encryption and decryption by exploring Julius Caesar’s method of shifting letters in the alphabet.

Appropriate for ages

11+.

Allow 55 minutes to complete the module.Important note:

This module should be led by an instructor with basic Wolfram Language knowledge. If you would like to learn the language, please try this. If you would like a Computational Thinking Initiative ambassador or volunteer to help you run an adventure, please .

Appropriate for ages

11+.

Allow 55 minutes to complete the module.Important note:

This module should be led by an instructor with basic Wolfram Language knowledge. If you would like to learn the language, please try this

Learning Objective

◼

Students will understand how the Caesar cryptosystem encrypts messages and, based on this understanding, devise a method for decryption

Computational Thinking Principles and Practices

◼

Exploring entire categories all at once (i.e. look at all flags, all of Shakespeare’s sonnets, etc.)

◼

Simulating things that are hard or impossible to do by performing real-world experiments

◼

Treating the computer’s “misunderstandings” as proxies for one’s own

Standards Alignment

◼

AP Computer Science Principles:

◼

LO 1.1.1: apply a creative development process when creating computational artifacts

◼

LO 1.2.5: analyze the correctness, usability, functionality and suitability of computational artifacts

◼

LO 3.1.1: find patterns and test hypotheses about digitally processed information to gain insight and knowledge

◼

LO 4.1.1: develop an algorithm for implementation in a program

◼

CSTA K–12 Computer Science Standards:

◼

Level 3A CPP 9: explain the principles of security by examining encryption, cryptography and authentication technique

Helpful Background

◼

Information on Caesar ciphers:

## STARTING POINT

STARTING POINT

“Today we’re going to take a look at Julius Caesar’s cryptosystem, sometimes called the Caesar cipher. Let’s start by looking at the alphabet. Simply type Alphabet[] and press +.”

In[1]:=

Alphabet[]

Out[1]=

{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}

“Every letter in the alphabet has a position.”

In[2]:=

LetterNumber[Alphabet[]]

Out[2]=

{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26}

In[3]:=

LetterNumber["d"]

Out[3]=

4

“In much the same way, we can go from numbers to letters.”

You may need to explain Range here.

In[4]:=

Range[26]

Out[4]=

{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26}

In[5]:=

FromLetterNumber[Range[26]]

Out[5]=

{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}

In[6]:=

FromLetterNumber[4]

Out[6]=

d

◼ Try a number larger than 26 and ask, “Why is there a missing symbol there?”

◼ Are there other numbers we can't use with FromLetterNumber?

◼ Other than letters, what might appear in a message you would want to encode?

◼ If students are curious about the syntax for lists, try showing some list functions like First or Part and explaining that using a standard syntax lets us find pieces of the list.

CHECKPOINT

Check to see if everyone understands that letters have a position.

“We can also apply LetterNumber to a word.”

In[7]:=

LetterNumber["cat"]

Out[7]=

{3,1,20}

In[8]:=

FromLetterNumber[{3,1,20}]

Out[8]=

{c,a,t}

“That doesn’t look like the word we started with. We ended up with a list of letters instead of a single word. Let’s use StringJoin to combine them.”

In[9]:=

StringJoin[FromLetterNumber[{3,1,20}]]

Out[9]=

cat

◼ Try some other words.

◼ What is your name in numbers?

◼ Change your word to numbers, share the numbers with your neighbor and have them change it back to a word. Was it the word you started with?

CHECKPOINT

Check to see if students are comfortable switching between words and numbers.

“Julius Caesar used letter positions to encrypt messages. Caesar’s cryptosystem works by shifting the position of each letter in the alphabet. One way to do this is to just add three, or some other number, to each position.”

In[10]:=

LetterNumber["cat"]

Out[10]=

{3,1,20}

In[11]:=

{3,1,20}+3

Out[11]=

{6,4,23}

In[12]:=

FromLetterNumber[{6,4,23}]

Out[12]=

{f,d,w}

In[13]:=

StringJoin[FromLetterNumber[{6,4,23}]]

Out[13]=

fdw

“In this case, we call ‘fdw’ the encrypted version of ‘cat.’”

◼ Try some other words.

◼ Change your word to numbers, shift over the letters by adding, share the numbers with your neighbor and have them change those numbers back to a word. Was it the word you started with?

◼ “You have just encrypted!”

◼ Some students may already notice the issue with shifting Z. A discussion should be encouraged.

CHECKPOINT

Check to see if students understand the concept of shifting the position of the letters in the alphabet.

“But what happens if we try to shift the position of Z, even just by one?”

In[14]:=

LetterNumber["z"]+1

Out[14]=

27

In[15]:=

FromLetterNumber[27]

Out[15]=

Missing[NotApplicable]

“Or what if we want to do a series of words? What will happen to the white spaces?”

In[16]:=

LetterNumber["keep calm and code on"]

Out[16]=

{11,5,5,16,Missing[NotApplicable],3,1,12,13,Missing[NotApplicable],1,14,4,Missing[NotApplicable],3,

15,4,5,Missing[NotApplicable],15,14}

“We have two problems here: we’re running out of alphabet, and we don’t have a way to deal with spaces. Let’s focus on the first problem: how can we get another letter by shifting Z?”

◼ If students suggest shifting downward instead of upward, try the code using subtraction instead of addition. Try a word that includes A to demonstrate the problem.

◼ If students suggest shifting only Z downward, ask, "Will this create any problems?" Demonstrate that with a shift by one, both X and Z would be encrypted as Y, so the person decrypting the message wouldn't be able to tell the difference between the two letters.

◼ If we shifted all of the letters up by one, would any of them become the letter A?

◼ What would happen if we wrote our letters as a circle instead of a line?

CHECKPOINT

Check that students understand the need to connect the end of the alphabet to the beginning.

“Let's take a look at the function RotateLeft.”

In[17]:=

RotateLeft[Alphabet[],3]

Out[17]=

{d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c}

“It’s helpful to see which letter went where, so to speak. We should make rules for what letter becomes what.”

In[18]:=

Thread[Alphabet[]RotateLeft[Alphabet[],3]]

Out[18]=

{ad,be,cf,dg,eh,fi,gj,hk,il,jm,kn,lo,mp,nq,or,ps,qt,ru,s

v,tw,ux,vy,wz,xa,yb,zc}

◼ Students who are more math inclined may want to try Mod.

◼ Try changing the value of 3 (3 was Julius Caesar’s personal favorite number).

◼ Try RotateRight instead.

◼ It may help to demonstrate that you could rewrite LetterNumber or FromLetterNumber as replacement rules.

CHECKPOINT

Check with students to see if they understand replacement rules.

“Now that we have replacement rules, we need a function that actually does replace the letters in a word.”

In[19]:=

StringReplace["cat",Thread[Alphabet[]RotateLeft[Alphabet[],3]]]

Out[19]=

fdw

“Look familiar? Let’s check and see if our new method works with white spaces.”

In[20]:=

StringReplace["keep calm and code on",Thread[Alphabet[]RotateLeft[Alphabet[],3]]]

Out[20]=

nhhs fdop dqg frgh rq

“It looks like we solved the white space problem too. We’re replacing only the letters that have replacement rules and leaving everything else the same.”

◼ Try a different phrase.

◼ Can your neighbor understand your phrase?

◼ Students may begin to try and decrypt their messages by using RotateRight or negative shifting values.

◼ As a challenge, ask students to encrypt white spaces.

CHECKPOINT

Check in with students to be sure they all have a method for encrypting phrases.

“So far we’ve been able to encrypt words and series of words. But what happens when a person receives the message?”

In[21]:=

StringReplace["nhhs fdop dqg frgh rq",Thread[Alphabet[]RotateLeft[Alphabet[],-3]]]

Out[21]=

keep calm and code on

“But what if we were trying to hack the message and didn’t know how the letters were shifted? We could work our way through all possible shifts and look at all the messages.”

In[22]:=

Table[StringReplace["nhhs fdop dqg frgh rq",Thread[Alphabet[]RotateLeft[Alphabet[],-n]]],{n,26}]

Out[22]=

{mggr ecno cpf eqfg qp,lffq dbmn boe dpef po,keep calm and code on,jddo bzkl zmc bncd nm,

iccn ayjk ylb ambc ml,hbbm zxij xka zlab lk,gaal ywhi wjz ykza kj,fzzk xvgh viy xjyz ji,

eyyj wufg uhx wixy ih,dxxi vtef tgw vhwx hg,cwwh usde sfv ugvw gf,bvvg trcd reu tfuv fe,

auuf sqbc qdt setu ed,ztte rpab pcs rdst dc,yssd qoza obr qcrs cb,xrrc pnyz naq pbqr ba,

wqqb omxy mzp oapq az,vppa nlwx lyo nzop zy,uooz mkvw kxn myno yx,tnny ljuv jwm lxmn xw,

smmx kitu ivl kwlm wv,rllw jhst huk jvkl vu,qkkv igrs gtj iujk ut,pjju hfqr fsi htij ts,

oiit gepq erh gshi sr,nhhs fdop dqg frgh rq}

“Do you see the decrypted message?”

◼ Students may decide to keep the n-value positive and use RotateRight instead.

◼ Why was doing this helpful at all? It just looks like a bunch of nonsense. Do we want to have to dig for the message each time?

◼ How long would it take you to try all of the options by hand?

◼ In this example, finding our phrase was really easy because it was third, but what if the message were buried deeper inside of the list?

CHECKPOINT

Check that the students agree that a single decrypted message would be more useful.

“Let’s try letting the computer pick out the decrypted message for us. The computer can check to see whether a group of letters is in the dictionary.”

In[23]:=

DictionaryWordQ["cat"]

Out[23]=

True

In[24]:=

DictionaryWordQ["fdw"]

Out[24]=

False

“We can use TextWords to check all of the groups of letters in a longer phrase to see which of them are words.”

In[25]:=

DictionaryWordQ/@TextWords["mggr ecno cpf eqfg qp"]

Out[25]=

{False,False,False,False,False}

In[26]:=

DictionaryWordQ/@TextWords["keep calm and code on"]

Out[26]=

{True,True,True,True,True}

“That works, but sometimes by chance one of the incorrectly decrypted messages will also contain a real word. The best decryption should be the one that gives all real words.”

In[27]:=

Count[DictionaryWordQ/@TextWords["keep calm and code on"],True]

Out[27]=

5

In[28]:=

Table[Count[DictionaryWordQ/@TextWords[StringReplace["nhhs fdop dqg frgh rq",Thread[Alphabet[]RotateRight[Alphabet[],n]]]],True],{n,26}]

Out[28]=

{0,0,5,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0}

“A shift of three gives us all real words—we’ve broken the cipher!”

◼ Have the students exchange messages of unknown shifts with their neighbors. Can they follow all of the steps to decipher them?

◼ Do you have other ideas for picking out the real words? One approach involves machine learning, where you can use a built-in language classifier to identify which items might be English words. Other approaches may include checking letter frequencies (e.g. the most common letter is E etc.).

◼ If you re-decipher one of the wrongly deciphered messages, will you be able to get the originally encrypted message back? Why is that?

◼ Is the Caesar cipher a very safe way to send a secret message?

## FINAL POINT

FINAL POINT

Ten minutes before the end of the module time.

### Summarize

Summarize

Summarize what was done in the module and talk about findings.

### Refer

Refer

Refer back to the learning objective and summarize how you have reached it.

### Extend

Extend

Extend the module to the future. For example, “If you have time at home, try increasing the rotation for each letter as you go through the alphabet.”

### Possible Additional Relevant Functions

Possible Additional Relevant Functions

LetterCounts • Position • Flatten • Classify • Mod

### Possible Pitfalls

Possible Pitfalls

◼

Students may need a review of Map (/@) and Table

◼

Students may need an explanation about listable functions versus functions that require Map