affine · ax+b mod 26 · coprime keys · live

Encode with the Affine cipher

Linear substitution where each letter is replaced via (a·x + b) mod 26. The multiplier a must be coprime with 26 for the cipher to be reversible. Runs locally.

Affine Cipher
Input
12 B
1 lines
Output
12 B
1 lines
Status
ready
live
Ratio
1.00×
out/in
input · text
output · affine
IHHWVCSWFRCP
Quick Start

How to use the Affine cipher

Pick coprime multiplier a and any shift b. Each letter X is replaced by (a·X + b) mod 26. Decryption uses the modular inverse of a.

  1. 01

    Choose a (coprime)

    a must be coprime with 26 — only 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25 work.

    Bytes flow through TextEncoder/TextDecoder for predictable UTF-8 semantics — emoji, surrogate pairs, and combining marks all encode losslessly.

    multiplierUTF-8TextEncoderlosslesslive
    a · raw bytes5 · UTF-8
    gcd(a, 26) = 1
    loaded · UTF-8MIME · ready
  2. 02

    Choose b (shift)

    Any value from 0 to 25 — pure shift component.

    Hash and signature operations call crypto.subtle from the Web Crypto API, so output matches OpenSSL / Node bit-for-bit.

    shiftWeb Cryptosubtledeterministiclive
    b · options8 · live
    y = (a*x + b) mod 26
    options applied · livelive
  3. 03

    Type or paste text

    Letters are transformed; everything else passes through. Live preview updates as you tune a, b, or the mode.

    Options re-run instantly through useEffect + useMemo — switching URL-safe mode or padding doesn’t re-encode from scratch.

    inputmemoisedoptionsinstantreactive
    input · encodedtext · ready
    AFFINECIPHER
    encoded · livelossless
  4. 04

    Copy or download

    One click to clipboard. Download as .txt.

    Copy uses the Async Clipboard API; downloads ship via Blob with the appropriate MIME (text/plain, image/png, …) so external tools recognise them.

    exportclipboardblobMIME-correctclient-side
    ok · copiedready · done
    Copied
    shipped · ready to shipsuccess
Pro tips

Encoding without surprises

Always UTF-8 first

Use TextEncoder / TextDecoder for bytes ↔ string conversion. btoa alone breaks on non-ASCII.

URL-safe vs standard Base64

Standard uses +, /, =; URL-safe swaps to -, _, no padding. Don’t mix them.

Hashes aren’t passwords

SHA-256 is fast; that’s good for fingerprints, bad for passwords. Use bcrypt / argon2 for credentials.

Watch padding rules

Some decoders are strict about = padding, others auto-pad. If round-trips fail at exactly 1 or 2 bytes, check padding.

Use Web Crypto for keys

Generate cryptographic keys via crypto.subtle, never Math.random. The latter is predictable.

JWT decode ≠ verify

Decoding a JWT just splits and base64-decodes. Verification needs the issuer’s public key / shared secret.

Use Cases

When to use the Affine cipher

Math-flavoured puzzles, CTFs, classroom demos.

Number theory class

Concrete example of modular inverses in action. Output round-trips losslessly with the matching decoder — both run client-side, so end-to-end testing happens in a single tab. Encoding flows through TextEncoder for predictable UTF-8 semantics.

RFC-compliant

CTF challenges

Often appears as a step-up from Caesar. Web Crypto's subtle.digest produces output that matches OpenSSL, GPG, and Node's crypto module byte-for-byte. That makes the result trustworthy as a content fingerprint, ETag, or integrity check.

OpenSSL-matched

Crypto reading

Helpful when working through textbooks that introduce affine maps. Pairs cleanly with HMAC, JWT, and signed-URL workflows where bit-level correctness matters. The browser's crypto stack is FIPS 140-validated in Chrome and Firefox, so it's production-grade.

lossless

Cross-implementation

Verify another affine implementation matches the math. Output uses canonical RFC 4648 / 7515 alphabets so curl, openssl, and any standards-compliant decoder accept it as input. URL-safe variants drop padding for tokens that travel in query strings.

byte-identical

Brute force demos

Only 12 x 26 = 312 keys — show how trivial brute force is. Heavy hashing runs through useMemo so large inputs don't block typing. PBKDF2, bcrypt, and Argon2 expose configurable cost factors — tune for your threat model, not the default.

spec-grade

Hint hiding

Hide spoilers in walkthrough articles. Same input plus same options always yields identical bytes. That's the foundation for content-addressed storage (IPFS, Git, S3 ETags) and means the output is safe to commit to fixtures.

production-ready
FAQ

Frequently asked questions

If gcd(a, 26) is greater than 1 the mapping is not bijective — multiple plaintext letters collide on the same ciphertext letter and decryption is impossible.
Caesar is the affine cipher with a=1 — the multiplier is removed, leaving only the additive shift.
Compute the modular inverse a^-1 mod 26 once, then for each letter y the plaintext is a^-1·(y - b) mod 26.
No. The keyspace is only 12 multipliers x 26 shifts = 312 — brute force breaks any affine ciphertext instantly.
No. The cipher runs locally in your browser.
Suggest a tool

Something missing?

Tell us what you need. We ship the top requests every two weeks.

Sent straight to the maintainers · no account needed
47 in queue·8 shipped this month
Request queued. Thanks!