Function Repository Resource:

VigenereDecipher

Source Notebook

Decipher a Vigenère-enciphered string

Contributed by: Sander Huisman

ResourceFunction["VigenereDecipher"][string,key]

deciphers string using the key key.

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

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

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

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

ResourceFunction["VigenereDecipher"][key]

represents an operator form of ResourceFunction["VigenereDecipher"] 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 enciphered message is retained in the deciphered 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 decipher string. This will, especially for long strings, yield faster results and give the same result as Fold[ResourceFunction["VigenereDecipher"],string,{key1,key2,}].
ResourceFunction["VigenereDecipher"][key][string] is equivalent to ResourceFunction["VigenereDecipher"][string,key].

Examples

Basic Examples (2) 

Decipher a message using the key "LEMON":

In[1]:=
ResourceFunction["VigenereDecipher"]["LXFOPVEFRNHR", "LEMON"]
Out[1]=

Decipher with multiple keys:

In[2]:=
ResourceFunction[
 "VigenereDecipher"]["ZKYWUTTLKFMFZSLIFDWFYCXEQQDVZSHQHZVA", {"zoology", "abbreviated", "acquaintances"}]
Out[2]=

Scope (3) 

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

In[3]:=
ResourceFunction["VigenereDecipher"]["IHSQIRIHCQCU", {"GO", "CAT"}]
Out[3]=

Decipher multiple strings:

In[4]:=
ResourceFunction[
 "VigenereDecipher"][{"Tdak", "Upr", "Haaeo"}, "sparks"]
Out[4]=

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

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

Apply the operator to a String:

In[6]:=
op["Lwe lgsi wvgsis"]
Out[6]=

Properties and Relations (4) 

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

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

When deciphering with multiple keys, the keys can first be enciphered with each other to decipher 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["VigenereDecipher"]["IHSQIRIHCQCU", newkey] == ResourceFunction["VigenereDecipher"][
  "IHSQIRIHCQCU", {"CATCAT", "GO"}]
Out[9]=

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

In[10]:=
ResourceFunction["VigenereDecipher"]["VhbuIlCwxuofg", "CAT"]
Out[10]=
In[11]:=
ResourceFunction["VigenereDecipher"]["VhbuIlCwxuofg", "cat"]
Out[11]=

VigenereDecipher is related to the resource function VigenereCipher:

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

Possible Issues (3) 

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

In[13]:=
ResourceFunction["VigenereDecipher"]["DNESIASY", "a A"]
Out[13]=

The key should have valid letters and valid length:

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

Two keys can "negate" each other:

In[17]:=
ResourceFunction[
 "VigenereDecipher"]["LXFOPVEFRNHR", {"LEMON", "PWOMN"}]
Out[17]=

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

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

Publisher

SHuisman

Version History

  • 1.0.0 – 14 June 2019

Related Resources

License Information