 with LCG we saw a very simple algorithm for generating pseudo random numbers or sequences specifically sequences of pseudo random numbers and we care about the length of that sequence so a good pseudo random number generator will generate a long sequence of different values and then it repeats the longer the sequence before it repeats is better in this generator the length of the sequence depends mainly on the modulus m you make m large if you mod by a large number then the answers can be between zero and that large number minus one so the larger m the larger the potential sequence but it also depends on the values of the multiplier a and the increment c and there's no we're not going to go through any ways to say what's a how to determine a good value of c and a some examples of good values are given here if c is zero a of seven of the power of five people have done studies and find that's a reasonably good value of producing a large random sequence we saw when we chose bad values it's a bad random sequence we saw 23 24 25 and so on so those values are important another generator blum blum shub three people who come up with this algorithm we start with we choose some large prime numbers p and q let's not go through a detailed example we've got one on the slide but we choose two two prime numbers they should be large if we want this to work well and if we mod those numbers p and q by four we should get three as the answer and that's that's what they come up with in in the algorithm that's what this statement says p and q are the same if we mod by four and they both equal three and then we calculate the multiplication of those two prime numbers p times q gives us n and then choose another value s which is relatively prime to n in the next topic we're going to cover prime numbers relatively prime and different aspects of number theory what can we say briefly about relatively prime the greatest common divisor of n and s is one in this case so you cannot choose any value for s it's related to the it's not a prime number relatively prime is different than a prime number it's related to the calculator value n I think we cannot go through that in in a short way at the moment because we need a good 30 or 40 minutes to discuss prime numbers and relatively prime to cover that for now believe me that we can choose this value s if we know in fact p and q so the user chooses p and q they have there's a way to calculate both n and s from that and then n and s are used in the algorithm to generate a sequence of bits so you initialize the c or x0 to s squared mod n and then you go through a loop my indentation is not so good here these are both inside the loop so you go forever so for i equals 1 to infinity so you want to generate an infinite sequence of random bits pseudo random bits take take the previous value of x x0 for example square it mod by n and then you get some integer to get the binary value to get a zero one simply mod by two if you mod by two you'll get zero one as an output and then you go back and do it again you do for so that was for x1 is x0 squared mod n x2 is x1 squared mod n and we keep going just a quick example of that some pre-calculated values so in this case n and p and q 383 and 503 they are the two prime numbers if you mod by four each of those numbers you'll get an answer of three 383 mod four is three 503 mod four is three we can calculate n from that and it's there's a way to calculate s from those two numbers as well then we use these two numbers of 192649 and 101355 in our algorithm where we take s squared mod n gives us x0 20749 and then in our loop we take this value square it mod by n gives us 143135 then mod by two and it gives us one this mod by two simply if this is an odd number one if it's an even number zero that's just producing the bits because what this algorithm does is produces a sequence of bits so the first bit in the sequence is one then we take this 143135 square it mod by n and the second value gives us a bit one and just keep going and this is the sequence of bits that we'd produce the first bit the second bit one one zero zero one one one zero zero zero and we can keep going so the sequence of bits that are produced and that's known people have studied and and believe that that gives a pseudo random sequence of bits for as long as we want because it just keeps going there's no end to that sequence as long as we are not true there is an end but in this case our values of p and q are in fact too small for practical use by large prime numbers we normally mean hundreds of bits in length so this is a very small prime number so p and q should be much larger for this to be secure and if p and q are very large when we modify them together n will be very large and note we mod by n the larger n the larger the possible values that we get in the answer so we will not repeat if for a long time if n is very large so just another example of a random number generator and is considered cryptographically secure in that it meets all the requirements of a pseudo random number generator for the purpose of security some random random numbers we generate depending upon the application we don't care so much about whether it's how close to random it is that is if I need a random number to choose which quiz to give each student maybe I give random numbers to in the exam or in a quiz so everyone gets a different number and therefore the answers in the quiz or exam will be different then my random number generator doesn't have to be that good if it's wrong and two people get the same number not a big problem but with security for example if I want to generate a key then I want it to be truly or as close to truly random as possible so some algorithms produce more randomness than others and for security we say something is if it's suitable for security purposes cryptographically secure which comes back to one of the requirements we mentioned back earlier if you don't know the seed it's hard to determine what the stream of bits will be so if you know the algorithm if someone knows that this algorithm was used but they don't know the initial values that the user chose then they should it be hard to predict what the stream of bits will be and there are many other random number generators we'll briefly show one and then give some more examples so they were two random number generators algorithms another thing we can use to generate random numbers is our block ciphers as we said a cipher takes some structured plain text and should produce some random looking cipher text a cipher by definition should produce random values as output if it doesn't is not so good so all of our ciphers that we've we've covered can be used to produce random numbers or pseudo random numbers here's an example where the encrypt operation is AES or DES or whatever block cipher we'd like to choose and we can use different modes of operation to produce a sequence of bits as output so the seed in this random number generator is some encryption key so value of K combined with some initial value V that we choose and you see in this case this is counter mode so it's our counter mode of operation where the input that says zero we encrypt that we get an output and then using the same key we encrypt one we just increment that the value here we encrypt one we get some output we encrypt two we get some output and we just keep going and the output if our encryption cipher is is good the output should be pseudo random because what we'd like in our ciphers is if we take some structured or known input the output gives us some random value and that's what our ciphers AES and DES do so this is just applying counter mode and we produce a sequence of random bits on the output and we can do it using output feedback mode is similar where we take the output here and and just keep feeding it into the the encrypt operation and so long as the key or the seed is secret someone cannot determine what the sequence will be because if you don't know the key then you don't know what sequence is going to be produced here so you cannot predict what the sequence will be so we can use our block ciphers to generate random numbers what's the problem why would we sometimes not use block ciphers to generate random numbers what's the answer why would we not use our block ciphers to generate random numbers they produce cryptographically secure random numbers but what's the problem compared to say the two examples we went through before so the the generator lcg this equation blum blum shove was a bit more complex or take des and just repeat the operations which one's easier to implement if I ask you for the homework to implement a random number generator you will choose this one it's one line of code or set up some variables and so on very easy to implement and generally much faster than the block ciphers I ask you to implement des and remember just with simplified des there are all those steps the s boxes the permutations and so on much more complex so generally the block ciphers are slower because they're more complex slower so in terms of performance in terms of security using block ciphers is satisfactory but in terms of performance if we care about how fast we can generate them block ciphers may be slower there's some standards of how to generate random numbers pseudo random numbers and see x 9 1 7 is one standard it simply combines triple des so it takes a date and time as an input so the current date and time on the computer it represents it as a 64 bit value a seed value and a pair of 56 bit desk keys and it uses triple des and this is the the structure EDE is triple des remember it's applying des three times encrypt with des decrypt with des encrypt with des that's EDE in fact we use triple des three times in here takes the date and time as a 64 bit value we take some C V some initial value some two keys to be used in this triple des and we produce R and V as outputs we can follow what happens the date and time as used as the key here these two keys come sorry as the input here to the plain text input this is the cipher text output plain text input cipher text output plain text in cipher text out and the inputs of the keys of these three k1 and k2 k1 k2 k1 k2 k1 k2 and we combine them with some x o's and that's considered one way to generate a pseudo random sequence of values some value are so we keep doing that so we produce we apply that once we get R1 and we get the next value of the seed V and then we do it again so we this is our random output and then we take this and feed it into here using the same key and just keep going in a loop applying these these operations multiple times and we'll get 64 bits out each time so just to one way to use the block ciphers combined together the last thing we're going to go through is stream ciphers and look at the relationship between stream ciphers and random numbers a stream cipher uses a pseudo random number generator the structure of a general stream cipher is that we have some plain text to encrypt and we say we have a stream of plain text let's say a continuous sequence of bits of plain text coming in if it's say a voice application as you talk the software generates bits representing your voice we have a key we take the key and that's the input or the seed to the pseudo random number generator it's called a pseudo random byte generator in this case because if we take a byte of plain text one byte at a time eight bits we generate a byte out here and to get the cipher text we just apply the XOR operation take the random or pseudo random output XOR with a plain text and you've got your cipher text send the cipher text across the network and to decrypt you use the same pseudo random byte generator the same key and therefore you'll get the same output note that our pseudo random number generators if we apply the same input parameters we'll get the same sequence as output when we use for example these values if the key is x0 all right this was a bad one but if x0 was 23 and the key is 23 here and we're using LCG as the generator then the key here is 23 the first value out at the encryption side is 24 we XOR that with a plain text we get cipher text and the first value out at the decryption side is also 24 because we've got the same input so we using the same algorithm the same input we'll get the same value of this lowercase k here and we said last week XOR plain text XOR with lowercase k gives the cipher text and quite simply you take that cipher text and XOR with the same lowercase k you'll get the original plain text that's the property of Exclusive all so it's very easy to encrypt just generate a sequence of random bytes lowercase k here and XOR with the plain text as it comes in and then send and decrypt is just an XOR this lowercase k the output of the generator is called a key stream so this is a key as the input or the seed and this is a key stream in this example of LCG was not so good we saw a better one the last one we had which was what do we choose a is 5 c was 0 and m was 32 and we chose x 0 the seed as for example 1 so uppercase k in this case is 1 as an example and the lowercase k that's generated as an output this is the lowercase k we get our sequence which the first value is 1 and then what was the answer 5 5 25 29 17 and so on in fact we get back to 5 and then we repeat 1 and 5 so these are the values of lowercase k so when I have a value of plain text I XOR it with 5 I have to convert 5 to binary XOR and we get our ciphertext and the next byte of plain text I XOR with the next value as the output here and keep going as I have plain text of course this this sequence is not so good because it only has eight values but if we have a large value of m and better values of a and c we can get close to a billion different values so we can make this sequence much longer and we just keep encrypting our plain text and generating values of k as we go and the same is applied at the receiver when they decrypt they use the same sequence because they use the same generator the same parameters the same x 0 x 0 is the uppercase k and therefore they'll get the same values of the lowercase k the key stream as an output they'll get 5 25 29 17 and that will be used here 5 XOR with ciphertext 25 XOR with ciphertext and so on and we'll get the original plain text back so remember these are pseudo random number generators if you apply the algorithm with the same parameters you'll get the same sequence you'll not get a different sequence so generally with a random number generator a pseudo random number generator when you use the same seed you'll get the exact same sequence out and that's what we use here with the same seed x 0 equal to 1 we'll always get this sequence so to use a pseudo random number generator for a stream cipher what do we need to be secure the sequence here should be long have a large period so before it repeats the we should have many different values here and in our example here for example m should be large gives us a potentially larger period so we talk about the period of the sequence the number of values before it repeats and the algorithm that we use here should be such that the key stream this sequence here is appears random approximate true random number stream or sequence and the key the seed should not be able to be guessed because as with any cipher the key should be secret because if if the attacker knows the key they intercept the ciphertext if they know the value of the key they can generate the exact same sequence of numbers here if they know x 0 equals 1 the attacker would generate 5 25 29 17 x or with a cipher text and they'll get the plain text so the key must be secret and to keep it secret it must be long enough to withstand a brute force attack so say 128 bit key when we compare stream ciphers to block ciphers they're usually simpler to implement and faster as a result a stream cipher we should not reuse keys with the same you know we should not reuse keys and we'll give some examples of what goes wrong if we do whereas with block ciphers we can reuse keys let's go through a stream cipher first and then see some examples the one will go through is RC4 which we've seen before it was used in wireless LAN in the old WEP and is used in internet data transfer when I connected the Google website using HTTPS my browser used RC4 so it's commonly used let's go through how RC4 works RC4 was an algorithm divided designed by Ron Revest back in 1987 it's used as we've said already in web browsing and in the past in wireless LANs we'll see it's very simple we'll go through the algorithm and you see compared to desk for example it's something you could implement quite easily it can use different size keys so the length of the key can be between 8 and 2048 bits so you can choose the key length depending upon your requirements should be large enough such that it can withstand brute force but there are some limitations there are some problems when you use RC4 incorrectly and that was shown especially in in wireless LANs in WEP it was shown that although the algorithm is okay but if it's used with a key and an initialization vector in the wrong way it's very easy to break so the way that you choose the key is important with RC4 it's not so much the algorithm it's how the keys are used how does it work Ron Revest you will see this name come up when we come in other topics anyone know what else he developed anyone can think of a cipher that we haven't mentioned but you probably know the acronym a three-letter acronym we've got we haven't mentioned it AES no not the ones we've seen we've seen AES and DS they aren't named after the algorithm not the people this one's named after the designer there's another one you've probably heard of is RSA we will cover it later but RSA is another famous asymmetric cipher and the R stands for Revest in this case and the SA for two other guys we have some parameters and some variables that are used in the algorithm uppercase K unfortunately we have uppercase and lowercase K because we refer to the key and the key stream the key is like the seed to our random number generator the key stream is the output the random stream of bits so we operate in terms of bytes here the key is between 1 and 256 bytes so 8 and 2048 bits we can choose the length there are two important variables used or maintained in the operation called the state vector s 256 bytes and some temporary vector t same length and what we do is we generate a byte each time we have a loop and we generate one random byte and then another random byte this is our key stream it's generated from the state vector we have a state vector which we initialize to value 0 to 255 so it's a vector from has 256 elements the first element is 0 the next one is 1 and the last one is 255 that's just a start and t is a 256 element vector or array and that's initialized to repeating values of the key so if the key is 1 2 3 4 then t will be 1 2 3 4 1 2 3 4 1 2 3 4 that fills out the 256 values then what we do is we for the first operation use the vector t and perform some initial permutation rearrange s permutate s that's just in the initial step and then there's another permutation of s and from that generate a key stream okay that is generate a random byte and we continue to generate random bytes so we repeat step 3 in that we rearrange s generate a random byte rearrange s generate a random byte and keep doing that and those random bytes are then used to encrypt the plain text by X oring the plain text and that random byte so the random byte is K so the encryption is just an X or plain text X or K how do we get K by applying some permutation on this state vector s this is just the pseudo code to describe step one initialize s initialize t s is the first element is set to value 0 the second element the value 1 and so on so s becomes 0 1 2 3 up to 255 and t becomes the values of the key repeated by just modding by the key length some graphical form of s and t so s is simply an array with values 0 up to 255 t is an array with the key repeated then there's the permutation the initial permutation of s we will go through these steps with an example so let's just briefly mention them now in some loop for each value of i up to 255 calculate some value of j and then swap elements in s so we rearrange elements in s and then that's just done at the start and then we generate the stream of random bytes so in a while loop calculate some indexes i and j swap the elements calculate another index t and look up from s using that index t and find our key stream byte lowercase k and then to encrypt take the plain text x or with that lowercase k do this so in this while loop we produce the next value of k and x or with the next value of the plain text and so on it may look may look complex on initial view but I'm sure that you could implement this code in an easy homework one or two hours in your chosen programming language because you see here's the pseudo code right we set some variables some for loop with two lines of code another for loop for the initial permutation and swap elements in array easy and then other a while loop we continue while we have plain text following these steps of calculating an index swapping elements and looking up in an array so in terms of implementation this is very easy when you compare it for example to desk or AES or the other block cycles let's go through an example it takes some time but it's worth going through just show how easy it is I'll go through on the board because we need a lot of space we're not going to go through the full RC for algorithm I've cut things down because in RC for we have a 256 byte array and I cannot write down 256 values and rearrange them so I've cut it down so it's much smaller so a simplified version of RC for just to demonstrate that the steps that it goes through and to hopefully demonstrate the simplicity I think you have this handout in your lecture notes if not it's so towards the end of this topic just check it's on the website why did I not include it I didn't seem to include any no the handout that I'm going to go through is on the website I can put it in the copy center if needed because it's it's quite detailed it may be easier to watch than to copy down we'll do some steps but instead of dealing with 256 bytes let's cut down to so RC for has tuna an array of 256 elements and each element is 8 bits that is one bite we're going to cut it down so it's a bit simpler just an array of 8 elements and each is 3 bits so instead of having to go through iteration 256 times we would need to go through 8 times and instead of dealing with bytes just for simplicity let's deal with 3 bit values so we have some plain text and in we're going to operate in in decimal so 3 bit values I've just made up the plain text 1 2 2 and 2 where these are 3 bit numbers so this is 0 0 1 0 1 0 that's our plain text that's what we want to encrypt and we also need a key to encrypt chosen by the users and in my case 1 2 3 and 6 again that 3 bit values so what we do is we use the key to generate the stream and then to get the ciphertext we take the stream and X or with the plain text so the first thing we do is initialize the state vectors s and t so in the real RC for s was 256 bytes that is 256 elements of 1 byte each in this case s is going to be 8 elements each contains 3 bits so I'll write it as 8 elements each contains 3 bits and we initialize it quite simply from the values from 0 up to 7 in the real RC for from 0 up to 255 but in this simple example s 0 up to 7 just an array with 8 elements and T this other vector array is initialized with the key repeated so 1 2 3 6 1 2 3 6 it's the same length as s so that's step 1 then step 2 is to rearrange s perform the initial permutation so let's look at this for loop and here in the for loop where we see 255 we need to replace with 7 in this case because we've swapped we've cut things down in size so for i equals 0 through to 7 we let's say let's try first we want not go through all of them but for i equals a 0 j is initialized as 0 so the new value of j which is going to be an index is the current value of j which is 0 there's my pointer so we're going through this for loop the first instance j equals the current value of j which is initially 0 plus the ith element of s where i equals 0 plus the ith element of t and all mod by what mod by 8 in this example just make it and at the end here mod so j plus the ith element of s plus the ith element of t so we just look up i is 0 so it's in fact of this element we we index 0 1 2 3 0 1 2 3 so on so it becomes what's j in the end 1 0 plus 0 plus 1 which is 1 mod 8 equals 1 and then we swap elements in the array s that's what this swap function does it takes the ith element and swaps it with the jth element so s is currently this and what we do is swap s i i is 0 with s j and we just calculate a j to be 1 and then we get the new value of s so swap the first two elements in this case this is just the initial permutation of s we're going to rearrange s originally was 0 through to 7 we want to mix it up okay that was for i equal to 0 and we end up with s equal to 1 0 2 3 4 5 6 7 it's not mixed up much yet it's just the food first two elements so we repeat where i equal to 1 and then do it for i equal to 2 3 4 5 6 7 in our example we'll just do one more iteration simply recalculate j it's the old value of j which is 1 we calculated j as 1 so it's updated plus s i equals 1 s 1 plus t 1 mod 8 that was this this line j equals the old value of j which we calculated in the previous iteration is 1 s 1 plus t 1 mod 8 1 plus s 1 s 1 is be careful of the index the our index starts at 0 s 1 is 0 this is s 0 1 s 7 because we've updated s so it becomes 1 plus 0 plus t 1 plus 2 oh sorry I removed t but t is just a repetition of k so 1 plus 0 plus 2 3 mod 8 easy and perform a swap sorry I've gone and swap s 1 with s 3 swap s 1 with s 3 so we just calculated j to be 3 i is currently 1 so we swap element element 1 with element 3 in s so 0 and 3 values will be swapped and you'll get 1 3 2 0 4 5 6 7 yes yes sorry this this this swap should be indented that's that's good point my is it the same in the next one my indentation is not not good here this swap should be intended indented it's inside the for loop so inside the for loop we calculate j and then swap and then increment i and do it again so both the n4 is after the swap and we do it again up until i equal to 7 okay so this is the initial permutation and you end up with I have the answer a rearrangement of s 2 3 7 4 6 0 1 5 that's after we do that step so I've skipped ahead for the other values of i that was step step 2 yeah we do it that at the start and now what we do is we use this value of s to generate values of the key stream the lowercase k and once we get them we simply X or with the plain text so that what we will need is for iterations because we have four numbers in our plain text we're going to generate in let's go forward we're going to apply this stream generation so we have a while loop we just continue in fact for our case because we have four numbers in the plain text we need to apply this while loop four times and at the output of each while loop we'll get a lowercase k we'll take that lowercase k and X or it with p equal to 1 then we'll do the while loop again and X or the new value of k with p equal to 2 and then the new value of lowercase k with p equal to 2 and then again so we do that while loop four times of course our X or we can convert them into binary and you can do the X or let's just go through one iteration of the while loop just to give it an example so i and j are initially zero we calculate new values i equal to i plus 1 so we increment i where we see 256 you replace with 8 in this example because we the real algorithm uses 256 but in our example we've reduced it down to 8 to keep it manageable so i becomes 0 plus 1 1 j becomes I don't know why it's an uppercase j should be lowercase j j becomes the old value of j which is 0 it's been initialized to 0 plus s 1 mod 8 s 1 is 3 s 0 s 1 s 2 so 0 plus 3 mod 8 gives us 3 so we calculate the index i and j then swap the values in s swap s 1 and s 3 in our current state vector so we swapped 3 the the value 3 so we're swapping 1 with 3 so this value 3 with 4 will give us the new value of s so we keep permutating s the state vector 2 4 7 3 6 0 1 5 calculate this new variable t lowercase t another index we're going to use by taking s 1 and s 3 add them together that is 4 plus 3 we get 7 and mod by 8 in our case so the operation we're doing is calculate lowercase t s so i equals 1 j equals 3 s 1 plus s 3 which is 4 plus 3 is 7 mod by in our example 8 not 256 so 7 mod 8 t becomes 7 and then lowercase k is s 7 and s 7 in our array here is the last value which is 5 that's our first random number that's part of our key stream our stream of random numbers to encrypt our plain text we take that random number 5 and x or with our plain text value 1 that's convert them to so now the encrypt process we have 5 p equal to 1 lowercase k equal to 5 in binary x or and that's the first piece of cipher text p 1 the first plain text value is 1 in decimal or 0 0 1 in binary 3 bit values we're dealing with lowercase k is 5 we just determine that in the while loop which is 1 0 1 in binary x or them we get 1 0 0 or 4 in decimal which is our first piece of cipher text then we do it again that is we apply this while loop again find the new value of i note that we don't set them back to 0 we continue we had i equal to 1 j equal to 3 so i becomes 2 j becomes 3 plus s of 2 swap the values in s calculate t find the new value of lowercase k and x or that with 2 do it again x or with 2 and with 2 and we'll get 4 values of cipher text and we'll stop there for the example I'll give you the final values not not important what's important here is that notice how simple it is compared to say when we went through simplified des with simplified des we had two rounds all right in the real desk there are 16 rounds dealing with 64 bits and we had x or operations we have permutations different permutations we had s boxes in the real desk we have eight different s boxes which we substitute and get some value and then we repeat it all again this we have essentially four loops or while loops calculate values so add numbers and perform a modulus very easy to perform swap values in an array very easy to perform in software or hardware and perform lookups and then an x or so in terms of complexity this is much easier than what we've seen or what you see in any of the block ciphers just for your information the output the values of lowercase k that we get we calculate four values the first value was five the second value was see if we can find it six third value was zero and the fourth value was one we only needed four values for k but we could keep going if we had more plain text and we x or that with our plain text so this is lowercase k here one two two two and we'll get cipher text four four two three so that's our cipher text this is our random stream this lowercase k is a stream of random numbers so you can use RC4 as a random number generator even if you don't want to encrypt something just apply this while loop and these numbers of course you'll be having 8-bit numbers these will be a pseudo random stream of values and we simply x or that pseudo random stream with the plain text so again like with most of the ciphers we go through you don't have to remember the algorithm and I can't even think of an exam whether I've given you do a calculation but it takes a lot of time to calculate but understand the complexity compared to the other ciphers this one is much simpler to the block ciphers and that's the the goal of stream ciphers to be simpler and faster to implement but still provide security not considered breakable so long as the keys are used properly there is there's a weakness in the the algorithm if if the keys in many many protocols they use an initialization vector with the user chosen key and there's a weakness if those initialize that initialization vectors are too small and that's what happens in WEP can we give an example I'll come I'll come up to an example let's summarize and go through an example so that finishes random numbers and our quick example of one stream cipher there are others but RC4 is quite popular so up until now we've gone through block ciphers given some examples AES is one of the most popular ones in use today we've gone through random number generators in particular pseudo random number generators because our computers cannot easily generate true random numbers and we use those pseudo random number generators normally in a stream cipher like RC4 RC4 is its own pseudo random number generator we also see later that random numbers are important in other parts of cryptography the next topic we're going to move so I've done symmetric block ciphers and symmetric stream ciphers symmetric in that still both sides have the same key RC4 both sides need the same key desk AES both sides need the same key the next topic is focusing towards asymmetric ciphers where there are two different keys one of them is public and one is secret and we need to go through some mathematics to understand that so the next topic some number theory but for the last 10 minutes today let's finish with some examples instead of the example on WEP and RC4 I think we'll go straight to the homework so you can do that so the homework is described on on this web page you can see the description what I'll do is give some quick examples on the screen here it looks complex but you'll find once you understand what the commands are doing and I give you most of the commands there are you so you have to use them on the command line and we're using open SSL which is a program to do encryption using many different ciphers it looks complex but in the end you'll find that you need maybe 10 or 12 commands to do the whole entire homework there's from last year we had a similar homework different some different questions but using the same software so I have some screencasts that you can check and you'll see the commands in in there but let's do them now quickly looking at open SSL I want to encrypts a mess a message so first I'm going to create a message and you can use a text editor but importantly what we're going to do is we want to encrypt a message of a particular size I'm going to use desk and we know desk operates on 64-bit blocks so for convenience I want to produce a message which is a multiple of 64 bits and in the first case well yeah a multiple of 64 bits so let's produce a message which is a multiple of 64 bits and I need to find a message I may have one back here we go so I'm going to use echo all that echo does is writes to a file well writes to the screen but I'm going to output into a file so echo the minus n option means don't add a new line so I know the exact length and I've just got some string here and I've calculated before the number of characters and I'm going to put that string into the file called plain text txt and if I look plain text txt's 48 bytes okay the length is 48 bit bytes which is a multiple of 64 bits because 8 bytes is 64 bits this is six blocks of 64 bits and I want to encrypt that with des sometimes we'd like to look not just at the ASCII representation but at the hexadecimal or binary and one program to do that is xxd and if we input a file name it shows us the hexadecimal representation of that ASCII because the ASCII each character is in ASCII is mapped to a 8 bit value a 1 byte value and of course we can represent that in binary or hexadecimal here the hexadecimal values so two digits here 48 corresponds to one byte because each hexadecimal digit is one is four bits if you want it in binary use the minus B option for binary and it formats them in a number of columns let's make let's see if it fits on the screen eight columns doesn't fit so well I'll try again that's I think it defaults to six okay so this is my ASCII text this is the raw form in binary so the first byte 0 1 0 0 1 0 0 0 is in decimal what is it six 72 is there right 72 in decimal if you look in the ASCII table 72 should correspond to the letter uppercase H okay so the mapping using the ASCII table I want to encrypt that 48 bytes of plain text using open SSL open SSL does many things regarding to security and uses many different ciphers and not just ciphers one thing it can do is produce random numbers we know with if I'm to encrypt with des we're going to need a key and we also need an initialization vector I'm going to choose random values and to produce a random number we can use the ran function with open SSL and how long should the key be for des how long should the desk key be 64 what 64 des should be 64 bits or 8 bytes so we pass we want a random number which is 8 bytes in length and I want to produce it in hex as an output and there's a random number okay in hexadecimal and I'm going to produce another one a different random number okay so there's a way to produce random numbers using SS open SSL there are other ways as well but that's one and now we're going to use open SSL to perform encryption and this is the main command you need for your homework once you know this the other steps are easy and we say we're not producing a random number we perform the operation for encryption okay in fact it covers block cipher encrypt and decrypt we specify the cipher I'm going to use des and we also should specify the mode of operation for example ECB electronic code book I want to encrypt let's give the minus e option it's not needed because by default it encrypts minus e means encrypt my input file is the plaintext file I created before and the output I choose some name cipher text doesn't matter what you choose dot e and c to mean it's encrypted what else when we encrypt with open SSL we need two inputs we need a key and an initialization vector and you can choose them I think in the homework I say choose an initialization vector of all zeros I'm going to choose a random one and we specify in hexadecimal and a key uppercase k and there's my random key I'm going to use so that's the main way to encrypt with open SSL we choose the cipher and the mode of operation if we want to decrypt we'd use a minus d here instead of encrypt the input in this case plaintext the output file name because the cipher text will go into a file we need an initialization vector in your homework you should use all zeros here so everything simple but it doesn't have to be in general and choose a random key now one more thing open SSL adds some small in fact a one byte integrity check at the end of the cipher text to check very simply if there are any errors we don't want that because we haven't covered integrity checks yet and if you it may optionally add some padding I know my message is an integer multiple of blocks so I don't need padding so you should use the no pad option so say we don't want padding because I know my input is a multiple of 64 bits I created it that way and yours should be as well and I encrypt and we now have cipher text and see which is a 48 byte file that's our that contains our cipher text and we can look at it using xxd because it's binary it it's if we looked at it in a text editor it would not display very well in binary form let's look at the cipher text and there it is so that's the cipher text in this case if we encrypt using what we use ECB so remember with this we operate on 64 bit blocks so here's 8 bits 64 bits is 1 2 3 4 5 6 7 8 the first block goes from here to here 64 bits and then with ECB we take the next 64 bits of plain text and encrypt in the next block which is here and we have in this case six blocks six by 64 bits there's our cipher text so now you have the commands to encrypt you can change these options especially the mode of operation different key and so on different file names you can display the binary using xxd minus b or even the hexadecimal form because some of the tasks need you to look at the binary form any questions so the tasks in the homework that's almost the first task done except you need to select the plain text a little bit different then the subsequent tasks use different modes of operation and one of them asked about the avalanche effect which is now do it again but with a different key remember the avalanche effect says if we change an input by a small amount we should change the output ciphertext by a large amount I want you to measure that and see if it's true for des so change the input key by a small amount a small amount is one bit so choose a different key which is different by this one by just one bit encrypt and now compare the output from this one with the second one and see how many bits are different and count them do it several times and look at the average on average how many bits differ when you change one bit in the key and discuss that so try the assignment do it by next lecture we're out of time for today next week we will start on the next topic discuss a little bit about the assignment and then prepare for the mid-term exam