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.
ResourceFunction["CaesarCipher"] lists over the first argument.
Letters retain their case (lowercase/uppercase).

Examples

Basic Examples (2) 

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 (3) 

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 (2) 

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 (2) 

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 (2) 

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 (1) 

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

In[16]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/7ef52102-a6c2-40d3-822e-f7207428830d"]
Out[17]=

Publisher

SHuisman

Version History

  • 2.0.0 – 26 June 2019
  • 1.0.0 – 12 April 2019

License Information