# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Attempt to crack a Caesar-enciphered message

Contributed by:
Sander Huisman

ResourceFunction["CrackCaesarCipher"][ attempts to decipher the Caesar-enciphered input | |

ResourceFunction["CrackCaesarCipher"][ attempts to decipher |

ResourceFunction["CrackCaesarCipher"] has the following options:

Method | Automatic | method used to decipher the message |

Language | "English" | language to use for dictionaries and letter frequencies |

"WordValueFunction" | StringLength | function for valuing a matched substring |

"DictionaryWords" | Automatic | list 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 |

Automatic | if 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.

Try to decipher a string:

In[1]:= |

Out[1]= |

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

In[2]:= |

Out[3]= |

Encode a message:

In[4]:= |

Out[4]= |

Try to decipher the message using a dictionary attack:

In[5]:= |

Out[6]= |

Try to decipher the message using a letter frequency attack:

In[7]:= |

Out[7]= |

Use another language:

In[8]:= |

Out[9]= |

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

In[10]:= |

Out[11]= |

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

In[12]:= |

Out[13]= |

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]:= |

Out[15]= |

Decipher a long text:

In[16]:= |

Out[17]= |

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

In[18]:= |

Out[19]= |

The setting for Language can change the outcome:

In[20]:= |

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]:= |

Out[23]= |

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

In[24]:= |

Out[25]= |

A frequency analysis is much faster:

In[26]:= |

Out[26]= |

- 1.0.0 – 28 June 2019

This work is licensed under a Creative Commons Attribution 4.0 International License