# 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 free online introduction. If you would like a Computational Thinking Initiative ambassador or volunteer to help you run an adventure, please contact us.

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 free online introduction. If you would like a Computational Thinking Initiative ambassador or volunteer to help you run an adventure, please contact us.

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: https://en.wikipedia.org/wiki/Caesar_cipher

◼

## 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 +.”

Alphabet[]

In[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}

Out[1]=

“Every letter in the alphabet has a position.”

LetterNumber[Alphabet[]]

In[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}

Out[2]=

LetterNumber["d"]

In[3]:=

4

Out[3]=

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

You may need to explain Range here.

Range[26]

In[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}

Out[4]=

FromLetterNumber[Range[26]]

In[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}

Out[5]=

FromLetterNumber[4]

In[6]:=

d

Out[6]=

◼ 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.”

LetterNumber["cat"]

In[7]:=

{3,1,20}

Out[7]=

FromLetterNumber[{3,1,20}]

In[8]:=

{c,a,t}

Out[8]=

“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.”

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

In[9]:=

cat

Out[9]=

◼ 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.”

LetterNumber["cat"]

In[10]:=

{3,1,20}

Out[10]=

{3,1,20}+3

In[11]:=

{6,4,23}

Out[11]=

FromLetterNumber[{6,4,23}]

In[12]:=

{f,d,w}

Out[12]=

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

In[13]:=

fdw

Out[13]=

“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?”

LetterNumber["z"]+1

In[14]:=

27

Out[14]=

FromLetterNumber[27]

In[15]:=

Missing[NotApplicable]

Out[15]=

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

LetterNumber["keep calm and code on"]

In[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}

Out[16]=

“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.”

RotateLeft[Alphabet[],3]

In[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}

Out[17]=

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

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

In[18]:=

{ad,be,cf,dg,eh,fi,gj,hk,il,jm,kn,lo,mp,nq,or,ps,qt,ru,sv,tw,ux,vy,wz,xa,yb,zc}

Out[18]=

◼ 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.”

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

In[19]:=

fdw

Out[19]=

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

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

In[20]:=

nhhs fdop dqg frgh rq

Out[20]=

“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?”

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

In[21]:=

keep calm and code on

Out[21]=

“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.”

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

In[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}

Out[22]=

“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.”

DictionaryWordQ["cat"]

In[23]:=

True

Out[23]=

DictionaryWordQ["fdw"]

In[24]:=

False

Out[24]=

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

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

In[25]:=

{False,False,False,False,False}

Out[25]=

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

In[26]:=

{True,True,True,True,True}

Out[26]=

“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.”

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

In[27]:=

5

Out[27]=

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

In[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}

Out[28]=

“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

◼