 All right, hey y'all Mr. Gibson here with your next lesson in cryptography and today we're going to be looking at One method that we could use to determine the keyword length for the vision air cipher We'll go on to learn how knowing the length of the keyword is going to be really helpful for us but for today, we're going to focusing on Just how to find the length of that keyword and the method that we're going to be using is called the kasiski test The kasiski test was named after this person here Friedrich Wilhelm kasiski a German military person Who wrote in 1863 a 95 page book on cryptography? And it was the first published account of a procedure for attacking the vision air cipher But more broadly polyathletic substitution ciphers of other types as well It's worth noting that Charles Babbage was most likely already aware of this method about 10 to 15 years earlier But just hadn't published it Maybe just kept it to himself. Charles Babbage is widely known as kind of the The father of computing and then creating some of the first mechanical computers ever ever made This method that we're going to discuss relies on Analyzing the gaps between repeated fragments and cipher text Um and those gaps are what's going to give us the hints about the length of the keyword Now you might be thinking like there shouldn't be any repetitions in our cipher text That's one of the main reasons we switched over to polyathletic ciphers Because unlike monoathletics substitution ciphers The same letters in your plain text don't go to the same letters in your cipher text. That's the whole point So how do we get repetitions? So let's start by looking at some repetitions and think about well Why are they even there? So here's an example cipher text This was in ciphered using the vision air cipher And if you stare at this for a little bit, you might see some repeated fragments. I'll highlight them here now There we are There's more than just these two, but we're going to focus in on the repeated fragments z v r a o and m b d b m v s So a five letter fragment read that repeats and then a seven letter fragment that repeats And we're going to think about why are these here? Is this just random? It's supposedly, you know plausible that these these could just show up by chance But the fact that we've got so many repetitions of this length seems like it could not just be random There's got to be some underlying pattern here at play So let's take a look at maybe how these repetitions in our vision air cipher texts are generated To do that we're going to use this plain text phrase This is a little fragment here of a sentence at one sentence that ends on a plane And then the next fragment starts with the plane is due And we're going to try and encrypt this using vision air with a couple of different keywords here We're going to see do any of these keywords cause a repetition to occur in the cipher text for the same fragment in the plain text So we'll start with the five letter keyword water And when we encrypt our plain text into cipher text We'll see that the plain text repetitions start eight characters apart. So the first letter p to the next letter p is eight characters But we don't get a repetition in the cipher text. The first time is tc w and x and the second time is pe e a so No repetition for this one Let's try a different keyword. Let's choose a shorter one a four letter keyword just chose milk And when we encrypt our same phrase using that four letter keyword We do get a repetition So we have eight letters or eight characters between our plain text repetition and we get a cipher text repetition They both go to zx i y o we can kind of see well It makes sense the running key seems to have the same five characters above the word plain in both circumstances Let's try one more time. Let's use an eight letter keyword We'll choose the word hospital And when we create our running key and we encrypt our plain text Yet again, we get a repetition in the cipher text because again In the running key the same five letters appear over the word plain both times So let's think about that for a minute Why are we getting sometimes our running key lining up over the same words in the plain text? What might cause that to happen? So you could sit here and we could do some trial and error and try a bunch of different key lengths and figure out Which ones caused the repetition but for the sake of time I've got some information to share So let's look When we had our characters that repeated in the plain text eight characters apart It turns out that keywords of length two four and eight would cause that repetition in the cipher text If our repeated plain text fragments were a little bit further apart, maybe 10 characters Uh the keywords of length two five and 10 would also cause repeat repetitions in the cipher text And if we had a plain text that repeated 20 characters apart The keywords that would cause repetition would be two four five 10 and 20 So you need to hit pause on the video here and think about what's the pattern here What's causing the repetition based on the length of the keyword when you know how far apart your plain text fragments are Turns out the result is that if you have your keyword of a given length, we'll call that n If that n divides into the distance between the repeated plain text fragments, which we'll call delta Then the corresponding cipher text is going to repeat as well So a couple ways we could think about that if you want the math way We could say that delta the distance between the plain text fragments is congruent to zero when We're working in the modulus of n If you're more program or python Minded we could say that if the delta mod by n is equal to zero Then that means you're going to get the repetition in the cipher text So let's see how this plays out here. Let's go back to our example. Let's see how we can use this fact to our advantage here We're going to look at first at the fragment z v r a o And we're going to look at the index in our string Or basically the position in the in the cipher text that the repetitions start at so the first one's at index 102 And then 134 and then 390 402 and 426 With the green ones here the m b d b m v s They're at 143 155 307 411 and 439 And I find it usually pretty helpful to put this on a table So we've got our repeated fragments the indices of each occurrence of the of the fragment And then I calculated those delta values. So just looking at like to say z v r a o The first occurrence is at 102 the next one was at 134 So the first delta value is 32. They were 32 characters apart And then between 134 and 390 was 256 apart and then 12 and 24 respectively And then for the second fragment the m b d b m v s those delta values you get are 12 152 104 and 28 Now notice we can get a lot of different delta values But there's only one value for n n is the actual value of our keyword And remember what we just said If we assume that those repeated fragments in the cipher text Are coming from not chance but repeated fragments in the plain text Then whatever the length of the keyword n It has to divide all of these deltas not just one or two but all of them So there's a couple of strategies that we could we could use here We can just kind of guess and check and you might kind of realize we'll start with a small number So if it's got to divide 12, let's start there. So it's how they got to be two or three or four or six or 12 And then you can kind of start cross referencing those against the larger ones Well, okay, it can't be 12 because 12 doesn't divide into 32 for example Or it can't be six because six doesn't divide into 28 and you can start whittling down your possibilities from there And once you do that you realize well, it's got to be either two or four And a lot of times when we can narrow it down the two or three choices We can just assume that whoever the person encrypting the message is they use the most secure option So if I had this two or four, I would guess four a two letter visionary keyword isn't isn't very secure Another way you could do it that's a little more systematic as opposed to kind of guessing and checking Because we can use the prime factorizations of our delta. So You might remember learning in school that every number can be factored down to Its primes So for example, 12 can be factored down to two times two times three or two squared times three And what we're going to do is we're going to kind of work our way from smallest to biggest of our delta values And comparing its prime factorizations To figure out which factors are in common between all of these numbers because if n it has to divide all seven of these numbers That means n should have the same really share some factors with all of these numbers That's what it means to divide it means that it shares the factors of the number It's dividing into or at least some of the factors So We'll go to the next row here and we'll see that 24 factors the two cubed times three And if you look at well, which factors does it share With our set of common factors so far. It still only shares two squared times three And then we'll prime factorize 28 and we get two squared times seven And we'll look okay. Well, which of those factors does it have in common with our our list so far So if you compare that to two squared times three the only factors in common are two squared And then we'll prime factor 32 that's two to the fifth The only factors in common between two to the fifth and two squared are two squared And we can keep moving down the line And ultimately we realize that the only factors that are in common between all seven of those numbers Are two two squared, which means that the only possible numbers That our key length could be is either two squared itself or some subset of those Factors so either two or two squared So we get the same result. We know that our key could either be two or four in length and again, we'll assume four Now the question becomes Now that we know or at least feel pretty confident that our keyword has length four How do we know what the keyword is and what's the resulting plain text? But we're going to save that for a future lesson for now. That's it for the kasiski test Make sure you practice this a few times using our checkpoint quiz and make sure you got a hang of it Thanks for watching. We'll catch you on the next one