Monoalphabetic substitution / modulo 26

Affine cipher

Encrypt or decrypt ASCII letters with the standard two-key Affine substitution while preserving the rest of your text.

01 / Workspace

Encrypt text

A=0 / Z=25
Mode

The selected direction is always used. The tool never guesses a mode, key, alphabet, or numbering convention.

Keys

Enter one of: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25.

Enter a whole number from 0 through 25.

Use unsigned ASCII decimal integers with no sign, spaces, decimal point, exponent, or leading zero. The multiplier must be coprime with 26.

0 / 10,000 code points

Only ASCII A-Z and a-z are transformed, with case preserved. Digits, whitespace, punctuation, accents, emoji, combining marks, and other scripts pass through unchanged.

Keyboard: press Ctrl+Enter or Command+Enter anywhere in the tool to run the selected mode.

Ready to encrypt.

02 / Result

Result

No result yet

The result preserves input case and every code point outside the ASCII alphabet.

03 / Exact convention

How this Affine cipher works

  1. Number each alphabet. Uppercase and lowercase are handled separately, but both use A or a as 0 through Z or z as 25.
  2. Encrypt. For a letter value x, calculate E(x) = (a*x + b) mod 26.
  3. Decrypt. Calculate the unique inverse a^-1 satisfying a*a^-1 mod 26 = 1, then use D(y) = a^-1*(y - b) mod 26. The implementation uses a nonnegative modulo operation, including when y - b is negative.
  4. Validate the key. A modular inverse exists only when gcd(a, 26) = 1. This tool accepts a only from 1 through 25 and b only from 0 through 25; it rejects rather than wraps other integers.

Known-answer checks

Classic
AFFINE CIPHER with a=5, b=8 encrypts to IHHWVC SWFRCP.
Reference
AFFINEFUNCTIONSARELINEARFUNCTIONS with a=5, b=2 encrypts to CBBQPWBYPMTQUPOCJWFQPWCJBYPMTQUPO.
Preservation
Hello, 世界! with a=7, b=3 encrypts to Afccx, 世界! and decrypts exactly.

Limits and security

  • This is the standard 26-letter Affine substitution. It does not implement A=1 numbering, custom alphabets, key wrapping, brute force, frequency analysis, or automatic key recovery.
  • Input is limited to 10,000 Unicode code points and 20,000 UTF-16 code units. A key field is limited to 64 UTF-16 code units and rejected above 16 Unicode code points. No partial result is produced for invalid or excessive input.
  • JavaScript iterates by Unicode code point. Non-ASCII code points are copied without normalization, but browser text controls normalize CR and CRLF line endings to LF.
  • Every valid key defines a fixed monoalphabetic substitution, so letter-frequency analysis and a small 312-key search can break it easily. It is educational and not secure for passwords, personal data, authentication material, or confidential messages.