 In our previous video we introduced the idea of symmetric key cryptography which is a method of encrypting and decrypting plain text messages. You can encrypt it to ciphertext and then you'll decrypt the ciphertext back to plain text. Symmetric key cryptography is about encrypting information so that Alice can share it with Bob and with the assumption that Alice and Bob have the same key and that any eavesdropper aka eve would not be able to decrypt the message because she doesn't have the key. Now one of the earliest examples in history of the use of symmetric key cryptography, at least in a more modern sense of how cryptography is viewed, is what's commonly referred to as the Caesar cipher, named after Julius Cever himself back in the days of ancient Rome. So the idea of the Caesar cipher is you would write down a phrase in Latin, so you might say something like Vinny Vicky Vitti, like something like I came I saw I conquered, something like that. I might be butchering the Latin right here. So you take this message, so this is just the plain text, and then you're going to shift the letters in the alphabet by like say one. So you might shift our two or whatever right, so you might replace okay we're going to go up in the alphabet so V will become a U I will be then come what becomes before I H what becomes for N M what becomes before I the H again right and then you get U H B H you get the idea here U H C H so I just what I did is I just took each letter each letter and you shifted it. You took your original letter you just shifted it to the left by one so if you think of it like as a number the idea is we just shifted everything to X minus one we went to its predecessor. But what happens if you're beginning the alphabet if you're at if you're at the letter A you would then shift back to the end of the alphabet to be Z and so essentially you just shifted everything to left working mod 26 the alphabet are being the that's 26 being the number of letters in our alphabet. Now I should mention that in the Roman the original Roman alphabet when they were speaking Latin they didn't have all the same letters that we do in English but that's beside the point we'd be working we'd be shifting things according to basically addition or subtraction mod the number of letters we have in our alphabet it's a very basic idea but this truly befuddled the goals I mean honestly Latin is hard enough to speak for the native Romans themselves I took Latin for two years it was a brutal language it is and so the barbarians were definitely perplexed when they got these messages they intercepted them they had no idea what Caesar's generals were talking about because even if they knew Latin enough to understand it this was just a bunch of gibberish even in the Latin language and so this this confused them and it was a strategic advantage for for the Romans in their conquest of Europe at the time and North Africa as well so let's let's look at another example of this thing here so let's suppose that Alice wants to use the Caesar cipher with a shift of three which by the way shifting three to the right was actually the method that Caesar himself used with this with the now called Caesar cipher and imagine Alice wants to send the message we write at noon to Bob I always love these lessons on cryptography because the message was encrypt they always have this like dark sinister tone to it we write at noon what would that mean we don't actually know the context of it Alice and Bob are trying to say here but it definitely does sound insidious right we write at noon but this is the message that she wants Bob to receive but she doesn't want Eve to receive so what she's gonna do is she's going to encrypt it now she's gonna encrypt it using this method of the Caesar cipher and so I actually have some magma code set up to compute this exact same thing and I'll show you to you in just a second so you use the command Caesar encrypt Caesar encrypt takes three parameters the first one will be the plain text message it should be something with this regular characters or in terms of the computer language this should be a string it'll be it'll be preceded and followed by quotation marks following the quotation marks you have a comma to represent the next parameter for the case of the Caesar cipher this parameter is gonna be a one we'll talk about in the next video what happens if you change this parameter but for Caesar encryption put in the number one right there and then the last number will be the shift that you want to do and this will always consider this a shift to the right so we'll shift by three which admittedly as we're working on 26 if you want to a shift to the left by one you can put in a negative one and it should compile just fine and so if you run the Caesar encrypt code on magma it'll send you the following message Zulu go back to do what cook it's kind of almost sounds like something like the the Swedish chef would say something and so the Caesar encrypt it shakes this plain text message and gives you a bunch of gibberish right which if anyone sees this they're gonna have no idea what this is saying all right so how did it work well the basic idea behind the Caesar encrypt algorithm when you go towards the magma code like I said it takes in three parameters the plain text a will talk about a little bit later and then B is our shift so the very first thing it does is it's going to take the plain text and it's going to turn it into a string of numbers using the digitized command we had talked about in the previous video and so then initializes our cipher text C just by taking an empty array okay and so then what happens is that it's going to run a for loop a for loop meaning that's gonna it's gonna perform the same operation over and over as it goes from one to the length of P so in magma pound P this is the length of the array so for each letter in the plain text is going to perform the following operation so you're gonna take a times PI plus B PI right here so since P is an array P bracket some index there gives gonna grab the ith entry of that array so you're gonna times it by a which of course is one so it's doing nothing in the situation you're gonna times it by a and they're gonna add the number B to it so since PI is a number we're gonna add B to it and then we're gonna reduce that number mod 26 we're then going to add that number to C since C starts off as an empty array cat right here means we're gonna concatenate this array with a single entry to it so we're just gonna add it to the end of our array and build it up so we're gonna get an array of numbers and then at the end we alphabetize and we turn that array of numbers back into letters and so in the end product as we get this Swedish chef comment right here and it's gonna be just but just random random information which then the shift is hidden inside of that thing and so there we go so that's that's how we encrypted this thing well I'll show you how to do this in magma in just one second so perhaps to the contrary though Alice is concerned that if she sends the message we write at noon although we can see spacing we can see capitalization our digitized command ignore spacing and it ignores capitalization so and lowercase letters as well it treats everything as an uppercase letter so when Bob decrypts this and we'll talk exactly how to do that in just a moment when Bob decrypts this then it's gonna have all the letters squished together there's not gonna be any lowercase letters whatsoever so she's concerned that might be hard for Bob to know when one word starts and the other word ends so she might instead add some little fillers into the message so she might write something like W.E.X.R.I.D.E.X.A.T.X.N.O.O.N. why are we doing that well the idea is the letter X is a very uncommon letter in the English language therefore if we insert letter X's that's essentially taking the place of like a space so Bob then when when he looks at the plaintext again he can see oh we ride at noon oh I know what to do and then he goes does whatever the thing they supposed to do this of course will change the ciphertext so you get Zalga had walk or rock or whatever it is right and so that's because all these extra letters are in there X's which again Bob will be able to decrypt them with the appropriate command we'll talk about that in just a second so let's go back over here to magma so if we wanted to implement this operation of magma we need to include the command Caesar encrypt which the code you can either copy from the screen you saw there or more likely click on the link in the description to take you to github for which you can copy all of this magma code and magma can be run on the following website where you can see right here which is also linked in the description of this video now to run cipher text the cipher encrypt you do need to have the digitized command and the alphabetized command copy and pasted prior to the pasting of a Caesar encrypt which you can see these commands already right here in my compiler Caesar decrypt will talk about that just in a second and so I want to show you what happens that we run these commands so what's going on here is that we have our command Caesar encrypt we have our message we have our parameters one and three right here in magma if you have a double forward slash in front of something that I'll actually comment it out so magma will ignore that command so if I run it right now there's no output if I remove the comment there and I run Caesar encrypt you now get this gobbledygook if I do the second one right here you run it again that's where these things come from if you change your parameters let's we want to shift by five right then you'll get a different message and so the shift is the key if you know what the shift is then you know what the original message is we'll talk more about that just just this moment right here actually so now let's suppose that Bob receives a message from Alice it's let's say he receives the message department of justice homeland encryption unified division no I don't think that's what that meant this is this is clearly a cipher text right this looks like gibberish so what Bob needs to do is he needs to decrypt it now he knows that Bob and himself are using the cipher shift key of B3 he knows that's the key and so what he's gonna do is he's gonna run the cipher decrypt algorithm which this is the inverse operation to the encryption mapping we talked about earlier it takes on three parameters the first parameter will be the cipher text the second parameter at the moment it's gonna be one we'll talk about what that does a little bit later and then the third parameter is gonna be his analysis secret key which is in this case a shift to the right by three when he runs the Caesar decrypt procedure he's gonna get the message algebra all right and that means something to to Alice and Bob which we know if anyone sends the message algebra that must be diabolical here's the actual code for Caesar decrypt you can copy it from the screen right here or more likely copy it either from the attached lecture notes to this video or from the github link is probably the best way to get a hold of this so what seizure decrypts gonna do is it's gonna first take the cipher text and it's gonna digitize it we need them to be numbers so that we can do algebraic operations on it then we're gonna initialize the plain text just as an empty array and what we have to do is because of this number a we might need the inverse of it again I'll talk to that a little bit later but as the number a we're just using as a one right now it would compute the multiplicative inverse of a which in this case will just be one and so then it's going to be basically solving the linear modular equation from before so the the previous equation took a x well you take x and you translate it to ax plus b well if you want to solve this equation you're gonna have to take the inverse of a times it by c and then subtract from it the inverse of b like so so that that's all we're doing so we're just solving the we're just solving the linear equation mod 26 and so that's then you do a for loop and you do this for each letter and see one by one you just reverse you on you you unshifted back to where it was and then you're gonna alphabetize it and so that's what we're trying to explain right here the encryption process as you send x to x plus b the decryption process then says you'll take y and you'll send it y minus b mod n that's the basic idea behind the Caesar the Caesar cipher in that regard so if we switch over to magma again let's take a look at that we will comment out Caesar encrypt encrypt we don't care about that one I already have the Caesar decrypt pasted on the screen right here and so what happens if we run that one we'll submit it and so we get back you put in this DOJ HEUD we get back algebra if I just type in some random message right something like this and you run it you get back also something random because the message that I just typed in right now this was not a legitimate message to begin with so when you decrypt it you get back a bunch of gibberish all right now of course if I put these processes together I might say something like the following let's Caesar say Caesar encrypt a message what we want to say we might say something like magma let that be our message right now it seems insidious right so let's say we do the shift of seven if we encrypt it I'm gonna actually save this as e and then so that you can see it on the screen I'm gonna type in e right there and then I'm gonna run the decryption process using the message e I don't need I don't want quotations in that case because if I do quotations around e it'll think my message is e and not the function or not the variable e that has it saved so now we want to run seven as our as our key right there and so you run it then the the ciphertext is gonna be th and th and you know that that's gibberish but then the the plain text is magma right there now be aware that if use a different key to encrypt versus decrypt you're not gonna get something back like so the encrypted message is RFL RF right that's the ciphertext but when you decrypt it using seven instead of five you end up getting back KYE KY right that that's that's not a real message right there and so it's imperative that those who are working to decrypt it have to use the same key as who encrypted it Alice and Bob need to have the same key so the symmetric key cartography met methods we talked about the Caesar cipher being one of them it has the benefit of being easier to use compared to other crypto systems we'll talk about for example public key cartography in the next lecture which is much more sophisticated but symmetric key cartography has the weakness that both keys are both both parties communicate must have the same key and that key has to remain secure if one of the parties doesn't have the key then they can't either encrypt or decrypt you can't communicate effectively like we saw just a moment ago and if any eavesdropper like Eve has the key then she also can decrypt the messages and she also can encrypt messages and pretend to be Alice so there's some authentication identity problems with that as well and so a symmetric key cartography the more parties involved in the communication the harder it will be to keep the secret key safe if an encryption process is compromised for example if an enemy captures an officer and learns the details of the encryption process through interrogation or maybe even torture yikes all past encrypted message are now vulnerable to be read all future messages can be protected by adjusting the encryption process this can be this is best done by switching the encryption key if Alec or if Eve knows that Alice and Bob are encrypting messages via the Caesar cipher and they shift everything by three then she can read all the messages and so therefore if once Alice and Bob discover this then they're like oh we're gonna we're gonna switch our key from three to 17 all right that's how we're gonna do this to try to protect future messages well and switch switching encryption method hastily is impractical since the encryption method will likely be part of the communication infrastructure right we have our infrastructure has built into it maybe the Caesar cipher so we can't be like oh she figured out we're doing the Caesar cipher we're now gonna go do the hill cipher next that would be too fast too difficult to do it and so typically a new key can easily be constructed and distributed right so we then have to we'll just be like oh we'll keep on using the Caesar cipher but we use a new key and that then has to be securely shared that's an issue in of itself which I won't talk too much about right now so for this reason the security of any reliable crypto system must not rest on the secret of the encryption method but instead must be on the key of the encryption of the encrypted system itself what I'm trying to say is if Alice and Bob expect to have safe communications the secret should not be how the Caesar cipher works if your encryption method is itself the secret eventually that'll leak out and you will not have enough time to develop a new system before your information is compromised the security needs to be on the key not on the method itself so we should assume that Eve knows these two are communicating using the Caesar cipher but on the other hand if they know it's a Caesar cipher how many keys are there with the Caesar shift the method has only 26 encryption keys which correspond to the 26 letters of the alphabet and you could try to beef that up by like saying oh okay we'll use lowercase letters so that I've doubled that we can use special characters like commas and periods in percentages or whatever symbols you want right so you could do sort of like an ASCII code which I think goes up to what 255 characters or whatever but the the the strength of the Caesar cipher is dependent upon how big the alphabet is how many letters are in the alphabet because that gives you the possible shifts well with only 26 letters it would take a computer no effort to search every possibility and see which decryptions actually give you plain English in which ones don't even a person doing it by hand it would 26 attempts would not take that long I mean it would be a moderate feat but it would be doable right this is what's commonly referred to as a brute force attack brute force attack meaning that you look at every possible you look at every possibility and see which one works generally speaking brute force attack is not very effective if you have a good crypto system because there's just too many possibilities if you're working mod 26 that's just too small it would be easy to brute force that even if you're working mod 255 or I guess 256 or whatever if you if you had something like that that's still would be too easy for a computer to do so in the end although the Caesar cipher worked very well back in ancient Rome in the modern digital age with with technology computers we need a much we need a much larger number of possibilities if we want our encryption to be secured from just brute force attack