Skip to content
TextArray
100% local

Affine cipher

Encrypt text using the affine cipher, a linear mathematical encryption based on modular arithmetic.

Input
Output

Affine cipher

The affine cipher is a classical encryption method that combines multiplication and addition in modular arithmetic. Each letter is transformed by the formula E(x) = (a*x + b) mod 26, where a and b are your chosen keys and x is the letter's position (0–25). To decrypt, you apply the inverse transformation using the modular multiplicative inverse of a.

Unlike simpler ciphers like Caesar (which only shifts), the affine cipher offers more security through two independent parameters. The multiplier a must be coprime with 26 — meaning it shares no common factors other than 1 — giving you twelve valid choices: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25. Combined with 26 possible offsets (b), this yields 312 different key combinations, a significant step up from Caesar's 25.

Paste your plaintext and select encode to encipher it, or paste ciphertext and select decode to recover the original. The multiplier controls how letters are scrambled, while the offset shifts them afterward. Capitalization and non-letter characters pass through unchanged. Everything runs in your browser, so your text and keys never leave your device. The live tally shows how many letters were transformed and the total character count.

FAQ

What is an affine cipher?
An affine cipher is a substitution cipher where each letter is encrypted using a linear transformation: E(x) = (a*x + b) mod 26. It combines multiplication and addition to scramble text, offering more variety than a simple Caesar shift.
Why must a be coprime with 26?
For decryption to work, the multiplier a must have a modular multiplicative inverse modulo 26. This only exists if a and 26 share no common factors — i.e., they are coprime. Valid values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25.
How do I decrypt a message?
Switch the mode to Decode, enter the same a and b values that were used to encrypt, and paste the ciphertext. The tool computes a's modular inverse and reverses the transformation: D(y) = a_inv * (y - b) mod 26.
Is the affine cipher secure?
No. With only 312 possible keys, it is vulnerable to brute-force attack and frequency analysis. It is suitable for puzzles and teaching cryptography, but not for protecting sensitive information.
Is my text uploaded anywhere?
No. The encryption and decryption run entirely in your browser. Your text and keys are never sent to a server.