Function Repository Resource:

CaesarDecipher

Source Notebook

Decipher a Caesar-enciphered string

Contributed by: Sander Huisman

ResourceFunction["CaesarDecipher"][string,n]

deciphers a Caesar-enciphered string that was enciphered by shifting letters in string by n places further in the Latin alphabet.

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

deciphers each string si.

ResourceFunction["CaesarDecipher"][n]

represents an operator form of ResourceFunction["CaesarDecipher"] that can be applied to an expression.

Details

ResourceFunction["CaesarDecipher"] replaces each plain text character with a different one a fixed number of places n up the alphabet. Here is an example for n=3, where the letter f is converted to the letter c:
ResourceFunction["CaesarDecipher"][string] performs the ROT13 substitution.
n can be negative, shifting the letters to the right.
ResourceFunction["CaesarDecipher"] threads over the first argument.
Letters retain their case (lowercase/uppercase).

Examples

Basic Examples (2) 

Perform a Caesar decipher with a shift of 2:

In[1]:=
ResourceFunction["CaesarDecipher"]["cdefg", 2]
Out[1]=

Decipher a text by shifting the letters 9 positions back:

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

Scope (3) 

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

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

Decipher multiple strings:

In[5]:=
ResourceFunction["CaesarDecipher"][{"Fbjxtrj", "ijanq", "junh"}, 5]
Out[5]=

Create an operator form of the function that deciphers with a shift of 5:

In[6]:=
op = ResourceFunction["CaesarDecipher"][5]
Out[6]=

Use the operator:

In[7]:=
op["junh"]
Out[7]=

Applications (2) 

Decipher a message:

In[8]:=
ResourceFunction[
 "CaesarDecipher"]["Ocdn dn v yzxdkczmzy hznnvbz ajm tjp!", 21]
Out[8]=

Decipher a message:

In[9]:=
ResourceFunction[
 "CaesarDecipher"]["Iushuj cuiiqwu: Jxu Mebvhqc bqdwkqwu yi vkd!", 16]
Out[9]=

Properties and Relations (2) 

Enciphering can be done using the deciphering function but with a negative shift:

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

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

In[11]:=
Table[ResourceFunction["CaesarDecipher"]["Input equals output.", i 26], {i, -3, 3}]
Out[11]=

Neat Examples (1) 

Try out all 25 options to decipher a message and find the original message:

In[12]:=
Grid[Table[{i, ResourceFunction["CaesarDecipher"][
    "Hsle td esp dpncpe xpddlrp esle hp hlye?", i]}, {i, 25}]]
Out[12]=

Publisher

SHuisman

Version History

  • 1.0.1 – 31 August 2021
  • 1.0.0 – 14 June 2019

Related Resources

License Information