Cipher

Runs in your browser. Your text never leaves your device.

Affine Cipher Encoder and Decoder

Paste an affine ciphertext and this tool tries all 312 keys for you, ranking the a and b pairs whose output looks most like English, so you can decode it without knowing the key. The affine cipher combines multiplication and a shift into a single substitution: each letter's position is multiplied by a value called a, then shifted by a value called b.

affine-cipher.encode
Plain text
Cipher text

Spyfzampifry wu hev orcv cjcpy lczzcp ah zrc qcuuimc qajcu za i vco flisc

8

Crack without the key

Solved

Solved. Two independent checks agree on the top key.

60 lettersneeds about 40312 keys tried

  1. a = 5, b = 8 chi-squared 0.33, lower is better

    Cryptography is fun when every letter of the message moves to a new place

  2. a = 5, b = 11 chi-squared 1.15, lower is better

    Rgneidvgpewn xh ujc lwtc tktgn atiitg du iwt bthhpvt bdkth id p ctl eaprt

  3. a = 9, b = 15 chi-squared 2.18, lower is better

    Jabwehrafwgb vp cts xgns ninab oneena hc egn dnppfrn dhinp eh f snx wofjn

All 312 keys (12 multipliers times 26 offsets), ranked by chi-squared against English letter frequencies. Lower is better. Scores are a ranking aid, not a guarantee: read the text.

Solved. Best key: a = 5, b = 8, chi-squared 0.33, lower is better.

Recent

Nothing yet. Your last 10 conversions, across every cipher, appear here. They are kept on this device only.

Affine Cipher at a glance

Type
Broken cipher (keyed)
Key space
312 keys
Directions
Encode and decode: Plain text to Cipher text and back
Example
HELLO → RCLLA (a = 5, b = 8, the tool's default)
Privacy
Runs in your browser, nothing is uploaded

If you know a and b, or want to encrypt a message of your own, set them yourself and read the result live. It generalises the Caesar cipher, which is really just an affine cipher with a fixed at 1. Everything runs locally in your browser.

How the Affine Cipher works

Each letter's position (A is 0 through Z is 25) is transformed by the formula y = (a times x plus b) mod 26, where x is the plaintext position and y is the ciphertext position. The value a must share no common factor with 26, otherwise two different letters could encrypt to the same result and the cipher could not be reversed.

There are exactly 12 valid values for a (1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25) and 26 possible values for b, giving 312 usable keys in total. To decrypt, the tool uses the modular inverse of a to undo the multiplication before reversing the shift.

Examples

HELLO (a=5, b=8)
RCLLA
CAT (a=3, b=5)
LFK
RCLLA (a=5, b=8, decode)
HELLO

History and origins

The affine cipher is a straightforward extension of the Caesar shift, formalised as cryptography began to describe encryption with modular arithmetic. It is a standard early exercise in cryptography courses because it introduces the idea of a mathematical key, not just a shift amount.

Because it is still a simple substitution, every plaintext letter always maps to the same ciphertext letter, so it falls to the same frequency analysis that breaks the Caesar cipher, just with a larger space of valid keys to search.

Frequently asked questions

Why can't a be any number from 1 to 25?

a must share no common factor with 26 (the alphabet length), or the encryption would not be reversible; two different letters would scramble to the same result. Only 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25 work.

How is the affine cipher different from Caesar?

Caesar only shifts letters by a fixed amount, which is the same as an affine cipher with a fixed at 1. Affine adds a multiplication step, which spreads letters around the alphabet less predictably and gives a larger key space to search.

How do you decrypt an affine cipher?

You need the same a and b used to encrypt. The tool finds the modular inverse of a, subtracts b from each ciphertext position, multiplies by that inverse, and reduces the result modulo 26.

Learn more

Go deeper on the ideas behind this tool.

Not sure this is the right tool?

If you have a message and you are not certain what it is, the cipher identifier reads the character set and the letter statistics and tells you which scheme it most likely uses, and why. It covers every code and cipher on this site.

Related translators