Affine Cipher Online — Encrypt & Decrypt Messages With the Affine Cipher

CodexFuse Team · 2026-10-13T15:07:06.900Z · 6 min read min read

Encrypt and decrypt messages with the affine cipher online. Learn the formula, valid keys, worked examples, and why it is not real security.

The affine cipher is a classical substitution cipher that encrypts each letter by running it through a small mathematical formula. If you have ever wondered how a few numbers can scramble an entire message, and how the same numbers can unscramble it, the affine cipher is the clearest possible example.

In this guide, you will learn how the affine cipher works, which keys are valid, how to encrypt and decrypt messages by hand with labelled fictional worked examples, and how to use the free affine cipher tool.

What Is the Affine Cipher?

The affine cipher is a classical substitution cipher that turns each letter into a number, applies the formula (a times x plus b) mod 26, and turns the result back into a letter. The pair (a, b) is the key, and the same key that encrypts a message also decrypts it.

The multiplier a stretches the alphabet, and the shift b slides it along. Together they produce a scrambled alphabet that follows a strict rule.

LetterABCDEFG
Number (x)0123456

Each letter becomes its position in the alphabet, with A as 0 and Z as 25. The formula works on those numbers, and "mod 26" means wrapping around when a result passes 25.

What Keys Are Valid for the Affine Cipher?

A key is valid for the affine cipher when a shares no common factor with 26 other than 1, which mathematicians call being coprime with 26. Valid values of a are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25, while b can be any number from 0 to 25. You may also like our in-depth guide to Rail Fence Cipher Online.

The rule exists because decryption must undo the multiplication. If a shares a factor with 26, two different letters can encrypt to the same letter, and decryption cannot tell them apart.

How Do You Encrypt a Message With the Affine Cipher?

To encrypt a message with the affine cipher, pick a valid key (a, b), convert each letter to its number, apply E(x) = (a times x plus b) mod 26, and convert each result back to a letter. Leave spaces and punctuation unchanged, and encrypt one letter at a time.

Fictional worked example with a = 5 and b = 8

Take the fictional message "HELLO": H is 7, E is 4, L is 11, L is 11, and O is 14. Now apply the formula to each number.

  1. H: (5 times 7 plus 8) = 43. 43 mod 26 = 17, which is R.
  2. E: (5 times 4 plus 8) = 28. 28 mod 26 = 2, which is C.
  3. L: (5 times 11 plus 8) = 63. 63 mod 26 = 11, which is L.
  4. L: the same calculation gives L again.
  5. O: (5 times 14 plus 8) = 78. 78 mod 26 = 0, which is A.

How Do You Decrypt an Affine Cipher Message?

To decrypt an affine cipher message, find the modular inverse of a, subtract b from each cipher letter's number, multiply by that inverse, and take the result mod 26. Convert each result back to a letter, and the original message reappears exactly.

Fictional worked example: decrypting "RCLLA" with a = 5 and b = 8

First find the inverse of 5. The number 21 works, because 5 times 21 equals 105, and 105 mod 26 equals 1.

  1. R (17): 21 times (17 minus 8) = 189. 189 mod 26 = 7, which is H.
  2. C (2): 21 times (2 minus 8) = negative 126. Add 26 repeatedly to reach 4, which is E.
  3. L (11): 21 times (11 minus 8) = 63. 63 mod 26 = 11, which is L.
  4. L: the same calculation gives L again.
  5. A (0): 21 times (0 minus 8) = negative 168. Add 26 repeatedly to reach 14, which is O.

Is the Affine Cipher Secure?

No. The affine cipher is not secure for real secrets because it has only 312 possible keys and keeps letter frequencies visible, so anyone can crack it by trying every key or by matching frequent letters. Real data needs modern encryption such as AES-256 or TLS, and the affine cipher should stay in puzzles and classrooms. Related reading: our complete guide on Beaufort Cipher Online.

Treat the affine cipher as a learning tool, not protection. If a message would cause harm if read by someone else, do not encrypt it with a classical cipher. Use a modern tool built on AES-256 or TLS instead.

For real protection, see our guide to the free online AES encryption tool.

How Do You Use the Affine Cipher Tool Online?

Open the free affine cipher tool, type or paste your message, enter your values for a and b, and the tool shows the encrypted or decrypted result at once. The tool checks that a is valid, applies the formula to every letter, and lets you copy the result.

  1. Open the affine cipher tool in your browser.
  2. Type or paste your message into the input box. Use a test message, not a real secret.
  3. Enter your key: pick a valid value for a from the list above, and any value from 0 to 25 for b.
  4. Choose encrypt or decrypt, and read the result, which appears at once.
  5. Copy the result into your puzzle or lesson. Try the affine cipher tool again with the opposite mode to confirm you get back your original message.

For comparison, see our guide to the Caesar cipher and our guide to the Vigenere cipher.

Security chores piling up while you run your business?

CodexFuse runs your servers, updates, backups, and security as one fully managed platform on pay-as-you-go credits, so real protection is handled for you while you focus on growth.

Get Started with CodexFuse

Frequently Asked Questions

What is the affine cipher formula?

Encryption uses E(x) = (a times x plus b) mod 26. Decryption uses D(y) = a-inverse times (y minus b) mod 26, where a-inverse undoes the multiplication by a. Find hundreds more practical how-tos on our blog.

How many keys does the affine cipher have?

The affine cipher has 312 keys: 12 valid choices for a multiplied by 26 choices for b. That tiny key space is why the cipher is easy to crack.

Why must a be coprime with 26 in the affine cipher?

If a shares a factor with 26, two plaintext letters can encrypt to the same cipher letter, and decryption cannot tell them apart.

Is the affine cipher the same as the Caesar cipher?

No. The Caesar cipher only shifts letters, while the affine cipher multiplies and then shifts. When a equals 1, it becomes a Caesar cipher.

How do you decrypt an affine cipher without knowing the key?

Try all 312 keys and look for readable text, or match the most frequent cipher letters to the most frequent letters in the language.

The affine cipher is a small formula with a big lesson: a cipher is only as strong as its key space and its ability to hide patterns. Learn it for the maths, and leave real protection to modern encryption.

Featured on Twelve Tools Hunt for Tools Featured on LaunchBuff Launch Llama Tools Launch your MCP Top Web Development Companies in the US - OnToplist.com Designer Listings Listed on SeeWhatNewAI Featured on SubmitMySaas Featured on AI Agents Directory Product Wing SaaS Wheel Featured on Stremit Featured on Thank John We Are Founders SubmitMySaas ShinyLaunch SaaS Tools Dir Featured on SaasHunt Product List Dir Featured on ShowMeBestAI Listed on Best-AI.org Featured on SaaSCity Listed on LaunchPact CodexFuse Solution on goodSaaS Featured on IndieLaunch Featured on BestAIApps AI X Collection Featured on SemiLaunch Listed on AlterBase We Like Tools codexfuse.dev LaunchClash Featured on Next Launch