 So for our second and final application of modular arithmetic, we're going to turn to cryptography, which is the study of making and breaking secret codes or more formally it's how to communicate in the presence of an adversary without giving up your information. So what we're going to describe here is how to use modular arithmetic to create a barrier. So remember that z29 here is the set of all equivalence classes created by the equivalence relation we get on the integers by setting a equivalent to b if and only if a is congruent to b modulo 29 as integers. So we've seen before that's an equivalence relation and it creates 29 distinct equivalence classes, namely these guys in here. And we've also spent a great deal of time discussing how to do arithmetic on these equivalence classes. That's what modular arithmetic is. We can define our O plus and O dot operations as usual. If I want to add quote unquote add to classes together using the O plus operation, I'm going to form the equivalence class of the regular addition of their representatives and likewise for multiplication. So keep that set in mind and we're going to be using it and keep those operations in mind we're going to be using those. So how would I encrypt a message under this so called affine cipher? Well first of all I'm going to add a little bit to the regular alphabet in English. We all know that the English alphabet consists of 26 letters and we're going to throw in three more one for the period, the question mark and the exclamation point. That'll get us up to a total of 29 characters. So here's how I will encrypt a message. First of all if I want to send a message under this system both myself and the person I'm sending it to we have to decide on two integers, one called m and the other called b and they need to be between 1 and 28 inclusively. And so what I'm going to do here is if I'm sending a message it's going to consist of letters and possibly the exclamation point, question mark and period. I'm going to go through each of the characters one by one and make replacements. I'm going to replace each character with the equivalence class in its corresponding position in z29. That means a is going to be replaced by the class of zero, a is the first letter in the alphabet, zero is the first equivalence class in z29, b will be replaced by the class of one, c replaced by the class of two and so on. All the way up through z that's the 26th letter in the alphabet and the class of 25 is the 26th element going in numerical order of z29. And then I have three more. I'm going to replace the period with class of 26, the question mark with class of 27 and the exclamation point with the class of 28. So I can go through my entire message and turn it into a string of equivalence classes rather than a string of letters. So I'm first of all going to do that. And then what I'm going to do is I'm going to go through each of the equivalence classes that's now in my list or my string and I'm going to make really two computations here. I'm going to take the equivalence class that I've converted from letters and o dot multiply it to m. That was the class of m. That was the integer that I chose at the beginning of this process. So m o dot x, that will give me another equivalence class which I'm then going to add using the o plus operation to the class of b. So the reason this is called an affine ciphers because the word affine in mathematics is often used to denote things having to do with straight lines. And as you can see, this almost looks like your y equals m x plus b point or slope intercept form for an equation of a line in the real numbers. And that's kind of what we're doing here. So I'm taking each character in the message converting it to an equivalence class and performing this modular arithmetic mod 29 operation upon it. That's going to give me a string of new equivalence classes, all of which are either the or somewhere in z 29, either the class of zero, class of one, all the way up to the class of 28. And so that means I can convert the list back to characters kind of going backwards through my replacement process. That list of characters is the encrypted message. So let's see how this works in real life. I'm going to encrypt the word help with an exclamation point on this. This is actually a five character message. And let's suppose I've chosen m equals three b equals 11. So what I need to do now is convert each of these letters into equivalence classes in z 29. Now h is the eighth letter of the alphabet. So that's going to convert into the class of seven in z 29. Likewise, e being the fifth letter in the alphabet will convert to the class of four in z 29. And likewise, we can go down through here and do L and P and the exclamation point, which is the very, very last letter in our extended alphabet. So it gets the class of 28. So what we're going to do next is now that I've converted my letters into five equivalence classes is use my m and my b appear to perform the modular arithmetic operations. I'm going to take my m, which is the class of three, and o dot multiply it to my character here, which is seven, and then o plus that to 11. Order of operations prevails here. So I'm going to do the multiplication first. Let's just continue through this entire line. And before we do the rest. So three, o dot seven, that is, we're just going to regular multiply the insides of those to get the class of 21 and then o plus this with 11. And to get the o plus, we're going to regular add the insides here to get 21 plus 11. That's 32. Now the class of 32, this is in z 29. So the class of 32 can be reduced down to a class that is in z 29. So we have to ask ourselves, what is 32 equivalent to mod 29? And that would be three in this case, 32 and three are equivalent to each other. So this is the class of three. Now let's just quickly run through. I'll quickly run through and do the rest of these. So the class of three, o dot the class of four, o plus 11. Well, the operation in parentheses would give me a 12, o plus 11. And that gives me a class of 23. Now the class of 23 is already in z 29, so it doesn't reduce down to anything further. It's just the class of 23 itself. For the 11 here, I have three, o times 11 plus o plus 11. And this gives me the multiplication process gives me 33, o plus 11. And that is the class of 44. And we have to ask ourselves something similar to what we did up here, what is 44 congruent to mod 29? And the answer to that is congruent to 15 mod 29. So this is the class of 15, because 15 and 44 are equivalent to each other under the congruence mod nine relationship. Likewise, the last two here, if I have three, o dot with 15, o plus 11. I will get 45, o plus 11. And that's equal to the class of 56. And you can check that's the same as the class of 27. And finally, the exclamation point, what does it encrypt to that is three, o dot with 28, o plus with 11. And that is 28 times three is 84 plus 11. That gives me the class of 95. And 95 is the same thing as eight, mod 29. So what do we have here, we have the classes three, 23, 15, 27 and 28. Now what we have to do next, the final step in the encryption process is to convert all of these things back into letters. And you can do that by just going to our chart that we had a couple of slides ago down here in the bottom, or just counting on your fingers in the alphabet. And what we're going to end up with here is the following message. I'll put this in red down at the bottom. The three converts to a D. The 23 is the letter X. The 15 is the letter P. The 27 is a punctuation mark, it's the question mark. And then eight is the letter I. So here is our encrypted form of the word help DXP question mark I. And that's the the ciphertext of this word help with the exclamation point. And that's what I send. Now someone intercepting this in the middle, who might have some malicious purposes for using my information, we'll look at this and just see just five random characters, it doesn't seem to be any information contained in this whatsoever. And that's exactly what should happen in a good crypto system. Now let's talk about decrypting this. So this is going to go to an intended recipient and that person needs to be able to decrypt the message to find out what I was trying to say. How is that going to work? Well, before we do that, we need to take a quick look at the operations of multiplication and addition mod 29. Here's an addition table for Z 29. This is the O plus operation in Z 29. I just want to point out that every number in Z 29, every equivalence class in Z 29 has a corresponding equivalence class such that when you add it to that class, you get zero. Again, for example, if I look at the class of 13, what do I add to 13 to give me zero? Well, apparently 13 adds to 16 to give me zero. So 13 O plus 16, that would be the class of 29, and that is the class of zero. Now likewise in multiplication, here's what it looks like. It's kind of an interesting little swirly pattern here. You can see that if you check through this, take some time to check through it, you can see that every column, no matter what you pick like 13 or 20, there's going to be some row where you have an entry of one, like 13 and nine. You cross reference those as an entry of one, 20 and 16, you have an entry of one. So no matter what number I might have picked, say for example, I pick the number 21 and look at its equivalence class, there's going to be some number out there, some equivalence class out there such that when I o dot that equivalence class with 21, I get the number one. It's almost like multiplying by a fraction in some ways. There are no fractions on the board here. So what number is that for 21? See, there's the column for 21, and there's the one, and that corresponded to 18. So apparently, if I take the class of 21 and o dot it with the class of 18, I get the class of one. So that's important because we need both of those facts for the decryption process, which I'm going to show you now. So to decrypt this message we get, the recipient has received a string of characters. It was DXP, question mark I in our example. So the recipient's going to first of all convert each of those characters back into equivalence classes as we've done before. For example, the D, that's the fourth letter in the alphabet. So the D would convert to the class of three. Okay, so now what he's going to do, what the recipient is going to do is two things. First of all, we're going to find the class C such that B o plus C equals zero. And we just saw that that always works in this extended alphabet. And we're also going to find the class N such that M o dot N is equal to one. Remember M and B were the numbers that the sender and the recipient agreed upon before this all went down here. And we also saw in the multiplication table that no matter what M we might have chosen, there will always be a corresponding N here such that M o dot N equals one. So what we're going to do is find those two equivalence classes C and N. And for each class that I received in the message, for example the class of three that I received that corresponded to the letter D, I'm going to compute two things. The recipient is going to compute two things. First of all, I'm going to take that class and o plus it with C. And then that's going to give me some new class. I'm going to take the result of that calculation and o dot it with N. And it's magically, not really magically, but it looks like magic. This will result in the plain text, the original message that was unencrypted. Now, you might not believe me on this, so let's take a look and see how it worked. So here was the received message that we got in the first example. That was help with an exclamation point and encrypted to this string here. So we're going to first go through and convert all of these into numbers or into the equivalence classes. So the D converts to three, the X was 23, the P was 15, the question mark was 27, and the I was an 8. All right? So we knew the recipient knows that M was equal to 3 and that B was equal to 11. So the recipient can go look this up in the table that the N, the equivalence class that multiplies to M to give me 1, is equal to 10. Now likewise, I can go and find the corresponding class C that adds to B to give me 0, and that will be 18 this time. And again, so here comes the calculation that gives me the decryption. And we'll do this for D and quickly run through the rest of them and show that it actually works. So what we're going to do is take 3, first of all, and O plus it with C. Okay? That's the 18 class. That gives me the class of 21. All right? That's what I was calling Z in the decryption process. And then I'm going to take that and O times it with N. So now I'm going to go and take 21 O times with 10. That of course is going to give me the class of 210. Okay? But now we've got to ask ourselves, what is the class of 210? What is 210 congruent to mod 29? If you go and make a calculation, you will find that this is the class of 7. And what is the class of 7 as a letter? It is the letter H. So as you can see, I am decrypting the D into the original plain text. The D decrypts to H. That was the first letter in the word help. So if you work your way through all the calculations in this process, here's what you end up with. Let's walk through the second line here and see how it works. We receive the character X that converts into the class of 23. And what I'm doing here is I'm taking the class of 23 and adding it using the O plus operation to my C here that was 18. And that gives me the class of 41. Now the class of 41 and Z29 is the same as the class of 12, because 41 and 12 are congruent to each other mod 29. Getting that 12 there, I now take it, the 12, and times it, multiply it using the O times operation to 10. That is the same as the class of 12 times 10, which is the class of 120. And the class of 120 is the same as the class of 4, because 120 and 4 are congruent to each other mod 29. And we continue this out for the P, for the question mark, and for the letter I. Now what I want to just point your attention to to end things off here is look at the classes that we received, 7, 4, 11, 15, 28. What are those as letters? Well the class of 7, of course we said is the letter H. The class of 4, let's see, A, B, C, D, E. The class of 11 is L. The class of 15 is P. And the class of 28 is the exclamation point. So the recipient here has received a string of characters. It just looks like noise. It has no information seemingly embedded within it at all. But through the mathematical process of using these modular arithmetic operations, the receiver can tell what the original message actually was. So that's an affine cipher. Pretty interesting. There's a lot of variations on it that are even more interesting. It's a nice application of how modular arithmetic can be used in sort of real-life purposes. But thanks for watching.