Wolfram Research

Function Repository Resource:

AtbashCipher

Source Notebook

Encipher a string using the Atbash cipher

Contributed by: Sander Huisman

ResourceFunction["AtbashCipher"][string]

enciphers or deciphers string using the Atbash cipher.

ResourceFunction["AtbashCipher"][string,{c1,c2,}]

enciphers or deciphers string using the alphabet ci.

ResourceFunction["AtbashCipher"][string,{{c1,1,c1,2,},{c2,1,c2,2,},}]

enciphers or deciphers string using the alphabets ci.

Details and Options

ResourceFunction["AtbashCipher"] converts the letters a—z to the letters z—a.
ResourceFunction["AtbashCipher"] also works on the modern Hebrew alphabet, converting the letters א—ת to ת—א; as described in [1].
The case of the letters are retained.

Examples

Basic Examples

Encipher a string:

In[1]:=
ResourceFunction["AtbashCipher"]["apple"]
Out[1]=

Encipher a sentence:

In[2]:=
ResourceFunction["AtbashCipher"]["Wolfram language is fun!"]
Out[2]=

Encipher the Hebrew word for sun:

In[3]:=
ResourceFunction["AtbashCipher"]["שמש"]
Out[3]=

Scope

AtbashCipher automatically threads over a list of strings:

In[4]:=
ResourceFunction["AtbashCipher"][{"apple", "banana", "peach"}]
Out[4]=

Use a custom collation (ordering) of an alphabet:

In[5]:=
alphabet = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m"};
ResourceFunction["AtbashCipher"]["sander", alphabet]
Out[6]=

The function is still its own inverse:

In[7]:=
ResourceFunction["AtbashCipher"][
 ResourceFunction["AtbashCipher"]["sander", alphabet], alphabet]
Out[7]=

Define an Atbash cipher for the Greek alphabet:

In[8]:=
ResourceFunction[
 "AtbashCipher"]["\[Pi]\[Alpha]\[Nu]\[Tau]\[Alpha] \[Rho]\
\[CurlyEpsilon]\[Iota]", Alphabet["Greek"]]
Out[8]=

Use several alphabets: the standard lower case alphabet, uppercase alphabet, and the numbers. Each of which will get ‘inverted’ in the Atbash cipher:

In[9]:=
ResourceFunction[
 "AtbashCipher"]["The secret code is 01234", {Alphabet[], ToUpperCase[Alphabet[]], IntegerString[Range[0, 9]]}]
Out[9]=

Properties and Relations

AtbashCipher is an involutory function:

In[10]:=
ResourceFunction["AtbashCipher"][
 ResourceFunction["AtbashCipher"]["sander"]]
Out[10]=

Possible Issues

Spaces, numbers, and letters outside the regular English alphabet are not transformed:

In[11]:=
ResourceFunction[
 "AtbashCipher"]["It was Chloë who opened the 2 piñatas"]
Out[11]=

Use RemoveDiacritics, StringDelete, IntegerName, and ToLowerCase to transform the message:

In[12]:=
string = StringReplace[
  ToLowerCase[
   StringDelete[
    RemoveDiacritics["It was Chloë who opened the 2 piñatas"], " "]], r : Longest[DigitCharacter ..] :> IntegerName[ToExpression@r]]
Out[12]=

Enciphering the transformed message results in a much harder to crack cipher:

In[13]:=
ResourceFunction["AtbashCipher"][string]
Out[13]=

Neat Examples

Find English words that encipher to another English word:

In[14]:=
dict = DictionaryLookup[];
dict = Select[dict, StringLength /* GreaterEqualThan[3]];
dicts = GatherBy[dict, StringLength];
matches = With[{d = #}, Select[d, MemberQ[d, ResourceFunction["AtbashCipher"][#]] &]] & /@
    dicts;
matches = Join @@ matches;
Column[TwoWayRule @@@ DeleteDuplicatesBy[{#, ResourceFunction["AtbashCipher"][#]} & /@ matches, Sort]]
Out[19]=

Resource History

See Also

License Information