Affine cipher encoder

The affine cipher combines multiplication and a shift, and is used in maths and cryptography lessons. It is a puzzle cipher, not a security tool.

Loading the tool…

Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.

How to use it

  1. Enter text, multiplier a and shift b.
  2. The result appears straight away and updates as you edit.
  3. Copy the result, or copy a link that keeps the same values to share it.

How it is calculated

Encoded = (a × x + b) mod 26, where x is the letter index (A = 0). a must be coprime with 26, so only 12 values are allowed.

Example

Inputs: Multiplier a = 5, Shift b = 8. Result: Encoded text: Ihhwvc swfrcp; Inverse of a for decoding: 21.

Affine cipher
Ihhwvc swfrcp

Limits and caveats

  • This is a puzzle and teaching cipher that is easy to break, so never use it to protect passwords or sensitive data.
  • Only Latin letters A to Z change; Arabic letters, digits and symbols stay as they are.

Questions about this tool

Why can I not choose a = 2 or 13?

Both divide 26, so two letters would map to the same letter and decoding would be impossible. Allowed values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25.

What is the inverse of a shown here?

It is the number that gives remainder 1 when multiplied by a and divided by 26. The decoder uses it.

How many affine keys exist?

12 choices of a times 26 choices of b gives 312 keys, few enough to try them all.