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:

E(x) = (ax + b) mod  m

Where:

The decryption formula for the Affine Cipher is:

D(x) = a-1(x - b) mod m

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?

  1. Choose the Alphabet: You can use a standard alphabet (e.g., "ABCDEFGHIJKLMNOPQRSTUVWXYZ") or a custom alphabet.
  2. 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.
  3. 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.
  4. Apply the Encryption Formula: For each number x, use the formula E(x) = (ax + b) mod  m to encrypt.
  5. 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:

2. Apply the encryption formula:

Thus, the ciphertext is: RCLLA.

How Does the Affine Cipher Decrypt?

  1. 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.
  2. 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:

3. Apply the Decryption Formula:

Thus, the plaintext corresponding to the ciphertext "RCLLA" is: HELLO.

How to Use the Affine Cipher Tool?

  1. Input Plaintext or Ciphertext: Depending on whether you want to encrypt or decrypt, enter the plaintext or ciphertext into the appropriate input box.
  2. 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.
  3. Customize the Alphabet: You can input a custom alphabet, such as uppercase letters, lowercase letters, numbers, or a multilingual character set.
  4. 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.
  5. 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.

Share This Page