Affine Cipher Tool
Plaintext
Ciphertext
What is the Affine Cipher?
The Affine Cipher is a classic symmetric encryption method and a variant of substitution ciphers. It encrypts letters using a mathematical formula. The core idea behind the Affine Cipher is to map each letter to another using mathematical operations. The encryption process is based on a linear equation, where the numerical value of each letter is combined with two constants (the slope and the intercept) to generate a new letter.
The encryption formula for the Affine Cipher is:
Where:
- x is the numerical representation of the plaintext letter (e.g., A=0, B=1, C=2, etc.).
- a is the slope (one of the encryption keys).
- b is the intercept (the other encryption key).
- m is the length of the alphabet (e.g., for the English alphabet, m=26m = 26).
The decryption formula for the Affine Cipher is:
Where a-1 is the modular inverse of a, meaning it is the number that is coprime with a and satisfies the equation a × a-1 ≡ 1 mod m.
How Does the Affine Cipher Encrypt?
- Choose the Alphabet: You can use a standard alphabet (e.g., "ABCDEFGHIJKLMNOPQRSTUVWXYZ") or a custom alphabet.
- Set the Slope (a) and Intercept (b): Choose a slope aa that is coprime with the alphabet length mm (i.e., the greatest common divisor of aa and mm should be 1). Choose an intercept bb as a constant for the encryption process.
- Convert the Plaintext to Numbers: Convert each letter in the plaintext to its corresponding number in the alphabet. For example, A=0, B=1, C=2, ..., Z=25.
- Apply the Encryption Formula: For each number x, use the formula E(x) = (ax + b) mod m to encrypt.
- Generate the Ciphertext: Convert the encrypted numbers back into letters to get the ciphertext.
Example
Assuming the standard English alphabet (A-Z, length 26), let's choose the slope a = 5 and intercept b = 8 to encrypt the plaintext "HELLO".
1. Convert the plaintext to numbers:
- H = 7
- E = 4
- L = 11
- L = 11
- O = 14
2. Apply the encryption formula:
- H: E(7) = (5 × 7 + 8) mod 26 = 17, which corresponds to "R".
- E: E(4) = (5 × 4 + 8) mod 26 = 2, which corresponds to "C".
- L: E(11) = (5 × 11 + 8) mod 26 = 11, which corresponds to "L".
- L: E(11) = (5 × 11 + 8) mod 26 = 11, which corresponds to "L".
- O: E(14) = (5 × 14 + 8) mod 26 = 0, which corresponds to "A".
Thus, the ciphertext is: RCLLA.
How Does the Affine Cipher Decrypt?
- Calculate the Modular Inverse of the Slope: To decrypt, calculate the modular inverse a-1 of the slope a under modulus m. The modular inverse is the number that satisfies a × a-1 ≡ 1 mod m.
- Apply the Decryption Formula: For each number x of the ciphertext, use the decryption formula D(x) = a-1(x - b) mod m to decrypt.
Example
Assume the ciphertext is "RCLLA", with the slope a = 5 and intercept b = 8. Let's decrypt it.
1. Calculate the Modular Inverse:
Slope a = 5. We need to find a-1 such that 5 × a-1 ≡ 1 mod 26.
Through calculation, a-1 = 21 because 5 × 21 = 105 ≡ 1 mod 26.
2. Convert the Ciphertext to Numbers:
- R = 17
- C = 2
- L = 11
- L = 11
- A = 0
3. Apply the Decryption Formula:
- R: D(17) = 21 × (17 - 8) mod 26 = 7, which corresponds to "H".
- C: D(2) = 21 × (2 - 8) mod 26 = 4, which corresponds to "E".
- L: D(11) = 21 × (11 - 8) mod 26 = 11, which corresponds to "L".
- L: D(11) = 21 × (11 - 8) mod 26 = 11, which corresponds to "L".
- A: D(0) = 21 × (0 - 8) mod 26 = 14, which corresponds to "O".
Thus, the plaintext corresponding to the ciphertext "RCLLA" is: HELLO.
How to Use the Affine Cipher Tool?
- Input Plaintext or Ciphertext: Depending on whether you want to encrypt or decrypt, enter the plaintext or ciphertext into the appropriate input box.
- Set the Slope and Intercept: Set the slope aa and intercept bb, ensuring that the slope is coprime with the alphabet length to avoid errors.
- Customize the Alphabet: You can input a custom alphabet, such as uppercase letters, lowercase letters, numbers, or a multilingual character set.
- Ignore Unknown Characters: During encryption or decryption, you can choose whether to ignore unknown characters (such as spaces, punctuation, etc.). If enabled, these characters will not affect the encryption or decryption process.
- Copy and Download Results: After encryption or decryption, you can copy the results directly or save them to a file for later use using the download option.