Affine Cipher

Encrypt and decrypt text with the Affine cipher using two numeric keys: multiplier a and shift b. Learn how modular arithmetic creates a classical substitution cipher.

Input
0 chars · 0 bytes
Try:
Result
✓ Monoalphabetic substitution cipher ✓ Two numeric keys: a and b ✓ Multiple alphabets supported ✓ Processed on our server
Examples
Encrypt with a=5 and b=8 Key: 5 Shift: 8
Input AFFINE CIPHER
Output IHHWVC SWFRCP

Classic English Affine example with multiplier 5 and shift 8.

Decrypt with a=5 and b=8 Key: 5 Shift: 8
Input IHHWVC SWFRCP
Output AFFINE CIPHER

The same numeric keys restore the plaintext.

Different key pair Key: 7 Shift: 3
Input HELLO WORLD
Output AFCCX BXSCY

Changing multiplier a and shift b creates a different substitution alphabet.

Preserve spaces and punctuation Key: 5 Shift: 8
Input MEET AT 10:00!
Output QCCZ IZ 10:00!

Only alphabet characters are encrypted. Spaces, numbers, punctuation marks, and other symbols remain unchanged.

How the Affine cipher works

The Affine cipher is a classical monoalphabetic substitution cipher that transforms each letter using a mathematical formula. Every letter is first converted to a number, then encrypted with E(x) = (a × x + b) mod m, where x is the letter position and m is the alphabet size.

The key consists of two numbers. The multiplier a changes how letter positions are scaled, while the shift b moves the result through the alphabet. Together they create a fixed substitution alphabet.

To decrypt a message, the value of a must have a modular inverse. This is only possible when a is coprime with the alphabet size. Spaces, digits, and punctuation are preserved unchanged.

Affine cipher keys

The Affine cipher uses two numeric parameters: a and b. The value a is the multiplier, and b is the shift. Both values affect the final substitution alphabet.

Not every multiplier is valid. For English with 26 letters, a must be coprime with 26. Valid examples include 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. If a is not valid, decryption cannot reliably restore the original message.

Affine vs Caesar cipher

The Caesar cipher uses only one operation: a fixed shift. The Affine cipher extends this idea by combining multiplication and addition modulo the alphabet size.

If a = 1, the Affine cipher behaves like a Caesar cipher with shift b. When a is another valid multiplier, the substitution pattern becomes less obvious while still remaining a monoalphabetic substitution cipher.

Security of the Affine cipher

The Affine cipher is stronger than a basic Caesar shift because it has more possible keys, but it is still weak by modern cryptographic standards.

Because each plaintext letter always maps to the same ciphertext letter, the cipher remains vulnerable to frequency analysis and brute-force testing of possible key pairs. Today it is mainly useful for education, puzzles, and learning modular arithmetic in cryptography.

FAQ

Decryption needs the modular inverse of a. If a and the alphabet size have a common divisor, that inverse does not exist and several letters can map to the same ciphertext letter.

The value a is the multiplier and b is the shift. Together they define the mathematical transformation used to substitute each letter.

If a is not coprime with the alphabet size, several letters may map to the same encrypted letter. In that case decryption becomes ambiguous or impossible.

No. Caesar uses only a fixed shift, while Affine uses both multiplication and addition. However, Caesar is a special case of Affine when a = 1.

No. It is a historical cipher and can be broken with frequency analysis or brute-force testing. It is best used for learning, puzzles, and demonstrations.

No. In this tool, only alphabet characters are transformed. Spaces, numbers, punctuation marks, and other symbols are preserved unchanged.
Related tools

Caesar Cipher

Classic letter-shift cipher with custom shift values.

Playfair Cipher

Classic digraph substitution cipher with keyword matrix encryption.

Vigenere Cipher

Keyword-based polyalphabetic encryption and decryption.

Vernam Cipher

XOR-based Vernam encryption with Base64 output.