Cryptography Masterclass | Master Encryption in Java | Lukas Vyhnalek | Skillshare

Cryptography Masterclass | Master Encryption in Java

Lukas Vyhnalek, Microsoft Employee, Programming Teacher

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
16 Lessons (1h 58m)
    • 1. Introduction

    • 2. What Is Cryptography

    • 3. History of Cryptography

    • 4. Basics of cryptography

    • 5. Substitution Cipher

    • 6. CODE SubstitutionCipher

    • 7. Caesar Cipher

    • 8. CODE CaesarCipher

    • 9. Affine Cipher

    • 10. CODE AffineCipher

    • 11. Stream Encryption

    • 12. CODE XorCipher

    • 13. Random Nimber Generators

    • 14. First Secure Cipher

    • 15. AES

    • 16. RSA


About This Class

So do you want to learn cryptography?

and you want to understand it, be able to describe it, be able to use it?

Am I right?

My name is Lukas Vyhnalek, I am the creator of this course (Cryptography from Scratch | Master Encryption in Java).

These days Cryptography is a hot topic, you often read about someone who hacked something, or about new cipher that will be able to secure our communication. And in my opinion in the future Cyber security will be a huge problem. And this course is designed to get your foot in cryptography and cyber security.

What’s stopping you from signing up to today?

·      You don’t have enough time: Not an issue. I designed this course so you can learn everything you need in a few HOURS!

·      You’re still weighing up the value: I plan on keeping this course up-to-date and make it bigger over time. In fact, if you don’t get everything you need from it… you will get your MONEY BACK.

·      You don’t have any previous experience: Seriously, not a problem. This course is expertly designed to teach everyone from complete beginners ( as the name says "Cryptography from Scratch")

Don't know Java?

Don't worry, if you have previous programming experience, the Encryption algorithms are really simple and I promise you, you will be able to rewrite my code from Java to C (or whatever language you use)

And if you don't, still not a problem you can just skip the "CODE" lectures, or you can write me a message and we will figure something out.

The Curriculum

In this Cryptography course we will start off with a little bit of history, then I cover the basics of Cryptography. Then I show you first simple ciphers (Substitution Cipher, Caesar Cipher, Affine Cipher), and by understanding these you should be able to know what cryptography is all about, and what not to do, because not only I explain how these ciphers works. I also explain How to break these Ciphers. So you will also know how cryptographically un-secure cipher looks like and how to break them.

Once you master Basics of cryptography, we can move on to modern cryptography, I explain what is Stream Cipher, and how Xor encryption works.

I also cover the AES encrypting algorithm and RSA (Public key cryptography).

Also by completing this course, you will get Certificate of completion (if you pass all 3 quizzes)

Still not Sure?

Still not sure whether to enroll in this course? Feel free to watch the preview videos. And if you don't what to. There is 30-Day money-back Guarantee, so you have nothing to worry about. 

So do you want to learn cryptography?


1. Introduction: Hello and welcome to my curse on cryptography. My name is Luke, and I will be your instructor for out s curse. This girl is designed for anybody who wants to learn about cryptography. I start off easy with substitution cipher, Caesar cipher and a Feen safer. Then once you understand the basics, we move on to modern cryptography. I explained all the algorithms in understandable way, and I don't go too much into the mathematical details. I also implement these ciphers in Java. And if you get stack or don't understand something, you can always reach out to me. Mostly I respond to questions. We then a day and Dad is pretty much it. Thanks for your time. And I'll see you in the curse. 2. What Is Cryptography: So what is cryptography? Well, cryptography is a study off techniques for secure communication. But I think you will understand better on an example if I sent a message to my friend where the Internet sometimes can happen, that some other person gets access to it. His message. And I don't want that and neither my friend. So what do we do? Well, we encrypt the message so that if some unauthorized person gets access, do his message, it won be able to read it because it will be encrypted. And what do I mean by encrypted? Well, if the message says, for example, of the world, the encrypted message can look like this. So, as you can see a complete nonsense, if you see this message, you won't be able to deduct from this message anything because as far as you're concerned, these are just some memorandum letters. But if someone has d correct decrypting algorithm, let's say, my friend, he won't be able to turn his nonsense into the original message. And that is pretty cool, right? So encrypting process basically means taking some plain text and turn it into a cipher text . In other words, nonsense and decrypting means taking the nonsense and turning it into plain text into the original message. So now, in real world, if you, for example, sent an email, the data will be encrypted. So if someone is, for example, listening to your communication, he won't be able to read the actual content off your email. The only person who will be able to read it is the receiver off this email. In this curse, I will talk about the encryption and decryption algorithms, how they work, and then we also implement them in Java. I am really looking forward to teaching you how days things works because these Ace security is a big topic. You read pretty often about someone who hacked into FBI database or Hecht. Some bit going. What? But anyway, I hope this is clear. The important thing from this lecture is that cryptography is basically a way off securing communication or data in general so that no party that are not authorized can reedy data. And that's pretty much it. If you have any questions, feel free to ask, and I will soon exam 3. History of Cryptography: Hello, gas. And welcome to this lecture. This lecture will be about the history off cryptography, if you are not really interested in this, feel free to skip this lecture. But in my opinion, it is interesting. And I believe that it can give you a good idea about what cryptography really s. And why is it so important to have a secure cryptography? And in order to do that, I will go back to the Second World War. So do you know that there was something called Enigma? It was a German encrypting system, and Germans believed that it was unbreakable. They believed that it is the most secure encryption that exists, and no one can read their messages. But they were wrong. And the effect at a huge impact on how do you war ended? And even before Second World War there, er, techniques how to encrypt your messages. But I want to get back to my point. The problem here waas that German belief that they're encrypting algorithm waas unbreakable . But how did they know it? The effect as they didn't. They just assume because when you create a encrypting algorithm, you don't know whether it is secure or not, it is not like in everyday life when you, for example, have a exam and you either get a or you get f so you either pass or fail bad. Either way, you know what the results are. So how do we know that encrypting algorithm is good? Well, we make it public, so we pose the algorithm and then we let some people these people are called crypto analysis and we let them try to break de encryption and then when? After a year or five or 10 years of trying, really smart people can break the algorithm, Then we trust this algorithm and we believed that it is secure. So what dead means once again, we only hope that the algorithm is good. There is nothing like mathematical proof or anything like that. We just hope that no one can break it. So what you should take from this is to never use untested, encrypting algorithm. If you can get a mathematical proof which you can't, you need to at least test it before you use this algorithm. And with that being said, I will soon except 4. Basics of cryptography: So what? Dive off encryption do we have? Imagine that I'm sending email to you. So I'm encrypting and you are decrypting But someone is listening to our communication. So I will be l as a Luke. You will be X since I don't know your name and demon who is listening will be s as a spy. So now the communication looks like this. I rode the email. Then I encrypt the email using encryption function and I send it to the Internet. Then you have your decryption function, so you are able to turn the encrypted text into plain text. But I think a minute about whether these two functions should be public or private. Just buzz video and think about it because in a few seconds I will tell you the answer. I guess that many of you set private and as a matter of fact, you are wrong. It is natural for people to think that these functions should be private. And for many years it has been established as a fact. But then some people thought why our dysfunctions private? The obvious reason that probably lead you to the conclusion is that when these functions are public than the spy can also use the decryption function and then he can read the plain text. So now what is there some way around? Actually, Derris, we add to the encrypting function something called key. So the encrypting function takes the plain text and key and turns it into cipher, text and disk E becomes really important on the other side when you are decrypting the message because in order for decryption to be successful, you need the correct key. So now I want you to think about how would you break this system? Imagine that you are spy and you want to decrypt the message. But you don't have a key. What would you do? Once again, you can pause the video because I'm about to tell you the answer. You don't know the key, but you have the message. But there is nothing stopping you from trying different keys. This is killed brute force, and it means that you are trying to find a key. So imagine that there is a limited number of keys black, 10 thousands. So what's by dust? Well, he takes thes cipher text and tries all the possible keys and Eventually he will break the cipher and read the message, and we don't want that. So the number of keys is really important. The more key cipher has the more secure decipher ISS. But when I get back to our example, the problem here iss, how do I get Dickie to you? So the simplest way would be to sent deaky before cipher text that we cannot do that. Why? Well, because spy would also read the key, and then he will be able to read our communication. So what do we do? Well, we need some other way to exchange our keys. And the way we do that is for some secure channel. So I grabbed the key and pass it to you through the Secure channel. And I know the secure channel sounds really abstract, right? So you can think about it as a telephone communication. So I am basically calling you and telling you d password. So the important thing to take from this lecture is that the encrypting and decrypting algorithm or function if you want our public, they are also taking key and text as a argument and returning some cipher text and the key is the secret thing. This is the thing that keeps our communications safe. And also, you should remember that a crypt a system should be secure even if the attacker in our Kays de spy knows all the details about the system including encryption and decryption function , the cipher text. But with the exception off the secret key, if he don't know what the KIIS, we should be just fine. And that is pretty much it for dis lecture. If you have any questions, feel free to ask and I will soon exam. 5. Substitution Cipher: Let's start off easy by substitution cipher. So it is actually a historic cipher, and that is a blood way off saying that it It's a pretty stupid cipher. It actually operates with letters, since it is historic. At the time it was created, nobody knew what ISS bite and what is a binary system. At the time, there were only words and letters and how it works. Well, do know whatis substitution. It may seem complicated, but it basically means replacement. So substitution cipher takes a character and replace it into different one. For example, the encryption can look like this. A is replaced by g b. Is replaced by t see is replaced by n and so on. So on now, for example, if your plain text ISS ABC the cipher text would look like this because we replaced a bi G . Then we replaced be by t and then we replaced. See by at So now do think this cipher is secure. The right answer is no. This is not a secure cipher. But then there is the question. How would you break it? And I want to pause the video and think about how would you break the cipher. So the first approach off how to break the cipher iss for a brute force? Right? So imagine that we are Attackers and we don't know this table. We don't know which character replaces which, how many options. Derris. Because the table is basically are key, right? And we need to figure out the table because we need the key in order for us to be able to decrypt the cipher text. If you already learned something about this greed, mathematics, you know that there is something called permutation and you probably know D answer already if you are one of those people, good for you. But if you are not, maybe tried to think about it a bit. We are trying to find a replacement for leather. A. So how many options do we have? Since we don't know anything about this cipher, it can be any leather. And we have 26 letters. I guess so. I write 26 then we need to know what character is replacing B. And we already have one character that replaces a right, so we are picking a character about this time. There's only 25 characters available because we already picked one that replaces a and we cannot replace, be by the same character, right Then when we pick character replacing, see, we have 24 options and we go all the way down to one. So the result is 26 times 25 times 24 all the way down to times one. So that is a factory away off 25. So we have factory off 25 options. How the key could look like and that is more or less two to the power off 88. And that is a huge number. Even the most powerful computers in the world would not be able to compute this in reasonable time. So brute force is not do away because there are too many possible keys. But the second approach is maybe a bit smarter. In the second approach, we use a letter frequency and what that means. Well, the problem with this cipher is that when we take a character, we always turned it into the same one. So, for example, all ace will be replaced as a G in are safer, right? But the problem is that some letters in plain text are more frequent than others. For example, the letter E is the most gammon leather in English, almost 13% off all letters in typical English text R E. The next common leather is the leather tea with about 9%. So what can we do? Well, we can just look at the cipher text, and we count the frequency off characters in it. And based on that information, we can figure out the key, the transformation table if you want. So if I add here, for example, that E will be replaced by D and in this cipher text, if we find a letter that have frequency off about 13% it is probably e in plain text. Off course. This approach won't work with a B B A as a plain text, but with common English text. This would work, so that is basically how we can break this replacement encryption. And it works because of aesthetic transformation, because a will always map to G. Now, the message from that is that if you are a crypt analyst, sis, in other words, if you are the good guy that is creating ciphers and securing data, you have a problem because even though the attacker would, for example, fail in one approach, he can try different one. And if one approach breaks your cipher, your grip sh in is unsecured even though all the other attacks were unsuccessful. So now I want to talk about the approaches attacker again today in the 1st 1 is the simple brute force as you saw before, where the attacker just simply tries Oh, possible keys And with each key it looks at the decrypted text and checks whether it is a valid text on the underhand, there is a analytic attack and what that means. Well, attacker really looks inside the encryption and decryption functions and tries to figure out some smart way off breaking it. So, for example, like we did with the frequency off characters in text that waas a analytic attack. But unfortunately it does not end here. Other type ISS, for example, social engineering. You probably heard about this already. For example, if I need a password off some person to their email, I can call them and tell them that I am from the I T department and we lost their password or something like that and that I need their password and these sorts off attacks are very common. And then it kind of doesn't matter how strong your crypto function is when you give away your password and the last one is the most mother and it is skill implementation attack. And I will explain this one by an example. Imagine that you have a credit guard, you know that these have a microprocessor inside D card, right? Something like 80 51. And this microprocessor runs the encryption algorithm and also on the card is somewhere stored key. And this key is very hidden. You cannot excess it, but what you do, you take some machine that measures power consumption and you trigger D card. So you basically say that I am de a t m and start encrypting and you measure the power consumption. And this power consumption has something to do with the G. This is Gold Side channel analysis, and it is actually pretty easy to extract the key if the hardware is not protected. Thankfully, since we know about this type of attack, we can also defend against it. So your credit card is safe. But this was just one example, off implementation attack. They're more off these, but I won't bore you with that. And that is pretty much it for dessert Video. If you have any questions, feel free to ask, and I will soon excited. 6. CODE SubstitutionCipher: Hello, gas and welcome to this video. In this video, I will implement the substitution cipher. So let's get into it. I already defined an encryption map which is just static character array, and it is filled with 26 letters and the way it will work ISS that I will use the character from plain text as a index. And when I index the encrypt map, it will return me a substitution character substitution leather. Okay. And next I defined a decrypt map which is just a function returning a in teacher. And this in teacher is basically the index, the index of the this character in this array. So I will use the index to compute the the encrypt it character. So let's get started. I ride here public static string because this function will return a string called Encrypt and I will pass there string and I'll just go it message now. Well, I will do this, Percival. I will create a integer variable gold difference between lower and uppercase. Right. So, like this. And it will be just a big a uppercase a minus. Lower case A. I will use that later on. And I'll also need the result, right? So string resolve and I'll initialize it to empty string now for every character. So I equals zero while I a smaller Dan message Death landing length. Okay, I'll increments I and I'll just great a variable. It will be simply or so I'll get the character at Index I and I'll store it into this E variable. Now our first check, whether it is a character. Sorry, but it is a leather. So it's letter C. I'll use this method Do determinate whether it is a leather. Now if it is letter, I will encrypt it. If it is not, I will just leave the character. I don't want to encrypt a spaces and stuff like that. So But in here, since I have dis stable the fines on Lee for lower case right, So I need to use a lower case leather as a index. So if the character IHS upper case and I'll just use the sea right here, I can also reference to dislike like this, but it's simply too right, see, And this should be in lower case. So if the character is upper case, I first need to turn it into lower case and the way I will do it A simply by subtracting this this variable right, so I used is variable to kind of transform for from upper case to lower case. So now what I can do is simply and gripped it. So I used the and crypt map like this, and as a index, I will use my character, see, But since in s key or utf the character, for example, a have value off 35 6 I don't know, but that's not important. And the all right, it's indexed form zero. Right? So I need to subtract from there. Hey, because when the character is a the any minus a zero, right, so it will reference to this first element. And that's the element I want to map. Right? So that's how d encryption works pretty much. But now I also need to turn this already encrypted letter to the upper case because I turned this see into lower case and I will just do that by simply adding the lift low upper variable, and that's it. And if the character is lower case, I'll just simply do only this line because this this is how you encrypt a lower case character. This is for uppercase, simple as that. So now when I'm done, I will just add to the result my character. And now I need to also return the result that that is pretty much it. That's how the encryption works. So now let me define a decrypt function so public static string called decrypt. And it will also accept some message Samos encrypt. So I will I will just gonna copy paste that and will be faster. So I still need this difference off lower and uppercase. I still need the result because I will return the encrypted text, the decrypted text and I still need to luv for every character in the message. I still need to check whether it is a leather and and if it is a letter I need to check whether it is uppercase and if it is uppercase, I need to make it lower case and then what I need to do. IHS used the use This function right here decried map because I will pass a character in there and it will be some decrypted character, like be maybe, for example So this will return me a index of B and it is free. And the cool thing about asking or some other coating is that a is for example, 36 then be is 37 right, So the letters are behind each other, so I can just at two a the index off the array, the index Zet de crib map returned, I hope does make sense. So what I will do is just simply right a lower case. Say, last the index that it returned. So I forget the name off off the function secret map. So decrypt map off the character, right? And I also need to cast it into because it will return. This function will return a in teacher So like this. And actually I can make I can do also this step inside this line because I know in here that the illiterates uppercase So all I need to do now is right. The upper case A. And then I don't have to ad d difference. Right? So now I'll just copy and paste it right here, because here I've all just use lower case, say, and it should work and yeah, so let me try this I already defined some bring ting brand lines. So let me just run this and hopefully it will work and we should see the result. So yes, it seems like it's work we have here the original message That's a hello world The encrypted message says deem with something And the decrypted message says hello world. So yes, that is pretty much it Now I also show you how how the frequency attack can work. I'm just going to write a soda code so it will not be a actual working example Bad It still may be interesting If you are not interested in this, you can skip this bar so I'll just write function like name like break and it will take this string message and you can't see it Barbara Malaysia. So I'll just un commend it and and what dysfunction will do is like this break decipher And what dysfunction will do is it will take the encrypted text and without knowing these knowing dysfunction, it will canned. Ah, figure out the cipher. So I still need like this because for every character I will compute the frequency. So I will have some something like in teacher array. Oh, I will have, like, integer array off that frequencies. So array frequency, and it will be array off 26 elements and each element off this Ray will be representing a number off the parents of death. Precise character. Right. So on index zero will be the number off ace in the encrypted text. Right on index one will be the number of bees and so on and in here I will just like increments the the array for every character. Right? And then I will use the array for because I know already the frequencies right in here. I computed the frequencies. So then it's simple. I will just use some map, I guess where I keep the track off the frequency, the normal frequency in English text and I will map my frequencies and get the and get this basically get the cipher. And that's how I would brag. Decipher If you have any questions, feel free to ask ends. I will soon except 7. Caesar Cipher: Hello, guys. And this lecture, I will talk about Caesar cipher, which is one of the most simplest crypto systems. And the idea is simple. The idea is to shift the letters in alphabet. It is sometimes gold shift cipher, and it is a historic cipher. So it operates with letters, not bytes, not bits. The modern crypto systems are working with bites and numbers, right? So if I get back to Caesar Cipher, you might ask, What do I mean by shift? Well, for example, if I shift by four characters, the encryption would look like this. It grabs a and turns it into a plus four. So to e, then we grab, be from plain text and turn it into F. Then we grab, see and turn it into G and so on and so on until we reach you and we turn it into I. Then we reach we and we turn it into Z. So now we reach W. But we don't have a leather behind Z ride. Z is where our elf bet ends. So what do we do? You may pause the video and try to figure it out. It is not so hard it has to do something with the module operator. So how modelo operator works If we have, let's say 20 Modelo 26. What do we get? We get 20 right? Because we want the remainder after division. And when you divide 20 by 26 the remainder is 20 right? But what if I do? 27 Model 26. The result will be one right, because that is the rest after division. And if you look at the alphabet, it ends with a Z and starts with a So if you add a number to every letter it looked like this. But now where you want the letter? 26. Right. So what do we do? We do Margallo operation and we get zero. That is a so w will transform into a X will trance firm or shift to be. Why will transform to C and Z to Dee and Dennis are cipher right here. So now, for example, if I want to encrypt this text, it will look like this. So we grab a and turn it into E, Then we grab Z and turn it into D and so on. So how would the encryption and decryption function look like? Well, in our case, we shift by four. Right, But in general case, you can shift by any number, and the number by which you are shifting is the key. So let's say key is our key and it is just some number. Then the encrypted letter would look like this X passkey module. 0 26 Why 26? Well, we have 26 letters in our alphabet, right? So this will take care of the problem I talked about before. We don't have a letter after Z, but now let's take a look at the decrypting function. So now we got the cipher text, and we want to decrypt it into plain text into original message if you want. So how do we do that? Feel free to pause the video and try to figure it out Once again. It is not so hard. You need to think about how did we change the original character? We edit gay to it, right? So to get the original message, we just subtract the key. But we also need to do model 26 because, for example, if I want to encrypt X, I will turn it and to be right. But now, if I subtract from before and it will result in two minus free and minus free isn't a letter. So I need to do module 26 minus free module 26 is 20 free, right? And 20 free is associate ID with X. So we get our original character and that is basically how Caesar Cipher works. Now, do you think this cipher is safe? No, it is not. First of all, you can do frequency analysis like we did in previous lecture, but this time it is even easier. Would a brute force attack work? Yes. Simple brute force attack Would brag. Decipher why? Well, how many value skin key have some of you might say infinite? That is true. But now I wanted to notice one thing. What is the difference between K equal to 27 K equal to one? You can once again pause the video. The answer is there is absolutely no different. It is the same cipher. Why? Because 27 Modelo 26 is one and one. Module 26 is also one. So we are actually shifting by one. Both dives Another interesting fact is that we can shift on Lee by 25 numbers because shifting by zero doesn't make sense. Right, You would get the same character, so you need to go through only 25 options in order for you to find a key. And that is not a lot of work. So this algorithm is not secure. But it is one of the simplest one, and humans use it for hundreds of years. So I thought it might be nice to talk about it. But anyway, if you have any questions, feel free to ask, and I will soon exam. 8. CODE CaesarCipher: Well, hello, gas and welcome to his lecture. In this lecture, I will show you how to implement Caesar cipher in Java and also decode I will wrote I will add as a resource to this video so you don't have to write the code with me. You can just download it that if you want to write it with me, no problem. It's only up to you. So you may notice that the encrypt and decrypt function are missing. So I'm gonna go ahead and write them so public static string with uppercase s So the encrypting function will return a string, OK? And it will take a as the argument string and that will be the message and also and okay like this. So what I'm gonna do Dow is create a new string variable called Result and initialize it to empty string. OK, and now I'm going to go through all of the characters in the message and for each of them, I will compute the cipher and then I will add the computer character to the result I hope does make sense. So for in teacher I equal to zero, I will loop while I is smaller den, the length off my message like this and I will incremental I and for each character actually, let me do it like this. So character see will be equal to message and car at Index I Okay, so now what I'm going to do with this character. Well, first of all, I will check whether it is a character, because I don't I don't want to encrypt a space or a number. I want to encrypt Onley characters. So I will do character dot ists leather and see Okay, so this is character. Okay? This is already defined function and out past See as a argument and it will return true if it is a leather and if it is a space or something like that, it will return falls. So in here What I'm going to do is set see to Okay, okay, I'll I will just plus equal he So now C is equal to the encrypted character. Okay, but I still need to check whether I'm not behind Z right. So I will need to check whether the character character Okay, I can write the ISS lower case on and it will be I want the original character, right? So I will just do message car at index I and and right if it is lower case and and the see iss bigger than the character Z like this, right? That means I overflow right? I'm oversee So I need to do something. But I also need to check whether the character IHS upper case out bass d same character as a argument do this function message. So, like this and the same thing. But this time, whether C is bigger than uppercase Z. Okay, so I need to Andy braces on day. There is something wrong with this bet. Operate type for binary operator. And okay, I forget to close the braces in here and in a minute. No, she here should be racist. So this is braces one on. This should not be here like this. So now if this condition is true, that means the character is over floating right? It is. I'm behind Z. So what I'm going to do now? Well, I will just simply subtract from the character 26 because we have 26 characters, right? And that's it. Because when I went over Z behind Z, I need to get back the bagged the number of characters, right, To get to a or something like that, depending on key. I hope this makes sense. So now what I'm going to do is just simply at to the result, my character like this. And then I need to return the result. So now let me ride D decrypt function and actually let me just copy paste it because it will be a lot similar. So instead, off encrypt, we will have the crypt on. It still takes message and key. It's still have a result. And we still group for every character. Okay, we checked. Ready character is leather. But now if it is leather, we need to subtract the key ride. And then when we check whether the character is over floating, we need to check whether the character is smaller than a same Goes here about Onley upper case A like this. And if it does how at 26 I hope, does make sense. So let me show you that this actually works. If I run this program, hopefully it will compile. Okay, so yeah, it may take a while. Body. It seemed like it runs and as you can see, it works. So the original message Waas Hello, world. The encrypted message is something and the decrypted message is once again Hello, world. So this is basically how you can create a or implement a Caesar cipher. So now let me show you how you can simply break this cipher. So it will not be a actual working example bod. It will be just the idea. Okay, so it will return a string and I will call it. Just break the Cypher like this. It will accept a strength message. And in here for let's say indeed your I equal to zero. Why, while I iss smaller than 26 I will increments I So this will be a brute force attack. It will not be a animalistic attack. So 26 because there is 26 possible keys, right? And I can start actually from one and in here I will do basically the same. So same s here, right? This part of gold own at this time The key. Okay, let me just rename it to G because I you I use hi, Andy in the and instead of G, I will have G right, and I also need to define the result variable and so on. But the idea is that I tried us for every G, and at the end, I pretty much check whether the character or the the message is correct. And that's it. Whether the message makes sense, right, and that's pretty much it. That's how you break the Caesar cipher. So if you have any questions, feel free to ask, and I will soon exam. 9. Affine Cipher: So what is a thin cipher? Well, it is once again one of these simply or wants that is operating with letters. But in this case are key is made up from two numbers, and the encryption function is defined like this. So let's say that X is a leather in our plain text, and I is the letter in cipher text. So I will be equal to a Times X plus B and, of course, module 26 because we have 26 characters, right? So that is the encryption rule. Now, I want you to pause the video and try to figure out how d decryption rule would look like. And don't worry, if you get stuck, I will be here to help you. So in order to figure out how the decryption would look like, we just simply need to transform this equation so that we have X on one side, and then I and the rest on other side. So first of all, we grabbed this bus B and turn it on the other side. So we get I minus B, and then we need to get rid off eight times X, and we do that by dividing this equation by a So we get this and dad is our decryption function. But now let's take a look at how many keys are in this script of system. So this step it is not so easy. The B can have 26 values, right? Because this time it can be even zero because we also multiply. But now comes the hard question. How many options do we have for a Well, this time? It is not so easy. And it might seem like there is a infinite number off Ace's right that once again, the same rule applies. If you multiply the key by two and 28 then do module Oh, you will get the exact same number. So this time the number of keys ISS slightly bigger, but it is still pretty small number, so brute force attack would work. Also, the frequency analysis would work because if you take a look at this equation, it is still consistent or static. In other words, for some X, you will always get the same I so that means the frequency property is preserved. Even though you have, this may be a bit complicated formula So that is it for this lecture. If you have any questions, feel free to ask and I will soon exam. 10. CODE AffineCipher: Well, hello, gas. And welcome to this lecture and this lecture. I will talk about a fiend staffer so I don't want to make things boring. And this cipher, it's pretty much the same s the cipher's that I talked about in previous lectures. So I maybe make this one a bit faster. So let's get started. I right here, Beverly ecstatic string and crypt because the function and grits And it accepts the message the A and the B part off our key OK, aesthetic. And I also defining variable called result and I initialize it to empty string. Now, for every I from zero, while a smaller dad message dot length, I will increment I and what I will do is stored the character so message car at I And then I will also check whether it is a leather first. So character dot is leather and out pass see into dysfunction or method. And now we usually do this stuff with upper case and lower case letter right. But we can also use the the inland if if you want so car and I'll just go, this offset and it will be equal to the value will be initialized depending on the weather , D. C is an upper case or lower case. So if I ready a director this upper case and Alba Sea or not being spastic, see for me. Then I'll said this variable to a If it is not, I'll set it too small. A Okay, So I hope I hope you understand this. Now I get the offset. And now I need to decrypt. So what I will do is I'll I will simply said see to the a times see C minus offset. Okay, so that is how we get the index of our character. Right? Remember, from the Explanation video, we use characters like 0 to 25. But actually the character have value like 40. So we need to get the value from 0 to 25. Ok, then I will do plus B and I will also do. Let me just put this into embraces. And and this also. So now I will also do module. Oh, Majuro. Damn, it's can write today Monjo 26 And I'll also add De Offset offset like this. So I am missing Brent to see somewhere. Probably here. And I also need to gas this into character, right? Because this is in general, some in teacher value, and I still have a right, so this should work. And now all we need to do is at the character to our reason out at the character to our results. So results will be plus equal C, and we also need to return the result like this. So this is our encryption I'll just go to for method. And now let's take a look at decryption So public static string decrypt. And this function is maybe a bit complicated, but I'll try to explain it as best as I can. So it except once again, the key a B and no, we need to compute something called inverse inverse multiplication Inverse off a. And why? Because we did module operation here. So we cannot just divide the character right? And we need we need to multiply by a to the minus one, and we will do that by I'll just initialize some variable called in ever in Hvar, I guess just enough That's better, OK, and also initializing very big called them because it is temporary and also D result like this and now I need to compute the inverse value off a And it is defined as if the inverse if a time some value is Model 26 is equal to one. The value the some value is equal to inverse. Okay, so I'll just write cycle. And for I equal to zero while I smaller than 26 I will increment I And then I will just set the temp value. Okay, to the result off a time, Cy, that is the something Margallo 26 and no, I will check whether the temp is equal to one. If it is, I will set the inverse valley the in nova variable to want to I sorry. And because I is the number that we are looking for because we want this equation to be one . So when this equation is equal to one eye is the inverse in verse multiplication. I've off a I hope this kind of makes sense, right? So now all we need to do It's pretty much the same. So for every character. So l just right in I equal to zero. Why? Ah, it's smaller than message length and I'll increment I and then Al just story character. So message car at hi on. And now I'll check whether it is a character, because we don't want to decrypt a a space or something like that. So whether it this a letter And now I also need the offset offset variable, And it will be equal to Allah. Just copy, paste this. That will be faster, right? So same as before. And now I will just compute a kind of I'll stand. I'll actually said it to tempt. Okay, so this will be something like middle calculation, like temporary calculation. And we will use that in the final results like here. But anyway, it will be inverse times. It's this is the the decryption, the decryption function that you saw in an explanation video. So it will be C minus the offset. Right. So we need the the the origin of attic D from the value from 0 to 25 and minus B, right, as the formula says. But plus 26 because we did module in here in about about anyway. Now we got this temp and all we need to do now is compute. The result will be into care because we this is into variable and we need character and it will be the temp. Modelo 26 blasts offset. So yeah, seven coin and right. So plus offset to get bagged the original number, right and module or general character story and module Because we want a character we want vary from 0 to 25. Otherwise we wouldn't get a character. So now all we need to do is just added to the results. So results plus equal C and then we need to return de result like this and hopefully this should work. So as usual, I already wrote some brain thing. Let's see whether it works and actually the dust. So hello, world something. Hello world. That's pretty much it for this video. If you have any questions, feel free to ask and I will soon extend 11. Stream Encryption: So, first of all, I want to talk about the difference between stream and block ciphers. Imagine that you have a phone call, so you pick up your phone and kill some number and D data are transmitted as a stream, right. It is a stream off data coming from one two other device. So when we want to encrypt something like this, something like phone conversation, we use stream cipher And what stream cipher? Dust? Well, it encrypts bits individually, so it basically takes one bit encrypted, then grabs another one encrypted and then another one and so on and so on where, on the other hand, the block cipher takes a block off data like 128 bits and encrypt whole block at once. Then it moves on to another one. And since we need to encrypt only one bid, we can keep the encrypting and decrypting where simple for example, like this. So we take one bed. That means this X on index I that is one bit from stream acts ok, similarly with gay. So Keyon and Excite is basically bit on index I from key. I hope that this makes sense and Since we are dealing with bits, the two values can only be zero or one. Right. So what do we do? We do module. 02 And then way we do our encryption and it is really similar to one cipher that you already saw in discuss. You may think about which one it iss if you thought the Caesar cipher. You are right. It is actually similar to Caesar Cipher, but this one, it's more secure. Now, you might notice that when decrypting you can see that we add the bit on index I from Key. You might expect a minus right in Caesar cipher. We had minus. So why is there a plus sign? Well, remember that we are talking about Byner system, so only one and zero so the decryption can be your average in like this. So I changed this part and turn it into this equation, and I can do that because they are equal, right? So what do I get? I get that when I want decrypt I It is actually equal to bit from X on index I and two times bit on index I from our key. But now I want you to go through the cases. That can happen. The first case is that bit from key on index I zero. Then we get bit on index I from X, and that is the actual result When you wanted right when decrypting we want to turn the eye into X because I is the cipher and X is d playing text ride. So we just saw that it works for zero. But does it work for a one when bit on index I from key is equal to one, we get a bit on index I from X plus two, right that since we do module oh to the result is just bit on index I from X and since every bit can either be one or zero these works for every case. So the plus sign is correct. I hope this makes sense, but let's get back to the example. If you talk, it is transformed into stream off bits. Somehow I won't bore you with how it is done. The important thing is that as I speak, I get bits and we can index them. So this is a bit x one. This is a bit x two and so on. And we also have a key and it is also a stream off bits. So we have key one key to and so on. Then we add these and do Modelo too. And there is actually a sign for this operation in cryptography since it is a pretty common operation. So this Sinus saying at two bits on input and do module Oh too Okay. And on the receiving end, we are decrypting we do the same. So we have the same key and key, one key to and so on. And we also have a stream off eyes. So I want I to and we get from that the orginal x one and x two and so on a ride. So that is basically how eight telephone communication is encrypted. Now, this is really important, so please pay attention. Do you know what is a X or operation? It is a binary operation. If yes then great But still police bait attention Don't skip this part. It's really important. So what are the possible values off input? Well, the bid on index I from X can be either true or false, right, and the bit on index I from Key. It's also either true or false, so we can put that into table like this. This way we get all possible combinations. Now here comes the X or operation and what X are basically thus? Well, when the bits on input are the same, the X or operation will be force. But when the bits are different, the X or operation is true. So it looked like this. Why? Well, zero and zero is the same. So we get false zero and one is different. So we get one. Same goes for one and zero and then one and one is the same. So we get zero again. I think this should be clear, but now try to do the same. But this time used the encryption equation so bit from X on index, I plus bet from key on Index I module. Oh, to what results do get. I suggest you pause the video and try to figure it out. So let's do this one line after another. On the first line we get zero plus zero Majuro to and that is just zero. Right? Then we get zero plus one macho to and that is just one right. Then we get one plus zero Majuro to, and that is also one. And lastly, we get one pass, one macho too. So that is two module to and that is 20 red. And now you may notice something. This equation is basically bit from X on index I ex sore bit from key on Index I and, yes, X or is the same as macho to addiction. So now you might ask, why x or or why not some other function, leg and or negation or negation? And so let's take a look at the table. If our plain text have value of zero, then based on the value off key, it can either be true or false. So that is a property you absolutely need in order for safer to work. Why? Well, if the bit would be zero all the time, the cipher text would be the same as the plain text. So you need to have the possibility of both outcomes. You need to be able to transfer it to both true and false. Same girls with one. If you are plain text is one. The encryption can also result into true or false based on the value of key. So now imagine that the key is somehow random. It seems like we have a reasonable encryption function, right? And I want to touch on another fact. I wanted to know this. What is happening when the key bit zero. If you look at the table, what can you see? The bit stays the same, right? Zero x or zero is still zero and one x or zero. This one. So that is a interesting fact. Now I want you to also notice what is happening when the key bit, it's one. If you once again look at the stable, you can see that it flips the value, right? It kind of do the negation. And now, if you look at the communication, I want to show you that it actually works. So imagine that we have zero as key one. Then what happens? Well, X one goes into this X or operation and since key 10 it stays the same ride. So then when we decrypt, we get the same axe on input and key one is still zero because the skis are the same. So what do we yet once again we get the same value and debt is X one. Now imagine that key one is one. So when x one goes to encrypt, where do we get we get a negation off X one ride and then when we decrypt, we get negation off X one on input and key one is still one. So we negate the value of X one once again, and we get double negation off X one. But if you already have disagreed, math or algebra or something like that, you know that double negation off X one is just x one because you basically flip the value two times and firm this fact, you can kind of figure out what I'm going to show you right now. We get back to our truth table. I want you to try to do X or with I and key. So basically, do X or off these two columns, what do you get once again, feel free to pause the video and tried to do this on your own. So you get the original X ray. So what is the point in here? Well, if you do x or off some stream with X you get, decipher. And then if you do X or off debt cipher with the same key, you get back d plain text. So I hope this makes sense, and I think it does. But anyway, if you don't understand something, please contact me. Just posed a question and I will get back to you as soon as I can with an answer. So you will be smarter, hopefully, and I will be happier. So with that being said, I will soon extend. 12. CODE XorCipher: Hello, gas send. Welcome to this video and this video. I will show you how you can use sore Operation X or operation to encrypt and decrypt. So this encryption and decryption will be a bit different because we will use message and password. So keep basically that right. Let me get started. So now, Blake static and the crypt. And now all just right. String message and string password. Okay. And races. What's wrong? Yeah, it will return strength. And so I defined, as usual variable results, and I'll set it to empty string. And for every character as usual. So I equals zero. Why? Well, I And now comes the interesting thing. Maybe So you have a message, and you have a password. What do you think will be shorter? Well, the password, Right. So you need to live through the message, right? Because message is the thing that you want to encrypt. So while message length okay and all incremental. Same thing. Now, what I will do is just great A as usual Karsi and I will actually do this thing so P or password houses rename it to key. That's better. So he and in here key dot We are at bad, right? I touched on this. So probably the key is smaller and we are talking about the length off message. So we need We cannot index by I because we would get behind the key, right? We would overflow. So we need to do module Oh, right here. And we will module dead by the key length. Okay, I hope does make sense. So we will kind a loop through the key. So example for for five characters imagine that we have 10 character message and five character key So 1st 5 characters will be encrypted by key. And then the 2nd 5 characters will be encrypted also by the same key. So first and sixth character will be encrypted by same character I hope does make sense. So now what I will do is set it to the X or operation. Okay, that's not the X operation. This is Xer operation, right? It's like the I don't know something hens our exhorted with the inputs car so message and car at index site. And now I also need to cast this into care, right, because we want character and this value is not a character use binary operation here. So all we need to do now is simply add it to the results. So result plus equal c and then we need to return the result. So return results like this and well, I will do now is right. No, does that. I didn't care about whether it is character in the message and so on and so on. Right? And you could you could do that. But I just don't water Don't want to bother you with that when it is not necessary. Right? I'm sure you are able to do this on your own. So now how do you think the decryption would look like? Just maybe pose the video and think about it? Because right from the explanation, new should know that if you do like sore operation with key, you get some I And then if you do sort operation with that, I you get the key. So what we are doing here? We duke sore operation with the message, right? We saw our key into message. And what if instead of the message would be the cipher decipher text, Would it work? Well, yeah, it would. So instead of this decrypt I can just write Encrypt, Right? So this is maybe a bit weird, but as you will see, it works right. Hello, World Some complete nonsense and hello world. So that's pretty much how this soar encrypting works. It is pretty simple, right? Is just like six lines and that's it for this video. If you have any questions, feel free to ask and I will soon exam. 13. Random Nimber Generators: So now you know how de encryption and decryption looks like. We basically just at G indexed, and that's it. That now you might ask. But where do we get the key? Right? The key is the probably most important part, and I do think it is a good idea to have some kind of repeating key. Let's say 101010 and so on. Well, no, because the attacker would came up with that key ride. So what we are looking for when we generate a key, we want the key to be random, right? Not repeating, not static, random. So let's talk about rend um, number generators. There are few glasses off these generators. 1st 1 is the true random number generator, and the easiest example pretty much everyone would give you is the corn flipping. When you flip a coin, it is a random outcome in this, either hats or tails that it is one or other, and it is random, unless you are some pro at corn flipping and you can get 10 hats in a row. Another example I can give you is the dice rolling. Same thing here. These are examples off true random generator. And this generating is dependent on some physical aspect, like flipping a coin that is a physical process, right? So this true random number generator is also in computer, and there are a plenty off random generators. For example, you can measure the time between key press and then we'll give you a random number because obviously keep pressing is random some of us right faster, some slower. Some words are easier to write, some some nose and so on and so on. So this is a example of a true random generator. So with true random generators, it is pretty much impossible to get the same number. When I flip acorn 1000 times, I get some random results, and I bet you cannot duplicate these results, right? But sometimes we wanted to placate the results. So dead is Wipe Silda. Random number generator exists, and the thing about that is that there are not the really random generators. They pretend to be random, but they are not. These numbers are computed. They are not based on any physical process. They are just computed numbers, and they're often computed like this. The first number is true random number. So that is some number based on the physical process and it is random. This number is called seed. And then there is this recursive formula and what it says, Well, the next I is just the result off some function applied to previous I. This may sound confusing So let me show you an example. If you ever wrote a program in C, you probably know dat There is something called rent function Random function. This function returns ape soda, random in teacher and it is defined like this. So we have first number that it's our seat and it is just 12345 and then every other number is computed like this. So we have some terrible constant time skate I plus Ki zero. And since this function shoot, return and in teacher, we get the final result by doing model 02 to the power off 31. Because in teacher have 32 bits about 1st 1 is for sign. So 31 So now it might be tempting to use these solder and numbers and get our key, but we cannot do that. I will explain why in a minute. We actually need a additional property. And that leads us to third group off random number generators. And these are cryptographic Lee secured random numbers and what they are. Well, they are soda, random generated numbers. But they have the additional property. The numbers are unpredictable. So what does that mean? Well, for example, if I give you 100 bits so G zero to G 99 you are the attacker, if you can came up with the key 100 these sequence is not cryptographic lee secured. But if you cannot compute the K 100 this sequence is unpredictable. And that means it's cryptographic lee secure sequence. So these are the types off randomly generated numbers. I hope you understand the soda. Random generated numbers are pretty much useless in cryptographic. We need a cryptographic lee secured soda, random generated numbers. And that is it for this lecture. If you have any questions, feel free to ask and I will soon exam 14. First Secure Cipher: Hello, Gas and this lecture. I will talk about the secured ciphers, and I will show you our first secured cipher. So how do we define a secure cipher? Well, it is easy. It is safer that it's unbreakable. Right? But we at something to this statement. It is unbreakable, even if you have unlimited computing power. So, for example, if you create a safer and you have a two to the power off 10,000 geese, there is no way that you would find a computer on this planet that could do brute force attack. But still, this cipher is not secure, because in the definition, we are assuming that we have unlimited computing power. So this would not be a secure cipher, I hope does make sense. Now in our first secure cipher, every bit from key stream will be true. Random generated number and each key stream bit is used on Lee once. So what that means. Well, imagine I'm rolling a dice and I get some numbers and imagine that I will roll the dice like 100 times. So for simplicity, let's say that I get key 12 key, 100. So now I can encrypt X one to x 100 the cipher text is basically unbreakable. If let's say FBI listens to my communication, they will not be able to break this cipher even with their super computers. So that is pretty cool, right? But the receiver still needs to know the key, right, that 100 bits that I just generate. Without it, he won't be able to decrypt the message. So this is secure and simple. But there is a drawback with this approach. The key ISS, same length as the message, and that is a huge drawback. Imagine that you have a one gigabyte file, and to transmit the message you need to gigabytes because one gigabyte is the key. And the 2nd 1 is the message that is just soo much, right? So what do he do? Well, we just use a different key generator. For example, let's try it now with some key stream generated wipes, odor, random generator. So you boot B R N G. That's so do random number generator, and he is producing a stream off G bits. So key one key to all the way out to Key and and you put the same b r N g to the receiver side. So you have tube. So do random number generators. But now the problem is that the spy can also use the same soda random number generator and he will decrypt the message. Right? So in order to prevent that, we add a key to deep soda random number generator, and then we will use the same key when decrypting. Then way, we kind of secure the communication without too much overhead. But now I want you to think about what is wrong with this. Is it really secure? Well, it depends on the key, right? And since this is so door under, um, number generator, you can think of it as the rent function from C. And if you remember correctly, it was defined as a times the previous element Bus B. And from that you can figure out that G is a and B right? So now I want to show you how the attacker can break it. Imagine that attacker nose first few bits off plain text. So, for example, he knows X gone x two and X free. These are free bits. A single character have eight bits. So we are good, right? No, As a matter effect, we are not. Why? Well, attacker also knows decrypted text, right? So what he can do, he can guider reverse the X or operation and get the key. So now the attacker knows the first free bits off our key. And that is a problem Because, for example, key to is equal to a time ski one plus B module om. So this is a good start for the attacker because now he has a linear equation. But there are two unknowns A and B right? And one equation with two unknowns have no solution. The problem is, there is also a second equation, and that is key free is equal to a time ski two plus B. And now he can solve this equation and figure out what A and B are. So he broke Decipher in the following lectures, I will talk more about this. But in this lecture I showed you a first secure encryption system where you generate true random number for every bit. It is a bit unpractical, but it works. It is actually the first unbreakable cipher in this curse. So that is pretty much it. If you have any questions, feel free to ask and I will soon exam 15. AES: Hello, gas and welcome to dis lecture. In this lecture, I will talk about A s. So let's get started. I will start with a brief history off A Yes. Guess who triggered the creation of a yes, the US government. Because Davor hacked And they said we need new ANC ripping algorithm and dead waas in year 90 97 and somehow they get 15 algorithms and then year after they test them somehow and get on Leath, I've best algorithms and all of these algorithms are safe and they are still around that they are not the A s. And then in the year 2000 1 algorithm called Ryan the job. I guess that was designed by two Belgian cryptographic hours become the A s advanced encryption standard. So this is a brief history. Let's take a look at how it works. So a s is a block cipher. That means it takes a block of data and encrypt this block. So they're 128 bits on input. But the interesting thing is that the key at free, possible length 1st 1 and the probably most used one was the 128 bit length off key. Then, if you want more security, you can use 192 bits, and if you want to be super secure, you can use 256 bit. But for normal people like me, a new 128 is just five. Maybe if you are some spy, you can use more. But if you are not 128 is enough. So why am I talking about a Yes as firmly name It is the standard, and by now it is probably the most used and probably most important symmetric algorithm in the world. And, for example, the American intelligence agencies use this encryption, so it must be probably pretty secure. Or at least they believe it is pretty secure. So now let's take a look at how D A S works. The encryption and decryption is Candice plaited into rounds, where the number of rounds is dependent on key size. For example, if you have a 128 bit key, you need 10 rounds. If you have 192 bit key, you need 12 rounds, and if you have 256 bit key, you need 14 rounds. So you are kind of trading security for speed because obviously 10 rounds will be faster than 14 Right? So, in each round you do this, you basically add the round G, then use substitute bites. She throws mixed columns and that is one round. Okay, so you do that with one part of G, then you grab next part of key and do the same. And when you reach last round, the only substitute bites shift rose and add the key. That is the only difference. Now, as I said before, you grab 128 bits off input and you can turn it actually into four by four matrix. And in each cell you have one bite, so eight bits and same goes for a key. He's also 128 bits long and you represent these bits by a four by four matrix also de around Key, which is just the ski 02 Key 10 are obviously coming from the 128 bet Key and each round key have length off 16 bytes. So they are a four by four matrix, that is, ex soared into the input matrix, so That is pretty much how a ES works. No, I will also explain these substitution. So as the name suggests, substitution means replacement. So it is pretty simple. You have something called as box, which is a big table, and it is filled with some bites. And and the moment it is not so important. How is it represented? And what is the actual content? Just imagine that you have a big table called as box. Now what do you do? Well, you take a one cell from your four by four input matrix. So that is eight bytes. And you use these eight bytes to look up the value. So you use these eight bytes as a index, right? And the value associate it with this index is in as books will be substituted. So you kind of grab the value and store it in the cell where you took your index key, right? And you do this with every cell in your inbred matrix. So now let me explain more Precisely. How do you look? Up works. You have your aid bites and you have a table. So you split these eight bites into four and four and you take the 1st 4 and compute the row from it. And then you take the 2nd 4 and compute column, and this will lead you to some cell in the icebox, and then you just grab it and replace the original cell in your four by four input matrix. I hope this is clear. Now, let's take a look at how the shifting off Rose works. It s pretty simple. So you have some four by four matrix, and what you do is you shift cells in 2nd 3rd and fourth row. The first world stays the same in second row. You are shifting by one element. And what do I mean by shift? Well, it is pretty simple operation. Imagine that you bush from right side off the road at you push one element out. Okay, Now you grabbed his arm meant and put it at D end, or in other words, a d space. So that is basically how you shift the second row. Then you should the third row by two. So you push from, writes ad again, and you push out two elements and then you put them into D space you created by pushing the rope. And with fourth row, you do the same. Only this time you push out free elements, so it will look like this. I hope you understand this. And the third step is that you take the input matrix and you multiply it by some other matrix. And how exactly do multiply it? Well, you have a Gunson matrix, right? So let's say that the Callum is B zero b one, B two and B free and the output S C zero, c one, C two and C free. No. How do you get D. C Calum? Well, it is actually pretty simple. You just multiply the B column by this matrix. So this is a constant matrix. And the interesting thing is that this matrix is once again shifted because the second row is the first row shifted by one. Right. The third row is basically second row shifted by one and four throw iss. 1/3 row shifted by one. So now let's take a look at how the multiplication works. So, for example, C zero will be equal to zero to times B zero plus zero free times B one plus 01 times B two and 01 times be free and everything in here is a bite. So this is a bite. This is a bite and this is a bad So how do you multiply it in order to get a bite? Well, you do something called Gallup multiplication. So you turned these eight bits into a pollen a meal? And how do we transform bite into a bona meal? Well, simply by writing the Paulina meals under this by so at the least significant bit, we have extra the power of zero. Then we have extra the power of one and all the way up to extradite power of seven. Now, from this you get the polynomial by adding on lee the excess that corresponds with one. So, for example, this bite will be X plus one. Why the Yes, we check. Where do you have once So there is a one at the least significant bit, right. So we write X to the power of zero, and that is just one. Then there is one at the second bit and this bit corresponds with X to the power of one. So that is se X, and all the other bits are zero. So the resolve this ex buzz one. If, for example, the most significant bit would be one you would add X studi borrow seven. Okay, I hope this makes sense. And now it iss simple. You just do born a mill multiplication. And that is how you get your c zero and you do the same for C one C two and C free. And that is how the mixed column works. Now, how do we decrypt? Well, it is actually pretty straightforward. You can pause the video and try to figure it out. We simply decrypt by doing the same Onley in opposite order. So when encrypting we went from left to right went decrypting we simply g o from right to left. And it kind of makes sense, right? So that is pretty much it for dis lecture. If you have any questions, feel free to ask and I will soon exam 16. RSA: Now you already know how the decryption and encryption works. That there is still the problem off key sharing how to send a key to receiver without the spy catching Dickie behind. His solution to this problem is a really clever idea imagined that I buy a lock and keep the key. Then I sent you the opened law and you used his lock to lock a message. Okay? And then you send me the locked message. And since I have the key, I can open it up and read the message and the spy can on Leigh read some locked message, but he doesn't have the key. So we are safe right now what I can do. Well, I can publish the luck somewhere and let anyone in the world used this lock in order to send me a message. And I still have one key, and nobody in the entire world have the same key. So I am secure because I am the only one who can open the message I hope does make sense. This is the idea. So now you may notice one thing, and that is that if you recall Caesar cipher, we used same key to encrypt and decrypt, right? And this is called symmetric cipher. So we used the same key to encrypt and decrypt, but the air s a uses something called asymmetric encryption. Now let's look at how it actually works. The key is split it into two. First is the encryption and then decryption key. And the important thing to note is that you can't guess wanke from another. You can't do that. So if I, for example, have key A there is no way to somehow figure out what the key be. ISS and also D skis are designed in such a way that anything you and gripped with G A can be Onley decrypted with G b and anything you decrypt with G B can be Onley encrypted with G a. Okay, so what do you do is generate these two keys and then big one off them as a public key. Now, were does public key mean well, you make it public, for example, you put it at the end of your email on your website or whatever. The thing is that it is public. So it is all over the world. The second part of your key is private, so you keep this key only to yourself. You don't share it anywhere. So now the idea is that the public e is used for encryption. Andy, Private key is used for decryption. So now when you want to send me a message, all you need to do is encrypted with my public key and send it that way. When Spy iss listening to our conversation, he won't be able to decrypt the message because he needed private key. And he cannot compute deprived key from public e. So we are safe. The only person that can decrypt the message is the person that has the private key. And guess who that is. Me. So this is pretty simple and smart, right? But now you might think OK, but how do I sent an email to you? I cannot decrypt the message using my private key. So what do I do? Well, you also have a private and public key, so I do the same on Lee. This time I encrypt the message with your public G and send it. It is a simple as that And another thing why this is useful is that if I d crib. The message with my private G. The message can be encrypted with my public key. Right? So it is not secure. But still this can be useful. Why? Well, I am the only person who has my private key. Okay, so the effect that you can decrypt a message with my public e means it must have been the encrypted with my private key, right? And once again, I'm the only person having the private key. So this can work as a signature, right? When I encrypt the message with my private key, you know that it is me talking to you. So I kind of signed the message. So the best thing we can do in order for secure communication is this. Imagine that I'm writing to you. So I great a message. Then I encrypt it with my private key. That way I kind of signed the email. Then I encrypt it again with your public key. So the message can be decrypted Onley by your private key. And I send this message. If Spy gets the message, he cannot open it because he would need your private G. Right? So when you get the message you first decrypted by your private key. Then way you get to the message that was encrypted by my private key. And all you need to do now is to decrypt this message using my public key. So now you get the email. The email will say the attacker didn't get to the content. And you know that I send this email because I can assigned it by my private key. So that is pretty much how public key cryptography works. I hope you understand this. If you don't ask me a question and I will see you next time.