 Hey everybody, Mr. Gibson here with your next lesson in cryptography and today we're going to be talking about the RSA cipher which is the Kind of most widely known Public key encryption system used today. There's a few others that are moderately well known like elliptic curve cryptography But this is probably the most widely used at this point So we're going to dig into the details and see how it works and look at an example So let's just talk a little about the history of the RSA cipher It was first published by Ron Rivest, Adi Shamir and Leonard Edelman in 1977 and it was one of the first public key cryptosystems, which is probably why it's still used today First in the door it really kind of sticks around for a while. So while some other ones have come up This is certainly the most prevalent It's important to note that the algorithm that we use is relatively slow for computers to actually complete So it's not used to encrypt large amounts of data In practice, it's actually most commonly used to encrypt keys to other more efficient ciphers that are more symmetric ciphers. So modern Symmetric ciphers that are used frequently are AES, Blowfish and a triple DES All of these are symmetric. So very similar to the more historical ciphers that we've covered in this course But they use the RSA cipher for the key transmission. So it's a nice way to solve that problem Those AES, Blowfish, triple DES are very secure symmetric ciphers The only issue with them is because they're symmetric is the key problem So solve that problem with RSA and those are great to use A little bit of mathematics that we're going to need to understand to fully work with the RSA cipher is this idea of the Euler's totient function So there's a lot of words there that are hard to pronounce Euler is the is the mathematician The totient is how you pronounce that word and then the the Greek symbol that represents the totient function is phi So kind of a circle with a vertical slash through it So we're going to represent that as phi of n where n is any number integer number And what that function is going to calculate are the number of integers that are between one and n For which those integers are relatively prime to n and just a reminder relatively prime Just means that the only shared divisor between the number and n would be one So for example, phi of eight would be four because there are four numbers between one and eight that are relatively prime to eight Those numbers are one three five and seven Phi of seven would equal six There are six numbers between one and seven that are relatively prime to seven and those numbers are one two three four five six That's going to be an important pattern there. We can see that when when n is prime something special happens And then our last example phi of ten would be four There are four numbers between one and ten that are relatively prime to ten and they are one three seven and nine What we're going to see about the totient function is that there's not a formula that we can use to really calculate that You actually have to go through and check each integer k k being the numbers between one and n And see if they are in fact relatively prime We'll see that there's a few algorithms that have been popped up over the years that make the slightly more efficient But not not vary. Uh, it gets really hard to do But here's a few of those shortcuts I mentioned If your number um n is is actually a prime number so we can call it p Then phi of p is just p minus one. We saw that in the example earlier phi of seven was six Phi of five would be four that's because p by pr But because p is prime by definition The only factor it shares with any other number is one Which means that every number that's less than p is going to be relatively prime to it So that's kind of just summarizing that observation And then the second uh important fact about the totient function We're going to need for the rsa algorithm is that if your number Is composed of the product of two primes So say p and q are two prime numbers and n is just equal to p times q then if you want to compute phi of n Which is the same thing as saying computing phi of p times q It turns out this this totient function Is multiplicative in nature meaning that you can just find the phi of each of those primes and then multiply The result so if we know that phi of p is p minus one and phi of q Is q minus one then phi of p times q is just p minus one times q minus one Now that only holds true when p and q are primes doesn't work for all numbers But it does work for these primes and that is going to be helpful for us here in a moment Okay, let's get into the rsa function Because it's public key We need to have a public key and a private key that are paired together So we're going to want to generate those keys in a special way So they have the properties that we want And we do that by starting out choosing two prime integers and we'll call those p and q In this system p and q must remain secret in fact that is the secrecy this this whole system is built upon relies on keeping those values unknown to anybody but you We're going to use p and q to compute two other numbers We're going to compute n which is just the product of p and q That's going to be public information that n is going to be just like with kid rsa n is going to be the modulus that we're working in in our operations So we're going to give that away But just don't show how you calculated it Then we need to compute phi of n and since using the result we saw on the last slide Since we know that n is composed of the product of two primes This is pretty easy for us to compute using that shortcut We can just do p minus one times q minus one phi of n needs to be secret that is the real reason That is the real reason why this whole system stays secure is that we're going to see that if anybody new phi of n Or new p and q and therefore could compute phi of n This whole system goes away. It's not secure anymore So we must keep those values secret Then we need to choose a value for e and e just like in kid rsa Is going to be part of our public key. It's going to be a number that we use for encryption So that's going to be public and what's what's interesting here is that we can just choose e There's only one criteria and that e needs to be relatively prime to that phi of n that we just calculated so As soon as you choose e you just want to do a quick check. Is it relatively prime to the value of phi of n A real common choice is to use this number 65 537 Two reasons It's a large prime. So it's going to be relatively unlikely that it shares any factors with phi of n And then the second reason is a more computer reason It can be represented using one and then a bunch of zeros and then a one in binary And there's something about the way that this gets programmed using computer programming That makes that very efficient that long string of zeros in the middle of the binary representation Makes this operation go a little bit quicker than it would otherwise And then the last thing we need to calculate is d which just like in kid rsa d is a value that we're going to use in the decryption process D needs to be the multiplicative inverse of e in the modulus of phi of n And we can quickly compute that using the euclidean algorithm that we've seen previously So again, you would not be able to get the value of d unless you knew e Which you will because it's public and phi of n which you better not because that is secret That's the reason why we need to that we need to keep phi of n secret is that if you knew that you could quickly compute d The multiple the inverse of e So you you need to keep that secret All right, let's see how this works in practice here in just a moment. But first Let's see how we encrypt the message So we encrypt a message we take our message m Convert that to a number and we raise it to the power of e and then mod by n That's going to generate our cipher text message as a number and we'll call that number c And then to decrypt a message, um, you take your cipher text message c You raise it to the power of d and mod by n and that'll get you back to your plain text message m So unlike with kid rsa where you took your message and multiplied it by e and then you took your cipher text and Multiplied it by d we raising it to a power here And that's where the math a lot of our mathematical security comes in Is that exponentiation is just a lot harder to wrap our heads around both? Kind of mathematically, but then also computationally it takes it to the next level a little bit harder to calculate So why does this work? Why is it secure? And how do we know that that encryption and decryption process actually works? That's a long answer the short version is it's complicated Is a little bit beyond the scope of this course to explain why those operations undo each other So you should if you're interested wanted to see why that works take a course on number theory or Read up on fermat's little theorem fermat's little theorem is kind of the the big mathematical result That's needed to prove that those two things actually will undo each other So i'll leave a link in the video description if you want to go read up on that Okay, let's see an example done in practice here So we've got our usual actors alice bob and eve Say alice wants to send the plain text message high over to bob without eve intercepting it So we've got the open lock representing plain text Which means that bob needs to generate a key pairing so that alice could get the public key So bob chooses these two prime numbers 1171 for p and 2099 for q Those are our secret remember pink numbers and letters are going to mean secret information and green will mean public So the first thing bob needs to do is compute the value for n doesn't want to show how he does that So you wouldn't show the 1171 times 2099, but he gets the result of n equals 2457929 Now he needs to get to work computing phi of that number and because bob knows p and q He can compute phi of n pretty quickly. Um, he can just do one less from p and one less from q and multiply those together Um, this is all now secret information. Nobody should know that phi of n is 2454660 Because he's now going to use that to compute his decryption key He's arbitrarily chosen e to be 65,537 it could be any number That's relatively prime to phi of n, but this is a nice choice He's confirmed that the greatest common divisor between e and phi of n equals one Meaning that's the only shared divisor. So that's a good choice to use for e Now he just needs to compute d and I remember d is the multiplicative inverse of e in the modulus of phi of n And we can use the extended Euclidean algorithm to do that pretty quickly and he gets the number 1687553 So there's the three numbers that bob needs to generate the public and private key pairs And he does that So there's the generation keys are generated. It is now time to share those keys. So bob sends the public key over to alice Eve grabs it along the way Not sure what she's going to do with that yet, but we'll find out Now that alice has the public key She'll convert her message high into binary using the ascii table and then convert that to a decimal And now she's off to work to create her encrypted message. So she's going to take her plaintext message 267 29 and raise that to the power of e and then mod by n So this operation you're not you're going to be able to do on a handheld calculator This this is a big calculation. You're going to need some serious computing to do it efficiently Python can do that with a built-in function called pow You can also give it to something like wolf from alpha and it will have no problem with it but you're going to need to some sort of specialized computer to do this your ti 84 or Calculator on your on your phone probably can't handle something like this So you're going to need something a little bit more specialized But when she does that she gets the number 101 6 9 5 5 That's our cipher text message. She could convert that to text using base 64 She wants to to make it easier to transmit, but it's fine. She can send it across the line that way and she does Uh, eve picks it up on the way. So she knows the cipher text in the public key Bob now knows the cipher text, but only bob can decrypt it because only bob has the private key So bob gets to work. He's going to decrypt that cipher text message By raising c to the d and modding by n. So in this case, that's 101 6 9 5 5 raised to the power of 1 6 8 7 5 5 3 And then we mod by 2 4 5 7 9 2 9 again That is not an easy calculation to do you're going to need something That's really good at computation to take care of that for you python wolf from alpha something When he does that he gets back the plain text message that alice had created on her end 26 7 29 He can convert that to binary and then back to the plain text message. Hi Not that complicated of a process, but it does require some computing power Now eve cannot retrieve that plain text message. She just does not have the ability to do so why In short again Multiplication is easy. It was easy for us to generate n and generate phi of n which were the things that we needed to compute D But it's really hard to do that if you don't know p and q And if you don't know p and q because your alice or eve you didn't choose them and you don't know them You can't calculate phi of n and if you can't calculate phi of n, you can't determine the decryption key d from e Here's just a quick example. This is a relatively small value for n, you know, it's in the trillions maybe It's hard by looking at that to decide what p and q are Computer can do it. It won't take too long, but that's only a 60 bit modulus And remember in practice these key values have bits that are thousands in length these keys are thousands of bits And it gets harder and harder for each additional bit for you to be able to factor that back down to p and q And again once you have p and q you can you can reverse engineer the decryption key the private key So here's what they are Just for this example But imagine if this thing had hundreds and hundreds of digits long It's going to be very hard for any computer to do it and that's where the security of rsa comes from When we have moduluses that are hundreds of digits long It takes computers hundreds of years of computing time Um To be able to factor those back and and at that point hopefully whatever message you are using to encrypt has now gone out of date It's important to realize that this is not a mathematically secure cipher With enough time and computing power you can always break rsa But what the premise is that it's going to take you too long for it to be worth your while for any message that you send using rsa So that's the basics of the rsa cipher Is our is our last cipher that we're going to cover in this course So I hope that you've enjoyed learning more about it and that's it for today. Thanks for watching. We'll catch you in the next one