Wolfram Research

Function Repository Resource:

CaesarCipher

Source Notebook

Performs Caesar’s substitution cipher on a string

Contributed by: Sander Huisman

ResourceFunction["CaesarCipher"][string,n]

shifts every letter in string by n places further in the Latin alphabet.

ResourceFunction["CaesarCipher"][{s1,s2,},n]

shifts every letter of each of the strings s1,s2, … by n places in the Latin alphabet.

Details and Options

The Caesar cipher replaces each plain text character with a different one a fixed number of places n down the alphabet. As an example here we show it for n=3:

The letter c is converted to the letter f.
ResourceFunction["CaesarCipher"][string] performs the ROT13 substitution cipher.
n can be negative, shifting the letters to the left.
CaesarCipher lists over the first argument.
Letters retain their case (lowercase/uppercase).

Examples

Basic Examples

Perform a Caesar cipher with a shift of 2:

In[1]:=
ResourceFunction["CaesarCipher"]["abcde", 2]
Out[1]=

Decipher a text by shifting the letters 9 positions back:

In[2]:=
ResourceFunction["CaesarCipher"]["Bjwmna", -9]
Out[2]=

Scope

Cipher/decipher using a shift of n=13, also known at the ROT13 cipher:

In[3]:=
ResourceFunction["CaesarCipher"]["Cheers!"]
Out[3]=
In[4]:=
ResourceFunction["CaesarCipher"]["Purref!"]
Out[4]=

Cipher multiple strings:

In[5]:=
ResourceFunction["CaesarCipher"][{"Awesome", "devil", "epic"}, 5]
Out[5]=

If the shift is an integer multiple of 26 the Caesar cipher does not affect the input string:

In[6]:=
Table[ResourceFunction["CaesarCipher"]["This is a test.", i 26], {i, -5, 5}]
Out[6]=

Applications

Cipher a message:

In[7]:=
ResourceFunction[
 "CaesarCipher"]["This is an ciphered message for you!", 21]
Out[7]=

Decipher a message:

In[8]:=
ResourceFunction[
 "CaesarCipher"]["Iushuj cuiiqwu: Jxu Mebvhqc bqdwkqwu yi vkd!", -16]
Out[8]=

Properties and Relations

Deciphering can be done using the same function but with a negative shift:

In[9]:=
ResourceFunction["CaesarCipher"][
 ResourceFunction["CaesarCipher"]["This is a test.", 10], -10]
Out[9]=

Manually do a cipher:

In[10]:=
cr = CharacterRange["a", "z"];
rules = Thread[cr -> RotateLeft[cr, 5]]
Out[11]=

Replace the characters of a string:

In[12]:=
StringReplace["physicist", rules]
Out[12]=

CaesarCipher gives the same output:

In[13]:=
ResourceFunction["CaesarCipher"]["physicist", 5]
Out[13]=

Possible Issues

Characters with diacritics are ignored:

In[14]:=
ResourceFunction["CaesarCipher"]["jalapeño", 10]
Out[14]=

Use RemoveDiacritics to convert these letters to unaccented letters:

In[15]:=
ResourceFunction["CaesarCipher"][RemoveDiacritics["jalapeño"], 10]
Out[15]=

Neat Examples

Figure out what long English words are still English words after being Caesar-ciphered:

In[16]:=
allwords = DeleteDuplicates[
   RemoveDiacritics@*ToLowerCase /@ Select[DictionaryLookup[], GreaterEqualThan[6]@*StringLength]];
wordgroups = GatherBy[allwords, StringLength];

ClearAll[CheckWordGroup]
CheckWordGroup[wgroup_List, n_Integer] := Module[{subg, poss},
  subg = GatherBy[wgroup, StringTake[#, 1] &];
  subg = Map[
    With[{start = ResourceFunction["CaesarCipher"][StringTake[First[#], 1], n]},
      {#, SelectFirst[subg, (StringTake[First[#], 1] == start) &, Missing[]]}
      ] &
    , subg];
  subg = DeleteMissing[subg, 1, \[Infinity]];
  subg[[All, 1]] = ResourceFunction["CaesarCipher"][subg[[All, 1]], n];
  poss = Flatten[Intersection @@@ subg];
  Transpose[{ResourceFunction["CaesarCipher"][poss, -n], ConstantArray[n, Length[poss]], poss}]
  ]

Column[
 Row[{#1, Underscript["\[Rule]", #2], #3}, Spacer[1]] & @@@
  Catenate[
   Table[
    Catenate[CheckWordGroup[#, i] & /@ wordgroups]
    ,
    {i, 25}
    ]
   ]
 , Left, 0.8
 ]
Out[17]=

Resource History

License Information