 Hey y'all, it's Mr. Gibson, and this is our next video for cryptography. In this lesson, we're going to be taking a look at the multiplicative cipher, which seems to be pretty similar to our Caesar cipher, in that we're going to be using some arithmetic to take our plaintext alphabet and generate a ciphertext alphabet. The difference between the multiplicative cipher and the Caesar cipher is that the multiplicative cipher will use the multiplication to generate our equivalent values in the ciphertext. And while that sounds like it might just be a real easy swap for addition to multiplication, what we're going to see is that there's a few considerations that we're going to have to take into account when we're multiplying by numbers in a given modulus. Let's take, for example, if we use the value for our key to be 2. As we take each plaintext equivalent value, so A's equivalent value would be 0 and B1 and so on, we can start generating values pretty quickly for what we want our ciphertext alphabet to be. A becomes 0, which will still be A, B goes to 2, which is equivalent to C, C goes to 4, which is E, D goes to 6, which is G, and so far so good, no problems. That is up until we get to N. When we double N, 13 times our key value of 2, we get 26. Since 26 is outside of our range of values, 0 to 25, we need to find its equivalent value in a modulus of 26. Thinking back to our last lesson, we can do that a variety of ways, but probably the easiest way to do that is to subtract off 26 to give us an equivalent value of 0, which corresponds to the ciphertext letter of A. And this is a similar problem that we saw when we first looked at general substitution ciphers with the keyword of unicorn. When we had two repeated letters in our keyword, we ended up getting two different plain text letters mapping to the same ciphertext letter, and that's what's happening here again. Both this plain text letter A and N have mapped to the same ciphertext letter of capital A. And you'll see if we keep moving down the line, O would go to 28, which its equivalency or reduction in modulus 26 is 2. That corresponds to a ciphertext letter of C, which we had already mapped from plain text letter B. And that same pattern is going to continue all along the way. That problem isn't really on the encryption end. It's not going to stop us from creating a ciphertext, but the real issue is going to come in when we have to decrypt that message. If we have a lot of letters in our ciphertext that came from two different plain text letters, there's not a way for us to know which plain text letter it came from. So as it stands, this algorithm is not invertible, which is a requirement for a good crypto system. But it turns out that we don't need to abandon hope altogether. It just happens that the key value of 2 is a bad one. So let's start over again. I'm going to erase all of this real quick. It turns out that if we had just done a key value of 3, everything would have been fine. 0 would still go to 0, which is A. 1 would go to 3, which ends up being D. And so on, up until we get to around, let's say, 9. 9 times 3 is 27, which is equivalent to 1, which gives us the ciphertext letter B. And if we keep continuing this pattern all the way to the end, we're going to see that we never actually end up repeating a ciphertext letter mapped back from a plain text letter. We're going to get 26 unique letters in the ciphertext alphabet mapped back to the 26 unique letters in the plain text alphabet. That's perfect, because then that's going to be invertible. So what was it about 2 that was bad and that 3 is good? Well, if you've done the reading, you might already know the answer, but here's the short version of it, is that whatever value we choose for our key needs to be what's called co-prime or also known as relatively prime with the modulus that you're working in. What that means is that it can't share any factors. So if you think about 2 and 26, we know that's not going to work because 2 and 26 have a shared factor of 2. However, 3 and 26, that's going to be fine. 3 and 26 shared no factors besides 1. And that means that this system is going to work with that key. So if we were to go through all the possible key values between 0 and 25 and try them out, we can eliminate the ones that have shared factors with 26, leaving only the following key values, which are good for multiplication ciphers, 1, 3, 5, 7, 11, 15, 17, 21, 23 and 25. These all work in a modulus of 26. You notice we're able to take out the factor, any number that had a factor of 2 and 13. You might have been able to predict that easily if you looked at the prime factorization of 26. 26 is represented as the product of its primes 2 and 13. And that's a nice trick for to figure out how can you quickly identify the good keys for a given modulus is look at the prime factorization of your modulus. Then look at all of the numbers that are between 0 and 1 less than your modulus. And any numbers that have a shared factor, looking at prime factors is usually a little bit easier than looking at the composite number that is your modulus. Just a little tip to speed things up. Now to decipher a multiplicative cipher, we're going to have to try something a little bit different. So let me clear the screen here and we'll look at that in just a minute. Okay, if we're working with the key of 9 and we want to decrypt a message, we need a way to undo the multiplication that we just did. So for example, let's say we had our plain text letter e. If we were to encrypt with the key of 9, we'd multiply 4 by 9 to get 36. Find the equivalent number between 0 and 25, which is 10, and we know that goes to k. But how do we know that if I'm decrypting a message that k is mapped back to e? For this cipher, we had an easy way to undo the addition. We could just subtract by the key and it would get us back to our equivalent number for the plain text. However, doing that here, we can see isn't going to work. If I take 10 and I want to undo the multiplication by 9, we might be tempted to divide by 9. But 10 divided by 9 does not get us back to 4. This is a little bit tricky. It might be a little bit counterintuitive at first. But what we need to figure out is what number could I multiply 10 by in order to equal 4? And we want that number to be an integer between 0 and 25. And unless you've worked in modular arithmetic before, multiplying 10 by an integer to make it equal 4 might seem like an impossible task. But what we're really thinking about doing is what could I multiply by 10 to get me to a number that's equivalent to 4 in modulus 26? That number is called the modular multiplicative inverse for a number in a given modulus. So we're trying to find the modular multiplicative inverse of 9 in modulus 26. So that question we're trying to answer is 9 times what is equivalent to 1 mod 26? That's what it means to be a multiplicative inverse is that we can take a number, multiply it by another number and have it be equal to 1. You might be familiar with multiplicative inverses with rational numbers. If I asked you for the multiplicative inverse of 5, you'd know that would be one-fifth because those two numbers multiply to give you 1. They are inverses of each other with the operation of multiplication. So to answer our original question of what is the multiplicative inverse of 9 in modulus 26, well, we don't really have a good way of calculating that yet. It turns out there is an algorithm for efficiently computing that value, but for now we're really stuck with trial and error. So we can sit here and think about it. We can use some of our tips from a previous lesson on modular arithmetic. In this case, we'll cut to the chase and we know that 9 times 3 is 27 and 27 is equivalent to 1 in mod 26. So we can say that 3 is the modular multiplicative inverse of 9 in modulus 26. You could write that and saying 9 inverse is equal to 3 just to kind of a notational thing, but let's verify that that's actually true. So we have already seen that if we take 4, the plaintext letter E, equivalent to 4, and multiply that by 9, we end up with 10. But what we want to show is that if we take 10 and multiply it by the inverse of 9, which is 3, we get back to 4. So let's try that out. 10 times our inverse of 3 equals 30, and 30 is in fact congruent or equivalent to 4 when we're working in a modulus of 26. You can convince yourself that's true by reducing it down using one of our methods. So that's how we're going to be able to undo our multiplicative cipher, not by dividing by the key, but instead by multiplying it by its inverse. Every key that works in the multiplicative cipher, so that 1, 3, 5, 7, and so on, must have a multiplicative inverse in that same modulus. I'll leave it to you to figure out the remaining modular multiplicative inverses for the keys that are not 9. And once you know those, it might be helpful to try and commit them to memory because we'll work with them fairly often. So you should have 12 valid keys in this system for our 26 letter alphabet, and they should have a corresponding 12 modular multiplicative inverse keys that go with them. That should do it for today. You should have a good understanding of how the multiplicative cipher can create a mapping from a plain text letter to a cipher text letter, and then how to find the modular multiplicative inverse of the key you used to help you go back from the cipher text letter to the plain text letter to the cipher message. Thanks for watching, and we'll catch you on the next one.