ArticleslgStudy

science

Affine cipher

Affine cipher is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Affine cipher rather than just read about it. In short: The affine cipher is a type of monoalphabetic substitution cipher, where each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard substitution cipher with a rule governing which letter goes to which.

Key takeaways

  • Affine cipher belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Affine cipher to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Affine cipher from memory before moving on to harder problems.

Reference excerpt

The affine cipher is a type of monoalphabetic substitution cipher, where each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard substitution cipher with a rule governing which letter goes to which. As such, it has the weaknesses of all substitution ciphers. Each letter is enciphered with the function (ax + b) mod 26, where b is the magnitude of the shift.

Description Here, the letters of an alphabet of size m are first mapped to the integers in the range 0 ... m − 1. It then uses modular arithmetic to transform the integer that each plaintext letter corresponds to into another integer that correspond to a ciphertext letter. The encryption function for a single letter is

E(x)=(ax+b)modm where modulus m is the size of the alphabet and a and b are the keys of the cipher. The value a must be chosen such that a and m are coprime. The decryption function is

D(x)=a−1(x−b)modm where a−1 is the modular multiplicative inverse of a modulo m. I.e., it satisfies the equation

1=aa−1modm The multiplicative inverse of a only exists if a and m are coprime. Hence without the restriction on a, decryption might not be possible. It can be shown as follows that decryption function is the inverse of the encryption function,

D(E(x))=a−1(E(x)−b)modm=a−1(((ax+b)modm)−b)modm=a−1(ax+b−b)modm=a−1axmodm=xmodm

Weaknesses Since the affine cipher is still a monoalphabetic substitution cipher, it inherits the weaknesses of that class of ciphers. The Caesar cipher is an Affine cipher with a = 1 since the encrypting function simply reduces to a linear shift. The Atbash cipher uses a = −1. Considering the specific case of encrypting messages in English (i.e. m = 26), there are a total of 286 non-trivial affine ciphers, not counting the 26 trivial Caesar ciphers. This number comes from the fact there are 12 numbers that are coprime with 26 that are less than 26 (these are the possible values of a). Each value of a can have 26 different addition shifts (the b value); therefore, there are 12 × 26 or 312 possible keys. The 12 coprime numbers are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. This lack of variety renders the system as highly insecure when considered in light of Kerckhoffs' Principle. The cipher's primary weakness comes from the fact that if the cryptanalyst can discover (by means of frequency analysis, brute force, guessing or otherwise) the plaintext of two ciphertext characters then the key can be obtained by solving a simultaneous equation. Since we know a and m are relatively prime this can be used to rapidly discard many "false" keys in an automated system. The same type of transformation used in affine ciphers is used in linear congruential generators, a type of pseudorandom number generator. This generator is not a cryptographically secure pseudorandom number generator for the same reason that the affine cipher is not secure.

Example In this example showing encryption and decryption, the alphabet is going to be the letters A through Z, and will have the corresponding values found in the following table.

Encryption In this encrypting example, the plaintext to be encrypted is "AFFINE CIPHER" using the table mentioned above for the numeric values of each letter, taking a to be 5, b to be 8, and m to be 26 since there are 26 characters in the alphabet being used. Only the value of a has a restriction since it has to be coprime with 26. The possible values that a could be are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. The value for b can be arbitrary as long as a does not equal 1 since this is the shift of the cipher. Thus, the encryption function for this example will be y = E(x) = (5x + 8) mod 26. The first step in encrypting the message is to write the numeric values of each letter.

Now, take each value of x, and solve the first part of the equation, (5x + 8). After finding the value of (5x + 8) for each character, take the remainder when dividing the result of (5x + 8) by 26. The following table shows the first four steps of the encrypting process.

The final step in encrypting the message is to look up each numeric value in the table for the corresponding letters. In this example, the encrypted text would be IHHWVCSWFRCP. The table below shows the completed table for encrypting a message in the Affine cipher.

Decryption In this decryption example, the ciphertext that will be decrypted is the ciphertext from the encryption example. The corresponding decryption function is D(y) = 21(y − b) mod 26, where a−1 is calculated to be 21, and b is 8. To begin, write the numeric equivalents to each letter in the ciphertext, as shown in the table below.

Now, the next step is to compute 21(y − 8), and then take the remainder when that result is divided by 26. The following table shows the results of both computations.

The final step in decrypting the ciphertext is to use the table to convert numeric values back into letters. The plaintext in this decryption is AFFINECIPHER. Below is the table with the final step completed.

Entire alphabet encoded To make encrypting and decrypting quicker, the entire alphabet can be encrypted to create a one-to-one map between the letters of the cleartext and the ciphertext. In this example, the one-to-one map would be the following:

Programming examples The following Python code encrypts text with the affine cipher:

See also Affine functions Atbash code Caesar cipher ROT13 Topics in cryptography

References

Worked examples

Example 1 — a first encounter with Affine cipher

Start with the simplest possible case. Write down what Affine cipher claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Affine cipher before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Affine cipher ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Affine cipher

In research
Affine cipher appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Affine cipher in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Affine cipher is common in secondary-school and first-year university syllabi. It links to neighbouring topics Classical ciphers, so understanding it makes those chapters shorter.
In everyday life
Look for Affine cipher outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Affine cipher” →

How to study Affine cipher in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Affine cipher means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Affine cipher out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Affine cipher in simple terms?

The affine cipher is a type of monoalphabetic substitution cipher, where each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back aga…

Why does Affine cipher matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Affine cipher?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Affine cipher.

Tags

  • Classical ciphers

Keep exploring