The Affine Cipher

Encryption

To encrypt, the formula is:

E(x) = (ax + b) mod m

Where:

Decryption

To decrypt, the formula is:

D(y) = a⁻¹(y - b) mod m

Values Coprime with 26

Values coprime with 26 are:

1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25

These values share no common factors with 26 except 1. For example:

gcd(3, 26) = 1

Solving for a Modular Inverse

Encryption example:

E(x) = (3x + 4) mod 26

Decryption formula:

D(y) = a⁻¹(y - 4) mod 26

Using the Euclidean Algorithm:

The Euclidean Algorithm is a method for finding the greatest common divisor (GCD) of two numbers — the largest number that divides both evenly. It works by repeatedly dividing the larger number by the smaller one and keeping track of the remainders until reaching zero. The last non-zero remainder is the GCD. In the context of the Affine Cipher, this algorithm helps us find the modular inverse of a number, which is necessary for decryption. By “rewinding” the division steps using the Extended Euclidean Algorithm, we can express 1 as a combination of the two numbers. The coefficient of the encryption key in this combination is the modular inverse, allowing us to reverse the encryption process.

Back-substitution gives:

1 = 9*3 - 1*26 → a⁻¹ = 9

Example: Encrypting and Decrypting "Math"

Convert letters to numbers:

Encrypt M:

E(12) = (3*12 + 4) mod 26 = 40 mod 26 = 14 → O

Encrypting "Math" gives: Oejz

Decrypt O:

D(14) = 9*(14-4) mod 26 = 12 → M

Affine Cipher Key Example

Encryption Key: E(x)=(3x+4) mod 26

Decryption Key: D(y)=9(y-4) mod 26

E(x) = (ax + b) mod m

Encrypt / Decrypt Text (Only avabile with the Encryption Key)

Enter text and specify your encryption keys `a` and `b`. a must be coprime with 26.




10/3/25 - James Kastner


Site hosted by Neocities Powered by OpenAI