Affine Cipher Encoder/Decoder

Interactive Affine Cipher Calculator for encoding and decoding text using the Affine cipher, a popular cryptography technique.

Full original guide (expanded)

Affine Cipher Encoder/Decoder

Encrypt or decrypt messages with an affine cipher by setting multiplier and shift keys. Review modular steps per character to validate the mapping.

Affine Cipher Tool

Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25).

Any integer. We automatically reduce a and b modulo m.

Options
m = 26 (A–Z)
Modular inverse & key details
Per-character mapping steps

How the Affine Cipher Works

The Affine Cipher is a classical substitution cipher. Each letter is first mapped to a number, transformed with a linear function modulo the alphabet size, and then mapped back to a letter.

Letter-to-number mapping

For the standard English alphabet:

A → 0, B → 1, C → 2, …, Z → 25

Encryption formula

Let:

  • m = size of the alphabet (26 for A–Z)
  • a, b = integer keys
  • x = numeric value of a plaintext letter
E(x) = (a·x + b) mod m

The result E(x) is then converted back to a letter.

Decryption formula

To decrypt, you must compute the modular inverse of a modulo m, denoted a⁻¹, such that:

a · a⁻¹ ≡ 1 (mod m)

Then, for each ciphertext letter with numeric value y:

D(y) = a⁻¹ · (y − b) mod m

Valid values of a

The cipher is only invertible if a and m are coprime (their greatest common divisor is 1). For the 26-letter alphabet, the valid values of a are:

{1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25}

If you choose an invalid a, this tool will warn you and refuse to decrypt, because no modular inverse exists.

Example: Encrypting “AFFINE CIPHER”

Using keys a = 5, b = 8, m = 26:

  1. Map letters to numbers: A=0, F=5, I=8, N=13, E=4, C=2, P=15, H=7, R=17.
  2. Apply E(x) = (5·x + 8) mod 26 to each letter.
  3. Map the results back to letters.

The tool above shows each step, including the numeric mapping and modular arithmetic, so you can follow the process line by line.

Common Questions

Can I use a custom alphabet?

Yes. Choose “Custom alphabet” and enter any sequence of unique characters. The length of this string becomes the modulus m. This is useful for:

  • Including digits (0–9) or punctuation
  • Working with non-English alphabets
  • Designing puzzle-specific symbol sets

How does this tool handle spaces and punctuation?

By default, spaces, punctuation, and digits are preserved unchanged (they are not encrypted). If you want them to be part of the cipher, include them in a custom alphabet and uncheck “Keep spaces, punctuation, digits”.

Is the Affine Cipher secure?

No. The Affine Cipher is easily broken with frequency analysis or by trying all possible key pairs. It is useful for learning and puzzles, but not for protecting sensitive information.

FAQ

How do I choose good keys a and b?

First pick a value of a that is coprime with the alphabet size m. For A–Z, use one of the 12 valid values listed above. Then choose any integer for b. This tool automatically reduces both a and b modulo m, so you do not need to worry about negative or large values.

What happens if I use an invalid a?

If gcd(a, m) ≠ 1, the mapping is not one-to-one: multiple plaintext letters map to the same ciphertext letter. In that case, decryption is impossible. The calculator detects this and shows an error instead of producing misleading output.

Can I recover the keys from a known plaintext and ciphertext?

In theory, yes: two distinct plaintext–ciphertext letter pairs are enough to solve for a and b. This is one reason the Affine Cipher is weak. This tool focuses on encoding/decoding, but you can use the formulas to solve for a and b manually:

y₁ ≡ a·x₁ + b (mod m)
y₂ ≡ a·x₂ + b (mod m)

Subtract the equations to solve for a, then back-substitute to find b.

About the author

Ugo Candido builds financial tools and educational resources to help readers make better money decisions. He focuses on practical, transparent models that reflect how lenders calculate payments and total cost of ownership.

Contact: info@calcdomain.com

Editorial policy

CalcDomain content is created for educational purposes and is reviewed for clarity, accuracy, and transparency. We do not accept paid placements that influence calculator outputs. Inputs and assumptions are shown directly in the interface so you can verify how results are produced.

Methodology

Results use standard formulas and the values you provide. Figures are estimates and may differ from lender quotes. For decisions that require professional guidance, consult a licensed advisor.

Sources

Inputs used by this calculator

  • Mode Encrypt
  • Decrypt
  • Alphabet A–Z (26 letters) Custom alphabet… Custom alphabet Characters must be unique. Alphabet length defines modulus m. Key a (multiplicative) Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25). Key b (additive) Any integer. We automatically reduce a and b modulo m. Options Preserve letter case
  • Keep spaces, punctuation, digits
  • Group ciphertext in blocks of 5
  • Show per-character steps

Consistency checks

Checks: non-negative values, plausible ranges, coherent outputs.

Operational notes

Fill in realistic values and keep units and timeframes consistent.

Key entities: Affine, Cipher, Mode Encrypt, Decrypt, Alphabet A–Z (26 letters) Custom alphabet… Custom alphabet Characters must be unique. Alphabet length defines modulus m. Key a (multiplicative) Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25). Key b (additive) Any integer. We automatically reduce a and b modulo m. Options Preserve letter case, Keep spaces, punctuation, digits, Group ciphertext in blocks of 5, Show per-character steps.

Inputs used by this calculator

  • Mode Encrypt
  • Decrypt
  • Alphabet A–Z (26 letters) Custom alphabet… Custom alphabet Characters must be unique. Alphabet length defines modulus m. Key a (multiplicative) Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25). Key b (additive) Any integer. We automatically reduce a and b modulo m. Options Preserve letter case
  • Keep spaces, punctuation, digits
  • Group ciphertext in blocks of 5
  • Show per-character steps

Consistency checks

Checks: non-negative values, plausible ranges, coherent outputs.


Audit: Complete
Formula (LaTeX) + variables + units
This section shows the formulas used by the calculator engine, plus variable definitions and units.
Formula (extracted LaTeX)
\[','\]
','
Formula (extracted LaTeX)
\[E(x) = (ax + b) \mod m\]
E(x) = (ax + b) \mod m
Formula (extracted LaTeX)
\[D(x) = a^{-1}(x - b) \mod m\]
D(x) = a^{-1}(x - b) \mod m
Formula (extracted LaTeX)
\[E(7) = (5 \times 7 + 8) \mod 26\]
E(7) = (5 \times 7 + 8) \mod 26
Formula (extracted LaTeX)
\[E(7) = (35 + 8) \mod 26\]
E(7) = (35 + 8) \mod 26
Formula (extracted LaTeX)
\[E(7) = 43 \mod 26 = 17\]
E(7) = 43 \mod 26 = 17
Formula (extracted text)
Encoding: $ E(x) = (ax + b) \mod m $ Decoding: $ D(x) = a^{-1}(x - b) \mod m $
Formula (extracted text)
$ E(7) = (5 \times 7 + 8) \mod 26 $ $ E(7) = (35 + 8) \mod 26 $ $ E(7) = 43 \mod 26 = 17 $
Variables and units
  • No variables provided in audit spec.
Sources (authoritative):
Changelog
Version: 0.1.0-draft
Last code update: 2026-01-19
0.1.0-draft · 2026-01-19
  • Initial audit spec draft generated from HTML extraction (review required).
  • Verify formulas match the calculator engine and convert any text-only formulas to LaTeX.
  • Confirm sources are authoritative and relevant to the calculator methodology.
Verified by Ugo Candido on 2026-01-19
Profile · LinkedIn

Affine Cipher Encoder/Decoder

Encrypt or decrypt messages with an affine cipher by setting multiplier and shift keys. Review modular steps per character to validate the mapping.

Affine Cipher Tool

Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25).

Any integer. We automatically reduce a and b modulo m.

Options
m = 26 (A–Z)
Modular inverse & key details
Per-character mapping steps

How the Affine Cipher Works

The Affine Cipher is a classical substitution cipher. Each letter is first mapped to a number, transformed with a linear function modulo the alphabet size, and then mapped back to a letter.

Letter-to-number mapping

For the standard English alphabet:

A → 0, B → 1, C → 2, …, Z → 25

Encryption formula

Let:

  • m = size of the alphabet (26 for A–Z)
  • a, b = integer keys
  • x = numeric value of a plaintext letter
E(x) = (a·x + b) mod m

The result E(x) is then converted back to a letter.

Decryption formula

To decrypt, you must compute the modular inverse of a modulo m, denoted a⁻¹, such that:

a · a⁻¹ ≡ 1 (mod m)

Then, for each ciphertext letter with numeric value y:

D(y) = a⁻¹ · (y − b) mod m

Valid values of a

The cipher is only invertible if a and m are coprime (their greatest common divisor is 1). For the 26-letter alphabet, the valid values of a are:

{1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25}

If you choose an invalid a, this tool will warn you and refuse to decrypt, because no modular inverse exists.

Example: Encrypting “AFFINE CIPHER”

Using keys a = 5, b = 8, m = 26:

  1. Map letters to numbers: A=0, F=5, I=8, N=13, E=4, C=2, P=15, H=7, R=17.
  2. Apply E(x) = (5·x + 8) mod 26 to each letter.
  3. Map the results back to letters.

The tool above shows each step, including the numeric mapping and modular arithmetic, so you can follow the process line by line.

Common Questions

Can I use a custom alphabet?

Yes. Choose “Custom alphabet” and enter any sequence of unique characters. The length of this string becomes the modulus m. This is useful for:

  • Including digits (0–9) or punctuation
  • Working with non-English alphabets
  • Designing puzzle-specific symbol sets

How does this tool handle spaces and punctuation?

By default, spaces, punctuation, and digits are preserved unchanged (they are not encrypted). If you want them to be part of the cipher, include them in a custom alphabet and uncheck “Keep spaces, punctuation, digits”.

Is the Affine Cipher secure?

No. The Affine Cipher is easily broken with frequency analysis or by trying all possible key pairs. It is useful for learning and puzzles, but not for protecting sensitive information.

FAQ

How do I choose good keys a and b?

First pick a value of a that is coprime with the alphabet size m. For A–Z, use one of the 12 valid values listed above. Then choose any integer for b. This tool automatically reduces both a and b modulo m, so you do not need to worry about negative or large values.

What happens if I use an invalid a?

If gcd(a, m) ≠ 1, the mapping is not one-to-one: multiple plaintext letters map to the same ciphertext letter. In that case, decryption is impossible. The calculator detects this and shows an error instead of producing misleading output.

Can I recover the keys from a known plaintext and ciphertext?

In theory, yes: two distinct plaintext–ciphertext letter pairs are enough to solve for a and b. This is one reason the Affine Cipher is weak. This tool focuses on encoding/decoding, but you can use the formulas to solve for a and b manually:

y₁ ≡ a·x₁ + b (mod m)
y₂ ≡ a·x₂ + b (mod m)

Subtract the equations to solve for a, then back-substitute to find b.

About the author

Ugo Candido builds financial tools and educational resources to help readers make better money decisions. He focuses on practical, transparent models that reflect how lenders calculate payments and total cost of ownership.

Contact: info@calcdomain.com

Editorial policy

CalcDomain content is created for educational purposes and is reviewed for clarity, accuracy, and transparency. We do not accept paid placements that influence calculator outputs. Inputs and assumptions are shown directly in the interface so you can verify how results are produced.

Methodology

Results use standard formulas and the values you provide. Figures are estimates and may differ from lender quotes. For decisions that require professional guidance, consult a licensed advisor.

Sources

Inputs used by this calculator

  • Mode Encrypt
  • Decrypt
  • Alphabet A–Z (26 letters) Custom alphabet… Custom alphabet Characters must be unique. Alphabet length defines modulus m. Key a (multiplicative) Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25). Key b (additive) Any integer. We automatically reduce a and b modulo m. Options Preserve letter case
  • Keep spaces, punctuation, digits
  • Group ciphertext in blocks of 5
  • Show per-character steps

Consistency checks

Checks: non-negative values, plausible ranges, coherent outputs.

Operational notes

Fill in realistic values and keep units and timeframes consistent.

Key entities: Affine, Cipher, Mode Encrypt, Decrypt, Alphabet A–Z (26 letters) Custom alphabet… Custom alphabet Characters must be unique. Alphabet length defines modulus m. Key a (multiplicative) Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25). Key b (additive) Any integer. We automatically reduce a and b modulo m. Options Preserve letter case, Keep spaces, punctuation, digits, Group ciphertext in blocks of 5, Show per-character steps.

Inputs used by this calculator

  • Mode Encrypt
  • Decrypt
  • Alphabet A–Z (26 letters) Custom alphabet… Custom alphabet Characters must be unique. Alphabet length defines modulus m. Key a (multiplicative) Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25). Key b (additive) Any integer. We automatically reduce a and b modulo m. Options Preserve letter case
  • Keep spaces, punctuation, digits
  • Group ciphertext in blocks of 5
  • Show per-character steps

Consistency checks

Checks: non-negative values, plausible ranges, coherent outputs.


Audit: Complete
Formula (LaTeX) + variables + units
This section shows the formulas used by the calculator engine, plus variable definitions and units.
Formula (extracted LaTeX)
\[','\]
','
Formula (extracted LaTeX)
\[E(x) = (ax + b) \mod m\]
E(x) = (ax + b) \mod m
Formula (extracted LaTeX)
\[D(x) = a^{-1}(x - b) \mod m\]
D(x) = a^{-1}(x - b) \mod m
Formula (extracted LaTeX)
\[E(7) = (5 \times 7 + 8) \mod 26\]
E(7) = (5 \times 7 + 8) \mod 26
Formula (extracted LaTeX)
\[E(7) = (35 + 8) \mod 26\]
E(7) = (35 + 8) \mod 26
Formula (extracted LaTeX)
\[E(7) = 43 \mod 26 = 17\]
E(7) = 43 \mod 26 = 17
Formula (extracted text)
Encoding: $ E(x) = (ax + b) \mod m $ Decoding: $ D(x) = a^{-1}(x - b) \mod m $
Formula (extracted text)
$ E(7) = (5 \times 7 + 8) \mod 26 $ $ E(7) = (35 + 8) \mod 26 $ $ E(7) = 43 \mod 26 = 17 $
Variables and units
  • No variables provided in audit spec.
Sources (authoritative):
Changelog
Version: 0.1.0-draft
Last code update: 2026-01-19
0.1.0-draft · 2026-01-19
  • Initial audit spec draft generated from HTML extraction (review required).
  • Verify formulas match the calculator engine and convert any text-only formulas to LaTeX.
  • Confirm sources are authoritative and relevant to the calculator methodology.
Verified by Ugo Candido on 2026-01-19
Profile · LinkedIn

Affine Cipher Encoder/Decoder

Encrypt or decrypt messages with an affine cipher by setting multiplier and shift keys. Review modular steps per character to validate the mapping.

Affine Cipher Tool

Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25).

Any integer. We automatically reduce a and b modulo m.

Options
m = 26 (A–Z)
Modular inverse & key details
Per-character mapping steps

How the Affine Cipher Works

The Affine Cipher is a classical substitution cipher. Each letter is first mapped to a number, transformed with a linear function modulo the alphabet size, and then mapped back to a letter.

Letter-to-number mapping

For the standard English alphabet:

A → 0, B → 1, C → 2, …, Z → 25

Encryption formula

Let:

  • m = size of the alphabet (26 for A–Z)
  • a, b = integer keys
  • x = numeric value of a plaintext letter
E(x) = (a·x + b) mod m

The result E(x) is then converted back to a letter.

Decryption formula

To decrypt, you must compute the modular inverse of a modulo m, denoted a⁻¹, such that:

a · a⁻¹ ≡ 1 (mod m)

Then, for each ciphertext letter with numeric value y:

D(y) = a⁻¹ · (y − b) mod m

Valid values of a

The cipher is only invertible if a and m are coprime (their greatest common divisor is 1). For the 26-letter alphabet, the valid values of a are:

{1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25}

If you choose an invalid a, this tool will warn you and refuse to decrypt, because no modular inverse exists.

Example: Encrypting “AFFINE CIPHER”

Using keys a = 5, b = 8, m = 26:

  1. Map letters to numbers: A=0, F=5, I=8, N=13, E=4, C=2, P=15, H=7, R=17.
  2. Apply E(x) = (5·x + 8) mod 26 to each letter.
  3. Map the results back to letters.

The tool above shows each step, including the numeric mapping and modular arithmetic, so you can follow the process line by line.

Common Questions

Can I use a custom alphabet?

Yes. Choose “Custom alphabet” and enter any sequence of unique characters. The length of this string becomes the modulus m. This is useful for:

  • Including digits (0–9) or punctuation
  • Working with non-English alphabets
  • Designing puzzle-specific symbol sets

How does this tool handle spaces and punctuation?

By default, spaces, punctuation, and digits are preserved unchanged (they are not encrypted). If you want them to be part of the cipher, include them in a custom alphabet and uncheck “Keep spaces, punctuation, digits”.

Is the Affine Cipher secure?

No. The Affine Cipher is easily broken with frequency analysis or by trying all possible key pairs. It is useful for learning and puzzles, but not for protecting sensitive information.

FAQ

How do I choose good keys a and b?

First pick a value of a that is coprime with the alphabet size m. For A–Z, use one of the 12 valid values listed above. Then choose any integer for b. This tool automatically reduces both a and b modulo m, so you do not need to worry about negative or large values.

What happens if I use an invalid a?

If gcd(a, m) ≠ 1, the mapping is not one-to-one: multiple plaintext letters map to the same ciphertext letter. In that case, decryption is impossible. The calculator detects this and shows an error instead of producing misleading output.

Can I recover the keys from a known plaintext and ciphertext?

In theory, yes: two distinct plaintext–ciphertext letter pairs are enough to solve for a and b. This is one reason the Affine Cipher is weak. This tool focuses on encoding/decoding, but you can use the formulas to solve for a and b manually:

y₁ ≡ a·x₁ + b (mod m)
y₂ ≡ a·x₂ + b (mod m)

Subtract the equations to solve for a, then back-substitute to find b.

About the author

Ugo Candido builds financial tools and educational resources to help readers make better money decisions. He focuses on practical, transparent models that reflect how lenders calculate payments and total cost of ownership.

Contact: info@calcdomain.com

Editorial policy

CalcDomain content is created for educational purposes and is reviewed for clarity, accuracy, and transparency. We do not accept paid placements that influence calculator outputs. Inputs and assumptions are shown directly in the interface so you can verify how results are produced.

Methodology

Results use standard formulas and the values you provide. Figures are estimates and may differ from lender quotes. For decisions that require professional guidance, consult a licensed advisor.

Sources

Inputs used by this calculator

  • Mode Encrypt
  • Decrypt
  • Alphabet A–Z (26 letters) Custom alphabet… Custom alphabet Characters must be unique. Alphabet length defines modulus m. Key a (multiplicative) Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25). Key b (additive) Any integer. We automatically reduce a and b modulo m. Options Preserve letter case
  • Keep spaces, punctuation, digits
  • Group ciphertext in blocks of 5
  • Show per-character steps

Consistency checks

Checks: non-negative values, plausible ranges, coherent outputs.

Operational notes

Fill in realistic values and keep units and timeframes consistent.

Key entities: Affine, Cipher, Mode Encrypt, Decrypt, Alphabet A–Z (26 letters) Custom alphabet… Custom alphabet Characters must be unique. Alphabet length defines modulus m. Key a (multiplicative) Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25). Key b (additive) Any integer. We automatically reduce a and b modulo m. Options Preserve letter case, Keep spaces, punctuation, digits, Group ciphertext in blocks of 5, Show per-character steps.

Inputs used by this calculator

  • Mode Encrypt
  • Decrypt
  • Alphabet A–Z (26 letters) Custom alphabet… Custom alphabet Characters must be unique. Alphabet length defines modulus m. Key a (multiplicative) Must be coprime with m (e.g. for m = 26, valid a: 1,3,5,7,9,11,15,17,19,21,23,25). Key b (additive) Any integer. We automatically reduce a and b modulo m. Options Preserve letter case
  • Keep spaces, punctuation, digits
  • Group ciphertext in blocks of 5
  • Show per-character steps

Consistency checks

Checks: non-negative values, plausible ranges, coherent outputs.


Audit: Complete
Formula (LaTeX) + variables + units
This section shows the formulas used by the calculator engine, plus variable definitions and units.
Formula (extracted LaTeX)
\[','\]
','
Formula (extracted LaTeX)
\[E(x) = (ax + b) \mod m\]
E(x) = (ax + b) \mod m
Formula (extracted LaTeX)
\[D(x) = a^{-1}(x - b) \mod m\]
D(x) = a^{-1}(x - b) \mod m
Formula (extracted LaTeX)
\[E(7) = (5 \times 7 + 8) \mod 26\]
E(7) = (5 \times 7 + 8) \mod 26
Formula (extracted LaTeX)
\[E(7) = (35 + 8) \mod 26\]
E(7) = (35 + 8) \mod 26
Formula (extracted LaTeX)
\[E(7) = 43 \mod 26 = 17\]
E(7) = 43 \mod 26 = 17
Formula (extracted text)
Encoding: $ E(x) = (ax + b) \mod m $ Decoding: $ D(x) = a^{-1}(x - b) \mod m $
Formula (extracted text)
$ E(7) = (5 \times 7 + 8) \mod 26 $ $ E(7) = (35 + 8) \mod 26 $ $ E(7) = 43 \mod 26 = 17 $
Variables and units
  • No variables provided in audit spec.
Sources (authoritative):
Changelog
Version: 0.1.0-draft
Last code update: 2026-01-19
0.1.0-draft · 2026-01-19
  • Initial audit spec draft generated from HTML extraction (review required).
  • Verify formulas match the calculator engine and convert any text-only formulas to LaTeX.
  • Confirm sources are authoritative and relevant to the calculator methodology.
Verified by Ugo Candido on 2026-01-19
Profile · LinkedIn
Formulas

(Formulas preserved from original page content, if present.)

Version 0.1.0-draft
Citations

Add authoritative sources relevant to this calculator (standards bodies, manuals, official docs).

Changelog
  • 0.1.0-draft — 2026-01-19: Initial draft (review required).