This course will introduce you to the foundations of modern cryptography, with an eye toward practical applications.

Loading...

From the course by University of Maryland, College Park

Cryptography

415 ratings

This course will introduce you to the foundations of modern cryptography, with an eye toward practical applications.

From the lesson

Week 1

Introduction to Classical Cryptography

- Jonathan KatzProfessor, University of Maryland, and Director, Maryland Cybersecurity Center

Maryland Cybersecurity Center

[SOUND] In this lecture, I wanted to go into

Â a little bit more detail about how it's possible to break the Vigenere cipher.

Â This lecture is really intended for

Â those of you who are going to be doing the first programming assignment, and so

Â I've actually tried to present exactly the necessary background you need.

Â In order to complete that assignment here.

Â Those of you who are not planning to do that assignment can feel free to skip

Â this video, although you may find it interesting as well,

Â even if you're not planning on doing that assignment.

Â Now when we introduced the VigenÃ¨re cipher,

Â we presented it in the following way.

Â We said that the key was a string of letters.

Â From the English alphabet and

Â the plaintext will also consist of letters from the English alphabet.

Â To encrypt a given plaintext with a key, what you do is simply shift every

Â character in the plaintext by the amount dictated by the next character of the key.

Â You can view this simple as addition modular 26,

Â because there are 26 letters in the English alphabet.

Â And if the key is sorted then the plaintext you simply wrap around in

Â the key as needed, and decryption just reverses the process.

Â So here was the example we gave, where we're encrypting the plain text,

Â tell him about me, using the key cafe, and so

Â you can view this as simply replicating the key cafe, as many times as necessary,

Â in order to complete to fill out the length of plaintext.

Â And then doing the appropriate addition, ie shifting,

Â modular 26 in order to get the cipher text as displayed her on the bottom line.

Â Now, I wanna present a variant of the Vigenere cipher, which is sort of

Â mathematically the same, although technically a little bit different.

Â And what we are gonna do here is we are gonna work with both plaintext and

Â keys viewed in terms of bytes rather than of letters over the English alphabet.

Â And so in particular what we are gonna do is we are gonna resume,

Â we start with an ASCII plaintext and we are gonna have a cipher text.

Â Which we're gonna represent in hex.

Â So ultimately, of course,

Â the plain text key and cipher text are all going to consist of bytes.

Â It's just that we're gonna view the plain text as nasty string and

Â it's easiest to view the key and cipher text as hex, digits or hex strings.

Â Now why would you want to do this?

Â Well, first of all, it turns out to be easier to implement.

Â So if you look at the code, actually for programming assignment one,

Â you'll see that it's fairly easy to implement this.

Â It's not that it's too much more difficult to implement the VigenÃ¨re cipher the way

Â we had just presented it, but it's slightly easier to do it this way.

Â I think more to the point, is that it's easier to use and the reason for

Â that is the VigenÃ¨re cipher as originally presented

Â was limited to plaintext that only consisted of lowercase English letters.

Â It did not support capital letters, it did not support punctuation,

Â it did not support spaces.

Â And if you were ever gonna do encryption and practice, you'd, of course,

Â like your encryption scheme to be able to handle arbitrary ASCII characters.

Â It turns out that what we're also gonna change is rather than doing addition,

Â modular 26, we're going to do byte-wise XOR.

Â So we're gonna take a byte of plaintext and a byte of the key, and

Â XOR them together to get the next byte of the ciphertext.

Â Now we could have done modular addition here too.

Â The number of possible bytes is 256, because a byte is of course eight bits,

Â and so we could have done addition, modular 256, and that would have been

Â maybe morally closer to the VigenÃ¨re cipher as we had originally presented it.

Â In fact, it doesn't really matter much from a mathematical point of view, and

Â it's easier to implement XOR,

Â it's faster to implement XOR than to implement modular 26 edition I suppose.

Â So let me give an example here.

Â This is just to specify exactly what's going on and

Â you can look in the code actually for programming assignment one.

Â I've given their code for encryption and

Â you can see how that lines up exactly with what I'm saying here.

Â So again we're gonna view the key as a string of bytes.

Â We are gonna view the plaintext as a string of ASCII characters, and

Â to encrypt, what we do is simply XOR, as I said,

Â the next character of the plaintext with the next character of the key.

Â And we wrap around in the key as needed, just like in the original Vigenere cipher.

Â And the decryption can reverse the process because XOR,

Â if invertible in the sense that if we take a plain text character,

Â XOR the next byte of key to get a cipher text character and then XOR the key

Â into the cipher text character, we'll recover the original plaintext character.

Â So here's a worked example.

Â Let's say our plaintext is the string "Hello!" so

Â this is a six character ASCII string.

Â Notice we have a capital letter here.

Â We have lower case letters here and we also have the exclamation point.

Â The key let's just take as the hex string.

Â A1 2F, so that's going to be a two byte key.

Â If we represent the plaintext "Hello!" as a sequence of hex characters and

Â you can do this just by looking in an ASCII table,

Â then you'll see that if we write it out in hex,

Â the plaintext is equal to hex 45 65 6C 6C 6F 21.

Â We're going to then XOR that with the key were again, the key is replicated as many

Â times as needed to fill out the length of the plain text.

Â So that means that in this particular case, we're going to XOR

Â the hex string on the previous line with the hex string A1 2F A1 2F.

Â Now it's easiest to see what's going on if you write everything out in terms of its

Â underlying binary representation.

Â So let's take the first pair of characters.

Â So we have the plain text character hex 48 and

Â the first character of the key, hex A1.

Â If we write those out in binary,

Â hex 48 is equal to 0100 1000 hex A1 is 10100001.

Â If you XOR those bit by bit you get the result 11101001.

Â And then if you convert that back into hex you get the hex string E9.

Â And if you do that for each character of the plaintext,

Â throwing it with the correspondent character of the key, you should find that

Â you get the cipher text, the six byte cipher text E9 4A CD 43 CE 0E.

Â Now how can we attack this variant Vigenere cipher?

Â And from now on, for the rest of this lecture,

Â we're going to assume that we're attacking this variant Rather than the original.

Â Well there are two steps involved.

Â So the first is determining the key length.

Â So, I didn't specify exactly how the key was chosen.

Â And in fact there are different ways you can imagine choosing the key.

Â But what I'm thinking of here, is that the key length is chosen uniformly in some

Â known interval and then after you fix the key length,

Â you then chose a uniform sequence of bytes of the specified length.

Â So when we're attacking the scheme,

Â we're gonna be given some cipher attacks, and we're gonna try to recover the key.

Â And we're gonna do that in two steps by A, first determining the length of the key,

Â how many bytes are in the key.

Â And then determining every byte of the key until we have the whole thing.

Â So let's go through these steps one by one.

Â When attacking the Vigenere cipher,

Â what we're going to make use of known plaintext letter frequencies for

Â the English language alphabet, we're gonna assume here that we're gonna

Â be given cipher text that results from encryption of English language test.

Â We've said already that if it's not the case,

Â if we're working with some other language.

Â Where the plaintext is from some other language, we can, of course,

Â do a corresponding attack, but just using different frequencies.

Â But here I'm assuming English,

Â though it won't matter in any of the mathematical equations that I show.

Â You can get these letter frequencies from Wikipedia.

Â You can also get them from calculating them yourself over some corpus.

Â But for our purposes it suffices to take the values that are given

Â on Wikipedia or in this chart.

Â So how do we determine the key length?

Â Well, first of all, let's let p i, for i ranging from 0 to 255,

Â be the frequency of the byte i in some English language text.

Â That is, p0 represents the frequency

Â of having the byte 0 in some English language plaintext.

Â Now of course, if you look at the ASCII table, you'll see that ASCII

Â characters corresponding to values less than 32 are non-printable characters.

Â And so if you're encrypting English language text,

Â you would have that p i is equal to 0 when i is less than 32.

Â And similarly, the value of p i for i greater than 127 is also 0.

Â The ASCII character with the value 127 is some other non-printable character,

Â and ASCII values are not even defined once you get above 128.

Â So the values of p i are going to be 0, the frequencies

Â of those bytes are going to be 0, when i is less than 32 or i is greater than 127.

Â As another example, if you look at the ASCII value corresponding

Â to the lowercase letter a, that's a 97.

Â And so p 97 is the frequency of the lowercase character a in

Â standard English texts, let's say.

Â And the main point here is, first of all, that this distribution can be calculated.

Â Here it wouldn't suffice to look at the distribution on Wikipedia

Â that only has English letters.

Â You would need also to take into account punctuation and

Â spaces, perhaps numerals, perhaps the difference between upper and lowercase.

Â But nevertheless you could calculate these from a known corpus.

Â And more to the point is that this distribution is very far from uniform.

Â As we've already seen, many of the p i's are 0.

Â Even the p i's that are non-0 are not uniformly distributed among those values.

Â An e is the most common letter, for example, and

Â a space would be relatively common as well.

Â And the key point we're gonna use in our attack is that if the length of the key

Â that was used to generate some ciphertext is N, then every Nth

Â character of the plaintext was encrypted using the same shift, or XOR in this case.

Â And so if we take every Nth character of the ciphertext and

Â then calculate frequencies of different bytes in every Nth character.

Â So we just imagine taking out every Nth character or

Â selecting out every Nth character of the ciphertext, and

Â then running your frequencies, your statistics over that.

Â Then what you expect to get are exactly the p i values, but

Â in some permuted order.

Â Because every time you have an e, let's say a lowercase e in the plaintext,

Â that's gonna get shifted by the same amount if you look at every Nth character

Â to some ciphertext character, some ciphertext byte.

Â And then you expect to see that corresponding byte appear with the same

Â frequency in the ciphertext, again, if you look at every Nth character.

Â So what you're doing is simply shuffling around the p i's, but

Â the values themselves should be replicated.

Â On the other hand, if you take every Mth character, where M is not a multiple of N,

Â then you're picking out characters that are all shifted by different key bytes.

Â And in that case, you would not expect the frequencies of those characters

Â to match up with the frequencies of the underlying plaintext.

Â And so, in particular, if you calculate the resulting frequencies

Â that you get from doing that, heuristically speaking, you

Â expect to get something that will be close to uniform or at least closer to uniform.

Â Because again, you're shifting your plaintext by different amounts

Â in this particular selection of ciphertext characters.

Â So the question of determining the key length boils down to distinguishing

Â the case where you have a bunch of frequencies that are permutations of

Â these known p i values,

Â versus the case where you've got a bunch of frequencies that are close to uniform.

Â There are various ways you could imagine distinguishing these two possibilities,

Â but the easiest is to do something like the following.

Â So say you've computed some candidate, some observed set of frequencies,

Â q 0 through q 255, where, again, q i here represents

Â the observed frequency of byte i in your selection of ciphertext characters.

Â Say, again, by picking out every Nth character of the ciphertext.

Â Calculate those frequencies and then just compute the summation of q i squared.

Â And let's see what we expect.

Â So if your q i values are close to uniform, i.e.,

Â each q i is about 1/256, well,

Â then the summation of q i squared is going to be the summation of 256 values that

Â are all approximately 1/256 squared, and that equals 1/256.

Â On the other hand, if the q i values are a permutation of the p i values,

Â then the summation of the q i squared will be about equal to, or exactly equal to

Â if they're exactly a permutation, the summation of the p i squared.

Â As I said earlier, you could compute the p i's from a known corpus, and

Â then compute what you expect to get for the sum of the p i's squared.

Â This would be somewhat annoying and time consuming to do, but

Â you certainly could do it.

Â But the key point, actually, is that this value,

Â the sum of the p i squared, will be significantly larger than 1/256.

Â And so this gives a way to determine the key length.

Â What you do is you simply try all possibilities for the key length, and

Â for every candidate value N, you pick out every Nth character of the ciphertext.

Â Compute the frequencies of each byte in that selection of characters.

Â Compute the summation of the frequency squared.

Â And then just look for

Â the maximum value among all the different choices of the key length.

Â And again, what you expect is that when you try the wrong value for the key

Â length, you should get approximately the summation of q i squared being 1/256.

Â And when you hit upon the right key length,

Â you expect to get something that's much larger than 1/256.

Â It will be close to the sum of p i squared, though again,

Â you don't really need to know what that value is.

Â You just need to look for the maximum.

Â So now that we have a way to get the length of the key,

Â we're left with determining each of the bytes of the key.

Â So let's assume we've done the first part correctly and

Â the key length N is now known.

Â Well, what we're gonna do, again,

Â is look at every Nth character of the ciphertext, pluck out every Nth character.

Â Note that we can do this starting with any character we want.

Â And in particular if we wanna figure out what the ith character or

Â byte of the key is, what we'll do is simply start at the ith character

Â of the ciphertext and then count every N characters from that and

Â pick out every Nth character starting from the ith character.

Â If we start from the first character and then get the first,

Â n plus first, two one plus first,etc., that will all correspond to a bunch of

Â ciphertext characters generated by shifting using the first byte of the key.

Â And so on, if we had started with the second character,

Â that would correspond to shifting or XORing with the second byte of the key.

Â And I'll call this the ith ciphertext stream.

Â So the sequence of every Nth character starting with the first will be the first

Â ciphertext stream, etc.

Â And again, the observation is that every byte in the stream was generated by XORing

Â some underlying plaintext with the same byte of the key.

Â What we'll then do is we'll take this resulting stream and

Â we'll try decrypting it using every possible byte value B.

Â So we'll try every possible value of that byte of the key ranging from 0 to 255.

Â And that will give us a candidate plaintext stream for

Â each possible value of that byte.

Â So we can imagine if we did them all at once, we would get 256 possible plaintext

Â streams that could result from decrypting the corresponding ciphertext stream.

Â Now these are all going to be gibberish because they don't correspond to adjacent

Â characters, they correspond to taking every nth character and if you took every

Â nth character of a plain text, you would get something which is gibberish.

Â However, you do still expect that the plain text frequencies are observed.

Â That is, even if you take every eleventh character of your plain text, you would

Â expect that the letter E will occur most frequently in that plaintext stream.

Â And so, when the guess B is correct, then you can deduce a couple of things.

Â So when you guess B for the shift value,

Â the value of that ith byte of the key is correct, then first one all,

Â every byte in the plaintext stream, that you've obtained,

Â should be a valid character that would appear in English text.

Â And, in particular, that means every byte in the plaintext stream

Â should take on a value between 32 and 127.

Â If it's outside that range,

Â then unless the plaintext came from something else that you didn't expect,

Â that would mean that your guess B could not possibly be correct,

Â because the English language text does not have any ASCII characters lower than 32 or

Â greater than 127.

Â Moreover, as we said earlier, the frequencies

Â of each of the letters would correspond to their frequencies in regular English text.

Â And so, in particular, if you just look at the frequencies of all the lower

Â case letters, say right as a fraction of all lower case letters over all so

Â the number of times that the lowercase E appears as a fraction of all lowercase

Â letters that should be close to known English letter frequencies.

Â And so what you can do is you can then try your guess B for the next byte of the key,

Â pluck out ever end character from your decipher text, everything by B

Â to get a candidate value for every nth character of plain text.

Â And then look and tabulate the observed frequencies for

Â each of the lowercase letters, say.

Â And we'll call these values qa through qv, so they just correspond,

Â those are the observed frequencies for the letter A among all lowercase letters,

Â the letter B among all lowercase letters,

Â up to the letter z over all lowercase letters.

Â And again when you guess B is correct, then you should find that

Â the summation of q i p i where PI are the known letter frequencies of the lower

Â case English letters should be equal to the sum of the PI squared.

Â And this is because you expect in this case QI to in fact be equal to PI right?

Â QI here is observed frequencies in a candid of plain text not in

Â the cipher text itself.

Â And that value is a known value you can calculate it for yourself or

Â you can take my word for it and use the value display here.

Â So again, what you expect is that when you hit upon the correct guess B for

Â the next byte of the key and then decrypt the Ith ciphertext

Â stream using that byte B, you should get a plain text stream, a sequence of bytes,

Â where first of all every byte in the stream is between 32 and 127.

Â Furthermore, the calculated frequencies of lower case letters in that stream

Â should give you some values, q a through q z,

Â such that the sum of q i p i is about .065.

Â In practice, you're not going to get exactly .065 and what you can do is simply

Â take the value of b that maximizes the value of summation q a p i.

Â Subject to this caveat of everything lying between 32 and 127 and

Â possibly others as well, other restrictions I mean as well.

Â So for example, if you happen to fee a huge amount of commas, that would be

Â a sign that you probably that you're guess for B is probably not correct.

Â And then you would try another one.

Â What's the time that we need to implement these attacks?

Â Well, let's say that we know that the key length is in the range of 1 to L.

Â So the determining the key length takes about time 256 times L.

Â These numbers are very roughly, they're just meant to be indicative.

Â There are different operations going on and so

Â this is not any kind of very specific measure.

Â But just in general, it's about 256 times L, because, for

Â each candidate value of the key length,

Â you're doing about 256 works to figure out all the observed frequencies, and

Â do the calculations, and then figure out which one is the maximum.

Â Once you've determined N, the actual length of the key.

Â Then, determining every byte of the key is done by

Â repeating N times the process of finding the next byte of the key.

Â And finding the next byte of the key takes about time 256 square because you're

Â enumerating overall 256 possible values for that byte.

Â And then, for each candidate value of a byte, you're doing about 256 work.

Â You're doing that.

Â That 256 squared work a total of N times.

Â N is at most L, so the work here is at most 256 squared times L,

Â and the point here is that it's linear in L.

Â So this really is not only polynomial time, but it's really quite efficient.

Â And it's instructive to compare this to the time it would require

Â to carry out a brute force key search.

Â So a brute force key search would require time about 256 to the L.

Â So the number of possible keys of length exactly L is 256 to the L,

Â if the key has L bytes in it and each byte takes on 256 values.

Â So we've gone here from the trivial brute-force key search, which takes

Â exponential time in L down to linear in L, just by being a bit clever in our attack.

Â Now I do want to point out a couple of things in practice,

Â now the first thing is that the attacks that I have described will become

Â more reliable as the ciphertext length grows larger, and the reason for

Â that is because when your ciphertext is larger, your plaintext is larger, and

Â when your plaintext is larger, you expect that the observed

Â letter frequencies in the plaintext will more closely correspond to

Â the letter frequencies tabulated on say Wikipedia.

Â And for very short ciphertexts and correspondingly short plain text, it's

Â possible that you have large deviations from those average letter frequencies.

Â But if you have a very short playing text of about 30 characters,

Â you might have no E's at all.

Â And then that would completely throw everything off.

Â But as you have hundreds of characters and

Â even thousands of characters, the attacks get better and better.

Â Now the attacks do still work for relatively short ciphertexts and

Â in particular I did it myself and the attacks I described do work

Â on the ciphertexts I've given you for homework assignment one.

Â However, the attacks don't quite work or may not quite work out of the box as I've

Â described them, and you may need to do a little bit of tweaking in the algorithms,

Â and a little bit of manual involvement might be needed.

Â You might have to even potentially guess.

Â You might get, for example, two different candidate bites for

Â the first bite of the key.

Â And then you have to look yourself and

Â see which one give you something which looks meaningful.

Â And if you have to do this in order to solve homework one,

Â you shouldn't feel you're doing something wrong,

Â it's perfectly fine to do that as long as you get the right answer in the end.

Â So, with that in mind, I'll just leave you to take

Â a look at the programming assignment and try your luck with it.

Â Please do feel free to use the discussion boards to ask questions and

Â to try to get help from both myself and other students in the class.

Â You shouldn't feel if you're stuck you should feel free to ask and

Â get yourself unstuck as quickly as you can.

Â And with that I just wish you good luck and

Â I do hope that you have fun with this assignment.

Â Coursera provides universal access to the worldâ€™s best education,
partnering with top universities and organizations to offer courses online.