Affine Cipher Encoder and Decoder

The Affine cipher encrypts each letter using the mathematical formula E(x) = (ax + b) mod 26, where 'a' and 'b' are integer keys. This monoalphabetic substitution cipher combines multiplicative and additive shifts to transform plaintext. Use our free tool below to encode and decode messages instantly with any valid key pair.

Plaintext
0 characters
Ciphertext
0 characters

Substitution Tablea = 5, b = 8

PlainABCDEFGHIJKLMNOPQRSTUVWXYZ
CipherINSXCHMRWBGLQVAFKPUZEJOTYD
Affine Cipher Formula
E(x) = (ax + b) mod 26Encryption
D(y) = a⁻¹(y - b) mod 26Decryption

Frequently Asked Questions About Affine Cipher

What is the Affine cipher?

The Affine cipher is a monoalphabetic substitution cipher that encrypts letters using a mathematical formula. It combines a multiplicative and an additive shift, transforming each letter through E(x) = (ax + b) mod 26, where 'a' and 'b' are integer keys. Unlike simple shift ciphers, the Affine cipher uses two keys to create a more complex letter mapping, making it a foundational example of algebraic cryptography.

How does the Affine cipher formula work?

The formula E(x) = (ax + b) mod 26 converts each letter to a number (A=0, B=1, ... Z=25), multiplies by key 'a', adds key 'b', then takes the remainder after dividing by 26. For decryption, the formula D(y) = a⁻¹(y - b) mod 26 reverses the process using the modular multiplicative inverse of 'a'. For example, with a=5 and b=8, the letter A (0) encrypts to (5*0+8) mod 26 = 8, which is I.

What are valid key values for the Affine cipher?

The multiplicative key 'a' must be coprime with 26, meaning gcd(a, 26) = 1. The 12 valid values for 'a' are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. The additive key 'b' can be any integer from 0 to 25. This gives a total of 12 x 26 = 312 unique key combinations. If 'a' is not coprime with 26, multiple plaintext letters map to the same ciphertext letter, making decryption impossible.

How do you decrypt an Affine cipher?

Decryption uses the formula D(y) = a⁻¹(y - b) mod 26, where a⁻¹ is the modular multiplicative inverse of 'a' modulo 26. First, subtract 'b' from the ciphertext letter's numerical value. Then multiply by a⁻¹ and take mod 26. For example, if a=7 then a⁻¹=15 because (7 x 15) = 105 and 105 mod 26 = 1. Without knowing the keys, you can attempt brute force over all 312 combinations or use frequency analysis.

What is modular arithmetic in the Affine cipher?

Modular arithmetic is clock-style math where numbers wrap around after reaching a set value. In the Affine cipher, mod 26 ensures results stay within the 26-letter alphabet. After computing (ax + b), the mod 26 operation returns the remainder when divided by 26, mapping the result back to a valid letter position (0-25). This wrapping behavior is what allows the cipher to cycle through the alphabet seamlessly.

How is the Affine cipher related to the Caesar cipher?

The Caesar cipher is a special case of the Affine cipher where a=1. With a=1, the Affine formula E(x) = (1*x + b) mod 26 simplifies to E(x) = (x + b) mod 26, which is exactly the Caesar shift formula. The Affine cipher generalizes the Caesar cipher by adding a multiplicative component, increasing the key space from 26 to 312 possible keys and creating a non-uniform letter mapping.

Is the Affine cipher secure?

No, the Affine cipher is not secure for modern use. Its key space of only 312 combinations can be brute-forced in milliseconds by a computer. As a monoalphabetic cipher, it is also vulnerable to frequency analysis because each plaintext letter always maps to the same ciphertext letter. However, it remains valuable as an educational tool for teaching modular arithmetic, number theory, and the mathematical foundations of modern cryptography.

What is the Affine Cipher?

The affine cipher is a type of monoalphabetic substitution cipher that uses mathematical functions to encrypt and decrypt messages. Unlike the simpler Caesar cipher, the affine cipher combines both multiplication and addition operations, making it more secure while still being relatively easy to understand.

The affine cipher was developed as an extension of shift ciphers, adding a multiplicative component to increase the key space. This classical encryption method is widely used in cryptography education to teach fundamental concepts of modular arithmetic and number theory.

The Affine Cipher Formula

The affine cipher uses two keys: a (the multiplicative key) and b (the additive key). The encryption and decryption formulas are:

Encryption: E(x) = (ax + b) mod 26

Decryption: D(y) = a⁻¹(y - b) mod 26

Where:

  • x is the numerical value of the plaintext letter (A=0, B=1, ..., Z=25)
  • y is the numerical value of the ciphertext letter
  • a must be coprime with 26 (valid values: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25)
  • b can be any integer from 0 to 25
  • a⁻¹ is the modular multiplicative inverse of a

How to Use This Affine Cipher Tool

Our online affine cipher encoder makes encryption and decryption simple:

  1. Enter your text in the input field
  2. Select key A from the dropdown (only valid coprime values shown)
  3. Adjust key B using the slider (0-25)
  4. Choose mode: Encrypt or Decrypt
  5. Copy the result with one click

The tool automatically validates your key selection and provides real-time conversion. You can also view the complete substitution alphabet generated by your chosen keys.

Features of Our Affine Cipher Tool

  • Real-time encryption and decryption - See results as you type
  • Smart key validation - Only valid A values (coprime with 26) are selectable
  • Substitution table display - View the complete letter mapping
  • Copy to clipboard - One-click result copying
  • Preserve case and spacing - Non-alphabetic characters pass through unchanged
  • Mobile responsive - Works on all devices

For automatic decryption without knowing the keys, try our Affine Cipher Decoder. To understand the mathematics behind valid keys, visit our Key Calculator.

Frequently Asked Questions

What is the affine cipher used for?

The affine cipher is primarily used for educational purposes to teach cryptography fundamentals, including modular arithmetic, multiplicative inverses, and cryptanalysis techniques. While not secure for modern applications, it provides an excellent introduction to mathematical encryption.

How many possible keys does the affine cipher have?

The affine cipher has 312 possible key combinations (12 valid values for A multiplied by 26 values for B). This is larger than the Caesar cipher's 26 keys but still small enough to be vulnerable to brute-force attacks.

Why must key A be coprime with 26?

Key A must be coprime with 26 (meaning gcd(A, 26) = 1) to ensure that each plaintext letter maps to a unique ciphertext letter. If A shares a common factor with 26, multiple letters would encrypt to the same result, making decryption impossible.

What are the valid values for key A?

The valid values for key A are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. These are all integers less than 26 that are coprime with 26.

How do I decrypt an affine cipher without knowing the keys?

You can use frequency analysis or brute-force methods to decrypt an affine cipher without the keys. Our decoder tool automatically tests all 312 key combinations and ranks results by likelihood based on English letter frequency patterns.

Is the affine cipher secure?

No, the affine cipher is not secure for modern use. With only 312 possible keys, it can be easily broken by brute-force attack or frequency analysis. It is best suited for educational purposes and understanding cryptographic concepts.