Function Repository Resource:

CrackCaesarCipher

Source Notebook

Attempt to crack a Caesar-enciphered message

Contributed by: Sander Huisman

ResourceFunction["CrackCaesarCipher"][string]

attempts to decipher the Caesar-enciphered input string.

ResourceFunction["CrackCaesarCipher"][string,n]

attempts to decipher string and gives the first n results.

Details and Options

ResourceFunction["CrackCaesarCipher"] has the following options:
MethodAutomaticmethod used to decipher the message
Language"English"language to use for dictionaries and letter frequencies
"WordValueFunction"StringLengthfunction for valuing a matched substring
"DictionaryWords"Automaticlist of words to use for a dictionary attack
Possible forms for the option Method are:
"Dictionary"sort possible deciphered messages based on the presence of words from a dictionary
"LetterFrequency"sort possible deciphered messages based on the letter frequencies and compare those with known letter frequencies based on Language
Automaticif the string has a length smaller or equal to 100 characters, "Dictionary" is used; for texts above 100 characters, "LetterFrequencies" is used
Dictionaries are known for the languages given by DictionaryLookup[All] with a Latin alphabet.
For the method "Dictionary", each word in the dictionary is tested for its presence. Each matched string gets a value according to the option "WordValueFunction". The highest-value-candidate deciphered message will be returned.
For the method "Dictionary", a custom list of words can also be given using the option "DictionaryWords".
Letter frequencies are known for the following languages: Czech, Danish, Dutch, English, Esperanto, Finnish, French, German, Icelandic, Italian, Polish, Portuguese, Spanish, Swedish and Turkish. Data is based on Wikipedia's article on letter frequency.
For the method "LetterFrequency", the frequency of the letters is compared to the known letter frequencies for various languages by calculating Pearson’s correlation coefficient between the letter frequency of the message for various shifts and the known letter frequencies for the language given by the option Language.
When n is given, a list of length n is returned. Each element is an Association with three keys: "DecipheredString" contains the deciphered string, "Score" gives the score of the decipher and "Shift" gives the Caesar shift. The methods "Dictionary" and "LetterFrequency" give different scores and cannot be directly compared.

Examples

Basic Examples (1) 

Try to decipher a string:

In[1]:=
ResourceFunction[
 "CrackCaesarCipher"]["Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald"]
Out[1]=

Scope (1) 

Give the top five deciphers of a short Caesar-enciphered message:

In[2]:=
message = ResourceFunction["CaesarCipher"]["go on", 11];
ResourceFunction["CrackCaesarCipher"][message, 5]
Out[3]=

Options (7) 

Method (3) 

Encode a message:

In[4]:=
s = ResourceFunction["CaesarCipher"]["the apple", 11]
Out[4]=

Try to decipher the message using a dictionary attack:

In[5]:=
ResourceFunction["CrackCaesarCipher"][s, Method -> "Dictionary"]
Out[6]=

Try to decipher the message using a letter frequency attack:

In[7]:=
ResourceFunction["CrackCaesarCipher"][s, Method -> "LetterFrequency"]
Out[7]=

Language (1) 

Use another language:

In[8]:=
ResourceFunction["CaesarCipher"]["Mijn naam is Sander!", 11];
ResourceFunction["CrackCaesarCipher"][%, Language -> "Dutch"]
Out[9]=

WordValueFunction (2) 

The default "WordValueFunction" is the length of the string:

In[10]:=
s = ResourceFunction["CaesarCipher"]["The red car!", 11];
ResourceFunction["CrackCaesarCipher"][s, 4, Method -> "Dictionary", "WordValueFunction" -> StringLength]
Out[11]=

Value each matched string with a different metric. In this case just counting them:

In[12]:=
s = ResourceFunction["CaesarCipher"]["The red car!", 11];
ResourceFunction["CrackCaesarCipher"][s, 4, Method -> "Dictionary", "WordValueFunction" -> (1 &)]
Out[13]=

DictionaryWords (1) 

Give a custom dictionary in form of a list of strings. If certain words are expected in a piece of text, this can speed up the cracking significantly:

In[14]:=
s = ResourceFunction["CaesarCipher"]["my hovercraft is full of eels!",
    11];
ResourceFunction["CrackCaesarCipher"][s, Method -> "Dictionary", "DictionaryWords" -> {"airplane", "truck", "hovercraft", "car", "boat", "plane", "bike", "bicyle", "moped"}]
Out[15]=

Applications (1) 

Decipher a long text:

In[16]:=
hamlet = ExampleData[{"Text", "Hamlet"}];
hamlet = ResourceFunction["CaesarCipher"][hamlet, 6];
ResourceFunction["CrackCaesarCipher"][hamlet] // Short
Out[17]=

Properties and Relations (1) 

The function CrackCaesarCipher can, like the resource function CaesarDecipher, undo a encipherment done by the resource function CaesarCipher:

In[18]:=
message = ResourceFunction["CaesarCipher"]["This is a test", 6];
{ResourceFunction["CaesarDecipher"][message, 6], ResourceFunction["CrackCaesarCipher"][message]}
Out[19]=

Possible Issues (3) 

The setting for Language can change the outcome:

In[20]:=
message = ResourceFunction["CaesarCipher"]["De rode auto!", 11];
ResourceFunction["CrackCaesarCipher"][message, Method -> "Dictionary",
    Language -> #] & /@ {"Finnish", "Dutch"}
Out[21]=

The deciphered message with the highest score might not be the right one. In this case, the second highest is the right deciphered message:

In[22]:=
message = ResourceFunction["CaesarCipher"]["The voodoo xeroxed a puppy", 11];
ResourceFunction["CrackCaesarCipher"][message, 10, Method -> "LetterFrequency"] // Dataset
Out[23]=

Long strings take a long time to decipher using the Method "Dictionary”:

In[24]:=
shorthamlet = StringTake[ExampleData[{"Text", "Hamlet"}], 1000];
shorthamlet = ResourceFunction["CaesarCipher"][shorthamlet, 6];
AbsoluteTiming[
 ResourceFunction["CrackCaesarCipher"][shorthamlet, Method -> "Dictionary"];]
Out[25]=

A frequency analysis is much faster:

In[26]:=
AbsoluteTiming[
 ResourceFunction["CrackCaesarCipher"][shorthamlet, Method -> "LetterFrequency"];]
Out[26]=

Publisher

SHuisman

Version History

  • 1.0.0 – 28 June 2019

Related Resources

License Information