Function Repository Resource:

VigenereCipher

Source Notebook

Perform Vigenère's substitution cipher on a string

Contributed by: Sander Huisman

ResourceFunction["VigenereCipher"][string,key]

enciphers string using the key key.

ResourceFunction["VigenereCipher"][string,{key1,key2,}]

repeatedly enciphers string using first key1, then key2, etc.

ResourceFunction["VigenereCipher"][{s1,s2,},key]

enciphers the strings s1,s2, etc. using the key key.

ResourceFunction["VigenereCipher"][key]

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

Details and Options

The key can be in either lowercase or uppercase letters.
Spaces in the key will leave letters unchanged, as do the letters "A" and "a" in the key.
Letters outside "a"–"z" and "A”–"Z" are not transformed.
The case of the original message is retained in the enciphered message.
When multiple keys are given and the LCM of the lengths of the keys is small, the keys get combined into a single key, which is then used to encipher string. This will, especially for long strings, yield faster results and give the same result as Fold[ResourceFunction["VigenereCipher"],string,{key1,key2,}].
ResourceFunction["VigenereCipher"][key][string] is equivalent to ResourceFunction["VigenereCipher"][string,key].

Examples

Basic Examples (2) 

Encipher a message using the key "LEMON":

In[1]:=
ResourceFunction["VigenereCipher"]["ATTACKATDAWN", "LEMON"]
Out[1]=

Encipher with multiple keys:

In[2]:=
ResourceFunction[
 "VigenereCipher"]["ATTACKATDAWNATTACKATDAWNATTACKATDAWN", {"zoology",
   "abbreviated", "acquaintances"}]
Out[2]=

Scope (3) 

Encipher a message twice, first using "GO" and then "CAT":

In[3]:=
ResourceFunction["VigenereCipher"]["ATTACKATDAWN", {"GO", "CAT"}]
Out[3]=

Encipher multiple strings:

In[4]:=
ResourceFunction["VigenereCipher"][{"Boat", "Car", "Plane"}, "sparks"]
Out[4]=

Create an operator that enciphers strings with the key "sparks":

In[5]:=
op = ResourceFunction["VigenereCipher"]["sparks"]
Out[5]=

Apply the operator to a string:

In[6]:=
op["The boat floats"]
Out[6]=

Properties and Relations (3) 

If a single character is used, the Vigenère cipher is identical to the Caesar cipher:

In[7]:=
ResourceFunction["VigenereCipher"]["ATTACKATDAWN", "D"] == ResourceFunction["CaesarCipher"]["ATTACKATDAWN", 3]
Out[7]=

When enciphering with multiple keys, the keys can first be enciphered with each other to encipher the message with a single key. The length of the replacing key has to be the LCM of the lengths of each of the keys:

In[8]:=
newkey = ResourceFunction["VigenereCipher"]["CATCAT", "GO"]
Out[8]=

This indeed matches with what you expect:

In[9]:=
ResourceFunction["VigenereCipher"]["ATTACKATDAWN", newkey] == ResourceFunction["VigenereCipher"]["ATTACKATDAWN", {"CAT", "GO"}]
Out[9]=

The case of the original text is retained; the case of the key does not matter:

In[10]:=
ResourceFunction["VigenereCipher"]["ThisIsAwesome", "CAT"]
Out[10]=
In[11]:=
ResourceFunction["VigenereCipher"]["ThisIsAwesome", "cat"]
Out[11]=

Possible Issues (4) 

Spaces and the letters "a" and "A" in the key will not alter the original message:

In[12]:=
ResourceFunction["VigenereCipher"]["ATTACKATDAWN", "a A"]
Out[12]=

The key should have valid letters and valid length:

In[13]:=
ResourceFunction["VigenereCipher"]["ATTACKATDAWN", "niño"]
Out[13]=
In[14]:=
ResourceFunction[
 "VigenereCipher"]["ATTACKATDAWN", "\[CapitalEpsilon]\[Upsilon]\[Kappa]\[Lambda]\[CurlyEpsilon]ί\[Delta]\[Eta]\[FinalSigma]"]
Out[14]=
In[15]:=
ResourceFunction["VigenereCipher"]["ATTACKATDAWN", ""]
Out[15]=

Two keys can "negate" each other:

In[16]:=
ResourceFunction["VigenereCipher"]["ATTACKATDAWN", {"LEMON", "PWOMN"}]
Out[16]=

Enciphering the keys with each other gives a string with only “A”s:

In[17]:=
ResourceFunction["VigenereCipher"]["LEMON", "PWOMN"]
Out[17]=

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

In[18]:=
ResourceFunction[
 "VigenereCipher"]["It was Chloë who opened the 2 piñatas", "LEMON"]
Out[18]=

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

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

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

In[20]:=
ResourceFunction["VigenereCipher"][string, "LEMON"]
Out[20]=

Publisher

SHuisman

Version History

  • 1.0.0 – 22 May 2019

Related Resources

License Information