How to use the affine cipher
- Enter your plaintext or ciphertext in the Text field.
- Select Encode or Decode from the direction dropdown.
- Set key a to a number coprime to 26 (such as 5, 7, or 11).
- Set key b to any integer between 0 and 25.
- Read the result instantly below the form.
How the affine cipher works
The affine cipher maps each letter to a number from 0 to 25, applies a linear function, and converts back to a letter. The encryption formula is E(x) = (a * x + b) mod 26, where x is the numeric value of the plaintext letter.
For example, encoding the word AFFINE with a=5 and b=8:
- A (0): (5 * 0 + 8) mod 26 = 8, which is I
- F (5): (5 * 5 + 8) mod 26 = 33 mod 26 = 7, which is H
- F (5): same as above, gives H
- I (8): (5 * 8 + 8) mod 26 = 48 mod 26 = 22, which is W
- N (13): (5 * 13 + 8) mod 26 = 73 mod 26 = 21, which is V
- E (4): (5 * 4 + 8) mod 26 = 28 mod 26 = 2, which is C
The result is IHHWVC. This affine cipher calculator performs these steps automatically for any input.
Decryption and the modular inverse
To reverse the process, you need the modular multiplicative inverse of a modulo 26. This is a number a_inv such that (a * a_inv) mod 26 = 1. The decryption formula is D(y) = a_inv * (y - b) mod 26.
For a=5, the inverse is 21 because (5 * 21) mod 26 = 105 mod 26 = 1. Decoding I (8) with b=8: 21 * (8 - 8) mod 26 = 0, which is A. Every valid key a has exactly one inverse in the range 1 to 25.
If you use an invalid a value like 2 or 13, no inverse exists and the affine cipher cannot decrypt uniquely. That is why the tool validates your input before processing.
Valid keys reference table
Key a must be coprime to 26, meaning the greatest common divisor of a and 26 is 1. Since 26 factors into 2 and 13, any even number or multiple of 13 is invalid. Key b can be any integer from 0 to 25.
| Key a | Inverse (a_inv) |
|---|---|
| 1 | 1 |
| 3 | 9 |
| 5 | 21 |
| 7 | 15 |
| 9 | 3 |
| 11 | 19 |
| 15 | 7 |
| 17 | 23 |
| 19 | 11 |
| 21 | 5 |
| 23 | 17 |
| 25 | 25 |
There are 12 valid choices for a and 26 choices for b, giving 312 possible keys. Subtracting the 26 trivial cases where a=1 leaves 286 non-trivial keys, which makes the affine cipher slightly stronger than a simple shift but still vulnerable to brute force.
Relationship to other ciphers
The affine cipher generalizes several simpler substitution methods. When a=1, the formula reduces to E(x) = (x + b) mod 26, which is exactly the shift used by the caesar cipher translator. When b=0, it becomes a pure multiplicative cipher.
Unlike polyalphabetic systems such as the vigenere cipher, the affine cipher uses the same key for every letter, making it a monoalphabetic substitution. Frequency analysis can break it quickly if the ciphertext is long enough. For more complex historical methods, explore the playfair cipher or the rail fence cipher, which rearrange letters rather than substituting them mathematically.
Handling non-letter characters
This affine cipher decoder preserves all characters that are not English letters. Spaces, punctuation marks, digits, and symbols pass through unchanged. Uppercase letters remain uppercase and lowercase letters remain lowercase after transformation.
This behavior keeps the structure of sentences readable while only obscuring the alphabetic content. If you need a cipher that encodes spaces or numbers as well, consider combining this tool with a base64 encoding step first, or look at binary approaches via binary to text conversion.
Worked examples
| Example | Input | Result |
|---|---|---|
| Encode AFFINE | Text: AFFINE; Direction: encode; Key a (must be coprime to 26): 5; Key b (shift): 8 | IHHWVC |
| Decode IHHWVC | Text: IHHWVC; Direction: decode; Key a (must be coprime to 26): 5; Key b (shift): 8 | AFFINE |
| Encode hello world | Text: hello world; Direction: encode; Key a (must be coprime to 26): 7; Key b (shift): 3 | afccx bxscy |
FAQ
What is an affine cipher?
An affine cipher is a monoalphabetic substitution cipher that encrypts each letter using the formula E(x) = (a * x + b) mod 26, where a and b are the secret keys.
Why must key a be coprime to 26?
If a shares a factor with 26, multiple plaintext letters map to the same ciphertext letter, making unique decryption impossible. Coprimality guarantees a modular inverse exists.
How do I find the modular inverse of a?
Find a number a_inv between 1 and 25 such that (a * a_inv) mod 26 equals 1. For example, the inverse of 5 is 21 because 5 times 21 is 105, and 105 mod 26 is 1.
Is the affine cipher secure?
No. With only 312 possible keys, a computer can test all combinations instantly. Even by hand, frequency analysis breaks it easily on texts longer than a few sentences.
Does this affine cipher calculator handle lowercase letters?
Yes. Lowercase letters are encrypted or decrypted using the same formula and remain lowercase in the output. Non-alphabetic characters are left untouched.
What happens if I enter an invalid key?
The tool checks whether a is coprime to 26 and whether both keys fall within the valid range. If not, it displays an error message instead of producing incorrect output.
Can I use this as an affine cipher decoder for homework?
Yes. Enter the ciphertext, select Decode, provide the keys given in your assignment, and the plaintext appears immediately. All computation stays in your browser.