1. Home
  2. Writing & text tools
  3. Affine Cipher

Affine Cipher

Practise modular arithmetic with a real cipher: choose a and b, see the formula and its inverse, and check your hand calculations against the substitution table.

Free, no sign-up. Runs in your browser: nothing is uploaded.

How to use Affine Cipher

  1. Choose a (coprime with 26) and b.
  2. Type the text and the direction.
  3. Or crack a cipher text by trying every key.

Features

  • Only valid multipliers offered (1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25)
  • Decryption with the modular inverse shown
  • Substitution table
  • Cracker ranks all keys
  • This is a classical cipher for puzzles, games and teaching. It does not protect real secrets.

Supported formats

You can open

  • Typed or pasted text
  • TXT

You get

  • Copied text
  • TXT

Private by design

Your files are opened and processed by your browser on this device. They are never uploaded to openWorkstand or anyone else, so the Affine Cipher is safe for contracts, IDs, photos and work documents. After you have used it once while online, it also works without an internet connection. How local processing works

Good to know

  • A classical cipher for puzzles and teaching: it can be broken quickly and must not be used to protect real secrets.

Guide

The affine cipher: E(x) = (ax + b) mod 26

The affine cipher turns each letter into a number (A = 0, B = 1 … Z = 25), multiplies it by a, adds b and takes the remainder after dividing by 26. The result is turned back into a letter. With a = 5 and b = 8, the word AFFINE becomes IHHWVC. It is a favourite in mathematics and computer science courses because it puts modular arithmetic to work in a few lines.

Choosing a key

The shift b can be any number from 0 to 25. The multiplier a cannot: to decrypt, a must have an inverse modulo 26, which exists only when a shares no factor with 26. Since 26 = 2 × 13, every even number and 13 are ruled out, leaving 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Only these are offered, so you cannot pick a key that loses information. With a = 2, for example, A and N would both become the same letter.

Decryption and the inverse

Decryption reverses the steps: subtract b, then multiply by the inverse of a, again modulo 26. The formula line under the settings shows both formulas with your numbers, including the inverse — 21 for a = 5, because 5 × 21 = 105 leaves a remainder of 1 when divided by 26. Students can use it to check calculations done by hand.

The substitution table

Because a and b are fixed, the affine cipher is a simple substitution: every A becomes the same letter, every B another, and so on. The table under the tool shows the complete alphabet mapping. It is useful for encrypting by hand and for seeing special cases: with a = 1 the cipher is a Caesar shift by b, and with a = 25 and b = 25 it is exactly the Atbash cipher.

Breaking it

There are only 12 × 26 = 312 key combinations, one of which leaves the text unchanged. The cracker decrypts the message with all 311 others and ranks the results by how English they look, using letter frequencies and common words. On a sentence or two, the right key comes first. Frequency analysis by hand also works: if the two most common cipher letters correspond to E and T, two equations give a and b.

In the classroom

The affine cipher is a compact exercise in modular arithmetic, inverses and the importance of coprime numbers, and a natural step between the Caesar cipher and more complex systems. Encrypt with the tool, decrypt by hand, then compare.

Private

All calculations run in your browser.

A worked example

With a = 5 and b = 8, encrypt the letter F. F is 5, so 5 × 5 + 8 = 33, and 33 mod 26 = 7, which is H. To decrypt H, subtract 8 to get −1, which is 25 modulo 26, then multiply by the inverse 21: 25 × 21 = 525, and 525 mod 26 = 5, which is F again. The substitution table shows the whole mapping at once, and the formula line confirms the inverse. Worked through by hand like this a few times, the role of the modular inverse becomes clear, which is why the cipher is so popular in exam questions.

See the special cases in the Caesar Cipher Solver and Atbash Cipher, use a keyword with the Vigenère Cipher, or practise number systems with the Number Base Converter.

Checked against the current version of the Affine Cipher on .

Troubleshooting

The file does not open

Check that it is one of the supported formats above and opens in another app. Damaged files and password-protected PDFs cannot be read until they are repaired or unlocked.

It is slow or stops on a very large file

Everything runs on your device, so speed depends on its memory. Close other tabs, use a computer rather than a phone, or split the file into smaller parts first.

The download did not start

Look for your browser’s download bar or icon. Some browsers ask once before a site may download files; allow it and click the button again.

Frequently asked questions

How do I find a⁻¹ by hand?

Look for the number that gives a remainder of 1 when multiplied by a and divided by 26. For a = 7, try multiples: 7 × 15 = 105 = 4 × 26 + 1, so the inverse is 15. The formula line shows it for every key.

Can two different keys give the same cipher?

No. Each of the 312 valid combinations of a and b produces a different substitution alphabet, and a = 1 with b = 0 leaves the text unchanged, which is why the cracker tries 311 keys.

Why does the cipher number letters from 0?

With A = 0, the arithmetic modulo 26 works out neatly: the 26 letters map to the remainders 0 to 25. Textbooks and exam questions on the affine cipher use this numbering.

Why can a not be 2 or 13?

To decrypt, a must have an inverse modulo 26, which only exists when a shares no factor with 26. Even numbers and 13 fail, because 26 = 2 × 13.

How is the inverse of a found?

It is the number a⁻¹ with a × a⁻¹ ≡ 1 (mod 26). For a = 5 it is 21, because 5 × 21 = 105 = 4 × 26 + 1. The formula line shows it for your key.

Keep working with the result

After you download, choose “Save to Workstand” to keep the result in a folder on your computer, attach it to a project and create a follow-up task. It is saved by your browser, not uploaded.