 To produce an affine cipher mod n, we'll choose a and b, where the greatest common divisor of a and n is equal to 1. We'll encrypt our ciphertext x using y congruent to ax plus b, and we'll decrypt by solving c congruent to ax plus b. For example, suppose Alice constructs an affine cipher mod 26 using a equals 7, b equals 11, and she sends the encrypted value 13 to Bob, or to decrypt it and find the original message value. So we know that 13 is congruent to 7x plus 11 mod 26. So we can add or subtract both sides of a congruent, so let's start off by subtracting 11 from both sides, yielding 2 congruent to 7x. Now to solve this, we need to find the multiplicative inverse of 7 mod 26, which turns out to be 15. Multiplying both sides by 15 gives, which recovers our original message, x equal to 4. So how good are affine ciphers? One way to measure the strength of a cryptographic system is to determine its key space, and so we ask, find the key space of an affine cipher. So for example, let's find the key space of an affine cipher mod 26. So for y congruent to ax plus b, b can be any one of 26 values from 0 to 25, and that's because we can always subtract b to try and recover x. On the other hand, since we need to find the multiplicative inverse of a, a must be relatively prime to 26, which is 2 times 13, which means that there are 13 values a can't be the even numbers, and there's one odd value that a can't be, namely 13. And so there are 26 minus 13 minus 1, 12 values, and so by our fundamental counting principle, there are 12 times 26 312 possible keys for an affine cipher. And that makes for a very small key space, so what can we do? The easiest thing to do is we can expand the key space by working with a larger modulus. For example, if we form blocks of 5 letters and use 2 digit codes for each letter, then we'd perform encryption on 10 digit numbers, like 0805121215, and then we'd be able to work mod 10 billion. This would give us a much larger key space and make our affine ciphers much more secure, right? To answer that question, we'll turn to the other side of cryptography, which is cryptanalysis and breaking a cryptographic system. So remember, you should always assume your adversary knows the method of encryption, so that if someone is trying to break an affine cipher, they know that it is an affine cipher. So the adversary knows that we're using an affine cipher, but they don't know the values of a and b. So breaking an affine cipher could be accomplished by finding the coefficients a and b. Since there are two unknowns, we need to find two plain-text ciphertext pairs. Suppose Alice is using an affine cipher mod 101, and Eve determines f of 3 is 8, and f of 17 is 4. Let's see if we can determine f. Since Eve has a plain-text ciphertext pair, Eve's information corresponds to knowing that the affine cipher encrypts 3 as 8. In other words, 8 is congruent to a times 3 plus b. And Eve also knows the affine cipher encrypts 17 as 4. And so she knows that 4 is a times 17 plus b. And so now she wants to solve the system for a and b. And the thing to remember is that you can add, subtract, or multiply, but never divide in a congruence. So our first step might be to subtract. We have 4 congruent to 17 a plus b and 8 congruent to 3 a plus b. So if we subtract the one from the other, we get. And now we want to solve this congruence. Since we know that a is equal to 43, then we can use either of the congruences to find b. So I can use the first. 8 is congruent to a times 3 plus b. We'll replace and solve. And since we now know a and b, we have broken this particular affine cipher. Now you'll notice it was pretty easy for Eve to break the affine cipher. And so the natural question to ask is, if an affine cipher is so easy to break, why bother with it? And the answer to that is to remember that a strong cryptographic system uses several different encryption methods. The affine cipher is just one of several you should put together to form a good cryptographic system.