 In this lesson, we'll be focusing on some substitution ciphers, in particular additive ciphers, and most notably the Caesar cipher. As we discussed in a previous lesson, there's many ways to create substitution ciphers. And in fact, if we were to just focus on our plain text alphabet on the screen, we would have 26 choices to map the letter A to some letter in the ciphertext alphabet, and then 25 choices remain for the letter B. And because those are independent choices, we can multiply the number of possibilities for each together and figure out there's just 650 ways just to assign letters A and B in the plain text to some letters in the ciphertext. Following that through, for the rest of our letters in our plain text, we have this 26 times 25 times 24 and so on, which is known as a factorial in mathematics, and 26 factorial we can see leads to some very, very large number of possible plain text to ciphertext mappings. In order to make this a little bit easier for people to implement and remember the correct one, we're going to look at some different algorithms to create our mappings for us. In order to do that, we're going to need some sort of numerical way to represent our alphabet. And in this course, we're going to do that by representing the letter A as zero, B as one, C as two, and so on, all the way up to the final letter of the alphabet, Z being 25. You'll see in other resources that sometimes A is represented as one and Z as 26, which makes a lot of sense when you think about counting. But the way that computers and oftentimes mathematicians and computer scientists start counting is with the number zero. And we'll see that this has a few, there's a few reasons for that, some of which make our mathematics work out a little bit nicer than having A as one. So for example, this short word crypto, we could turn into the numbers two, 17, 24, 15, 19, and zero, and 14. Now that we've established our numerical system for representing letters, we could learn how to actually implement an algorithm to encrypt a message. So the Caesar cipher named after Caesar, the famous historical figure, is an additive cipher, and it uses a key, which is an integer, and that key will specify what number to add to the numerical representation of your plain text letter. And that'll help us determine the numerical representation of our cipher text letter. So for example, if we had our plain text of crypto with those same numbers, and I had a key of three, I would add three to each of those values to get a new numerical value. So two becomes five, 17 becomes 20. And then we can look those back up and figure out, well, five is the same thing as an F, and 20 is the same thing as a U. 27, we don't really have a representation for us to talk about how to handle that. But for 18, we know as an S 22 is a W and 17 is an R. And we'll see that really no matter what the key is, as long as it's not a zero, adding that number to our plain text is pretty likely to result in some number that's bigger than 25 that we need to figure out a solution for. But it turns out that this problem is something that we've dealt with before in our everyday lives whenever you read the time off of an analog clock. Here we have a clock where the hour hand is at nine, and if I were to say five hours were to pass, you might think, well, we're going to end up at two. Mathematically we might have the statement nine plus five equals two, which doesn't make a lot of sense just taken out of context nine plus five is not two, it is 14. So maybe we specify it this way. Nine plus five is congruent to two or equivalent to two, and we'll put something there in parentheses to specify why that actually might be true. And in this case, we're thinking about it as clock time. So we can say nine plus five is equivalent to two when we're talking about clock time. And we have a couple of other examples here. If we're noon and five hours pass, you would say it's five. So we could say 12 plus five is equivalent to clock time or 12 plus 12 is equivalent to 12 in clock time. And we can extend this out further. So it's not just talking about times on a clock, but we could do the same thing for really any size number system we wanted to work with. In our particular case, we wanted to work with a number system that started at zero and ended at 25. And on the next slide, we have a very long clock. We've spiraled it around, but it starts at zero in the center and it works its way around. And we can see that every lap around the spiral increases the counter by 26, either forwards or backwards. So if I were to ask you what 24 plus three is, we could find 24, move three spots to the right on the spiral and see we land on 27. And if we focus on this wedge where the 27 is, we could also say that 27 is equivalent to one or 53 or 79. All of those are equivalent numbers, but we have to specify that we're working in a number system that goes from zero to 25. And the way that you would do that is we say we are working in a modulus of 26. We have 26 numbers in our system, and it starts at zero and goes to 25. This whole process is called doing modular arithmetic. And the number of the numbers in our number system is what we call the modulus. So mod 26 is kind of our shorthand, not for clock time, but in this case that we're dealing with a number system with 26 numbers in it. So I'll get a few other examples here. And this pink wedge that we've highlighted, we could say like 24 plus two would land us on 26 or we could say it's equivalent to zero. But all of these numbers are actually equivalent to zero, one of four, 78, 52, 26, and zero, assuming we're working in mod 26. Works for negative numbers as well. So if we did 15 minus 23, you might have thought, negative eight, which lands us in that green wedge back on the spiral. But if we wanted to represent an equivalent number, mod 26, we could do 18. In standard practice, we'll always reduce these down to the numbers that are actually between zero and the modulus that's specified. So we don't typically keep negative numbers or numbers that are bigger than the modulus. We're always going to get a number back between zero and 25 when we're working in mod 26. Another interesting feature here of this modular arithmetic system is that if I started at 15, I could have got to 18 by subtracting a number, 23. But I could have gone basically the other way around the spiral and just added a different number to land in the same spot. In this case, I could have subtracted 23 or added three, and I landed in the same equivalent pie wedge. And we can see here, those two numbers have another special relationship. If I started at 15, and I moved ahead three, and then I moved ahead again 23 more, I land on 41, but that's equivalent to where I started 15. I essentially just did a full lap around the circle. So that we can see that 23 and three, they have this special property. And as a result, we can call them additive inverses, because they essentially will bring us around the spiral one full rotation, bringing us right back to where we started. We might call that an additive inverse, because if you think about two numbers that you could add together, that sum to zero, maybe that's what you're used to thinking about about additive inverses. That's essentially what we have here with three and 23. They don't actually add up to zero. They add up to 26. But if you're working in mod 26, that's essentially the same thing. So we could say in general, any two numbers are additive inverses mod 26. If one of them is represented as n, the second one you could figure out by just doing 26 minus n. That'd be an important factor for us to remember here in just a moment. So we can use this to figure out our last couple of missing letters there when our key is three. If we added three to 23 where we had an X, we had a 26 before, but now we know it's a zero and we know zero is the same thing as an A. 24 goes to 27, but we know that's equivalent to one, and 25 goes to 28, but we know that's equivalent to two. So we could finally figure out our plain text. Now, mathematically, we could write this statement here. We could say that C is equivalent to P plus K mod 26, or solving for P that P is equivalent to C minus K, where P is some integer that represents our plain text letter. C is some integer that represents our cipher text letter, and K is some integer that represents our key. So there's our mathematical way to define the Caesar cipher. So let's actually practice deciphering a message now that we've defined how to do it mathematically. We'll start the same way. We'll take our cipher text letters AOL, VYF, and convert those two numbers, 0, 14, 11, 21, 24, and 5, and then we'll subtract off our key of 7. 0 minus 7 gives us a negative 7, 14 minus 7 is 7, and so on. We get our kind of plain text integers, but we notice those don't map to 0 to 25. We've got some negatives in there. So we'll mod by 26. So we'll find the equivalent number of negative 7 that is between 0 and 25, that is 19. 7, 4, 14, and 17 are already between 0 and 25, so we don't have to mod them, although 7 mod 26 still brings us back to 7, no big deal. And our last number of negative 2, if we mod that by 26, that'll bring us to 24. Negative 2 and 24 are equivalent, but 24 actually maps to a number. And we convert those back to letters here, and we get our word theory. Now, I mentioned before that we could subtract the key of 7, but we saw that on the back on our modular arithmetic spiral, we could have added a number. So think for a second, what number could we have added to decipher this message if we didn't want to subtract by 7? Hopefully, you came up with 19. 19 and negative 7, sorry, 19 and 7 are our additive inverses because they summed to 26. And let's just verify this actually works. So we'll take the exact same message, exact same integers for the ciphertext, we'll add 19 to them all. Instead of getting negative numbers, we'll get some numbers that are bigger than 25. In fact, we have several of them. We've got this 33, 30, 40 and 43, but finding the equivalent in mod 26 brings those numbers back to the exact same integers we saw when we subtracted by 7, and we get the exact same plaintext as a result. So we could go either way when you have your key, you can either subtract the key or add by the inverse of that key, the additive inverse of that key. We'll see other ciphers later where we'll have multiplication going on. And there, we're going to find out later, we don't actually have a choice, we're going to have to multiply by an inverse instead of dividing by the key itself. So we'll talk more about why that is in a future lesson. Now, how would we do this in Python? We'll see that Python has a handy modular arithmetic operator or the mod operator, it's the percent symbol. And it will tell you which value is equivalent to the one that you specify between zero and whatever your mod value is. So for example, if we had some plaintext represented numerically as 15, and our key was 20, we could compute the ciphertext numerical value by adding the plaintext number in the key together. And then if we mod by 26, it will find the equivalent integer between zero and 25 to whatever plaintext num plus key is. So those two numbers add to 35, modding by 26, as we'll see when we hit that print statement, is going to return back a nine to us. So it actually did that work for you. You don't have to draw out the spiral or do any addition or subtraction to figure out your equivalent number to the 35. The computer can handle that calculation on your behalf. Now, one quick trick to show you here, and we'll get more into the details on this in our next lesson, is that we can actually use a little bit of extra code to not just show you and compute the ciphertext numerically represented, but actually convert that back to the letter that you wanted to see as our ciphertext. And it has to do with this change in our print statement right here. Notice we are referring back to this letters string in our very first line of code in the block. And we're trying to use the ciphertext numerical value to specify an index to go look and pull a character out. Now, why might this work? Remember, our ciphertext numerical value was nine. So if we use that as the index to this letters string, we're going to go to the ninth index and pull that letter out. Well, look at the way that that string was constructed. A is at index zero, B is at index one, all the way to J being at index nine. The index value for the characters in that string are the exact same values that we specify should represent those letters in the alphabet. And that's part of the reason why we said A should be zero and not one is that by defining our letters string as A, B, C, D, and so on, we can ensure that the character's index matches the numerical representation that we've already set up on. So they are always in agreement. Little side note, we labeled the letter string all caps because if you remember, an all caps variable in Python by default, by convention means it's constant. We're not going to change that at any point throughout our code, which makes sense. When you have a alphabet that you're using to encipher and decipher messages, you don't really change that once you get going into the problem. It is what it is you define it when you get started and you don't expect to change it. So we'll use this trick of going back to this alphabet string or letters string and pulling specific characters out of it based off of the numerical value of the letter we hope to use. We'll use that quite a bit for all of our ciphers. So we'll learn more in detail how we can do this for more than just letter or more than just one letter at a time when we get into our next lesson. But for now, it's a nice little trick to keep up our sleeves. That does it for our Caesar cipher. Next up, we'll be learning about how to do the Caesar cipher on more than a single letter at a time in code. And then look at other types of ciphers that we can do for mono-alphabetic substitutions. We'll see that Caesar cipher does not have a lot of possible keys. So not going to be very secure. We're going to have to do a little bit better.