Wolfram Research

Function Repository Resource:

AffineDecipher

Source Notebook

Decipher a string using the affine cipher

Contributed by: Sander Huisman

ResourceFunction["AffineDecipher"][string,{a,b}]

deciphers string using the affine cipher with parameters a and b.

ResourceFunction["AffineDecipher"][{a,b}]

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

Details and Options

For the affine cipher the letters az are numbered 0–25. Each character in string is converted to a number (x) using the aforementioned numbering. Each number is then transformed by computing (ax+b) mod 26. The new numbers are then reinterpreted as letters. Here is an example enciphering the letters AZ for a=5 and b=8. Deciphering is done by reading the diagram from bottom to top.

The case of the letters is retained by ResourceFunction["AffineDecipher"].

Examples

Basic Examples

Convert a simple text:

In[1]:=
ResourceFunction["AffineDecipher"]["ihhwvc swfrcp", {5, 8}]
Out[1]=

Scope

AffineDecipher automatically maps over a list of strings:

In[2]:=
ResourceFunction[
 "AffineDecipher"][{"zrwu", "wu", "i", "zcuz"}, {5, 8}]
Out[2]=

Create an operator:

In[3]:=
op = ResourceFunction["AffineDecipher"][{5, 8}]
Out[3]=

Apply the operator to a string:

In[4]:=
op["nillaav"]
Out[4]=

Properties and Relations

The Caesar decipher can be recreated using the affine decipher by choosing a=1:

In[5]:=
string = "Bpqa qa rcab i bmab";
{ResourceFunction["AffineDecipher"][string, {1, 8}], ResourceFunction["CaesarDecipher"][string, 8]}
Out[6]=

AffineDecipher could be recast in terms of ResourceFunction["AffineCipher"]:

In[7]:=
enciphered = "ihhwvc swfrcp";
deciphered = ResourceFunction["AffineDecipher"][enciphered, {5, 8}]
Out[8]=

Again decipher, this time using AffineCipher with appropriate parameter values for inverting:

In[9]:=
minv = ModularInverse[5, 26];
ResourceFunction["AffineCipher"][enciphered, {minv, -8 minv}]
Out[10]=

Possible Issues

The parameter a must be coprime to 26:

In[11]:=
ResourceFunction["AffineDecipher"]["vlyetg mzbvjly", {14, 11}]
Out[11]=

Some of the permissible values for a are:

In[12]:=
Select[Range[100], CoprimeQ[#, 26] &]
Out[12]=

Resource History

Related Resources

License Information