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
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
The result preserves input case and every code point outside the ASCII alphabet.
03 / Exact convention
How this Affine cipher works
- Number each alphabet. Uppercase and lowercase are handled separately, but both use A or a as 0 through Z or z as 25.
- Encrypt. For a letter value
x, calculateE(x) = (a*x + b) mod 26. - Decrypt. Calculate the unique inverse
a^-1satisfyinga*a^-1 mod 26 = 1, then useD(y) = a^-1*(y - b) mod 26. The implementation uses a nonnegative modulo operation, including wheny - bis negative. - Validate the key. A modular inverse exists only when
gcd(a, 26) = 1. This tool acceptsaonly from 1 through 25 andbonly from 0 through 25; it rejects rather than wraps other integers.
Known-answer checks
- Classic
AFFINE CIPHERwitha=5,b=8encrypts toIHHWVC SWFRCP.- Reference
AFFINEFUNCTIONSARELINEARFUNCTIONSwitha=5,b=2encrypts toCBBQPWBYPMTQUPOCJWFQPWCJBYPMTQUPO.- Preservation
Hello, 世界!witha=7,b=3encrypts toAfccx, 世界!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.