 Checking on my phone and yeah, we're live, but now I'll put my phone down and pay attention. Right. Welcome everyone to Bitcoin for developers one unit to here again. I'm going over the sort of bonus session going over this material assuming that everyone here is already pretty familiar with the material or has gone through unit to so what we're doing here is just kind of You know, got some nice pictures to get some visualizations for some of the content and we'll walk through the exercise and today is really a great chance to ask lots and lots of questions. So as we go through this, we will pause regularly to answer your questions so as you have those questions please type them in the chat there and Mike will help facilitate sending those questions long to me. Alright, unit to topic so this topic is really all about how Bitcoin uses cryptography. So we're going to talk about you know what cryptography is we're going to talk about keys. We're going to talk about the basics of hashing keys and transaction signatures and keys and Bitcoin addresses. And again, this is kind of you know review of what was in unit to so we're going to go over some of these things just very briefly and then we're going to get to dive into an example, an example and exercise where we'll get to actually play with public and private keys using Python. So hopefully that will be fun and helps us get familiar with how to use you know cryptography key pairs etc. Alright, so we will go ahead and get into it again after each sort of segment here I'll pause regularly to answer questions so please do type the questions as they come up. Alright, so what we're focusing on today is how Bitcoin uses cryptography because of course Bitcoin we call it a cryptocurrency right and Bitcoin you know spawned the whole crypto industry right and of course this crypto and cryptocurrency all comes from cryptography right because these systems and Bitcoin are you know make a heavy heavy use of this technology right it's it's a necessary component for Bitcoin so we're going to dive into that today just starting at the top what is cryptography and the different types of cryptographic functions that Bitcoin uses and how it uses them. Alright, so Bitcoin uses many cryptographic functions right there's a heavy reliance on asymmetric cryptography. So we'll cover that briefly talk about public and private key pairs it's also what we'll use in our exercise today. So, Bitcoin uses this for signatures right to sign data specifically you know the blocking and unlocking scripts that you need to transfer funds on the Bitcoin blockchain, we won't get too much into scripting today that's unit seven. Alright, Bitcoin addresses will talk briefly about how some big Bitcoin addresses are derived from public and private key pairs specifically from public keys derived from public keys a lot of addresses. Alright, we'll talk about hashing because Bitcoin uses hashing constantly all over the place hashes everywhere. It uses them in mining, sometimes in signing because sometimes you'll hash data before you sign it uses them for creating addresses etc etc etc uses many many different types of hashes will primarily shot to 56 but different types as well but it uses them all over the place. Alright, and what's really really interesting here is that I think this is a common misconception which is very understandable due to the name right say Bitcoin is a cryptocurrency and when we think of cryptography we think of encrypting data right, which is obscuring data using some key or some algorithm to obscure it so it looks like nonsense but actually you can then decrypted and get in a useful message out of that. What's interesting is that while Bitcoin is very reliant on cryptography and different cryptographic functions, it does not encrypt any data. There is no encrypted data on in the Bitcoin system night, you could somehow encrypt some data and hash it and embed it in the blockchain and some capacity but the Bitcoin protocol does not rely on encrypting any data. This is a common misconception. So let's make sure we understand that we use all kinds of cryptographic functions for signing and hashing etc but no data is actually encrypted. Mike, could you hear the my computer loudly telling me that I just got an email should I mute that or is it okay. Okay. Well, apparently I just got an email. Alright, so we're going to talk about this briefly then we're going to go through you know encrypting signing and hashing and then we'll come back to this again so this is one of my favorite graphics for explaining all of this right this showing the difference between encrypting data signing it and hashing it so if we look at this first one. This is the sentence the quick brown fox jumps over the lazy dog I think that's just the sentence you always used to show fonts, right because it has like every letter in the alphabet. But, um, so we just let's say this is our, our message, right this is the data we have and then we're going to encrypt it or we're going to sign it or we're going to hash it. Now when you encrypt a message, right you get, you know, something like this out at the other end, right this is just example text this is an actual encrypted data here. It's just like this it just looks like nonsense like scrambled data like that's not English right that doesn't look like any language I'm familiar with it just looks like nonsense right because we have obscured that data we've encrypted it. And if we did actually take this information here and run it through an encryption algorithm and got encrypted data out at the other end, then there would be a mechanism to decrypt that data and turn it back into this you know human readable over here. But it's just showing what this looks like you take this data when you encrypt it you turn it into something that looks like nonsense right it's not human readable it's not even computer readable unless you have that decryption key. Alright, now in contrast let's look at what happens when we sign something here we have our message, and we put it through a signing algorithm and now often things are hashed before they're signed but that's not we're showing here we're just showing simply this message with a signature. And what happens there is you take the message and then you create a digital signature from that message using private key. And then you get this signature here. So we'll talk about that a little bit more in depth and then I believe next week we actually get to go into signing and verifying signatures which will be fun. But this is a bit what this looks like you take this message and you just add to it then you add this signature that goes along with that message and that's a bit what signing looks like. Then we'll take hashing again we have the exact same message this time we run it through a hash function and out the other end we get something that might look a bit like this. Now shot 256 is you know the hashing algorithm that's used quite a lot in Bitcoin and they're the you know digest like what you get out of that hash can be a bit longer than this but this is a good example. You get this just unique identifier of this data. Right, so you can see these are all different cryptographic functions which do very different things for us whether it's obscure the data, add a signature to it for verification or just give us you know this digest or this identify identifier for that data. So here's this graphic here and we're going to walk through some of this stuff and then we'll come back to this. All right elliptic curve cryptography and very cool stuff we won't dive into it here in depth because we already covered this in unit two. And you know, you don't like if you want to work with Bitcoin technology if you want to integrate it in different places and use it like you don't need to be a cryptographer. The, the level sort of the depth that it's covered in unit two where we read a, I think a chapter of mastering Bitcoin, and it talks really about how these signatures are created these trapdoor functions this curve, cool stuff. That's really like the level of knowledge that you're going to need to work with this technology. Right, if you read that portion and you understood it. And here you know you can see it's a very lovely curve this elliptic curve cryptography. But if you're working with this tech, then that's sort of the level that you'll need if you want to become a cryptographer that's a, you know, whole another field of study then you really want to understand in depth the math behind this stuff. Or maybe even if you want to get into Bitcoin protocol development then you'd want to otherwise for us what we read in unit two is going to be pretty sufficient to really for us to understand how to work with this technology you really need to get public private keys, the difference there how they're derived why one can't be turned back into the other etc. But once you got that you're pretty good good to move on and start you know playing with the software in other ways. Alright, so briefly we'll talk about the difference between symmetric and asymmetric encryption. Alright, so with symmetric encryption you have let's say a key, right, or a particular encryption. Let's say an algorithm right but think about is having one key. And this key or this method of encryption, obviously is generally something that's pretty mathematically, you know sophisticated right very very cool interesting encryption algorithms sort of stuff but here we'll go with the very very very simple example to the difference between symmetric and asymmetric encryption and so we'll say our encryption algorithm here is just to take a message and for each letter in that you know the message is you know a string in English and for each letter. In that string we're just going to take that letter of the alphabet and move it over to right. So then hello becomes this gobbledygook right this JG and Q right which is not easily human readable. And this is a very very simple example but okay this could work so if we have Alice and we have Bob and Alice wants to send encrypted message to Bob just wants to tell him hello. She can use this encryption key here to encrypt this data and send it to Bob, but then the problem is alright so Bob would be able to decrypt this message. If he knows what the key is if he knows that we're just moving, you know, two letters over in the alphabet. But the problem is how do you share that secret key. Now, if Alice and Bob are middle school students and are sitting next to each other in class they can just like whisper it, you know, and they've shared the key they know their secret between each other they can then send each other encrypted messages right. But if you're on the internet, and Bob's on the other side of the world, how is Alice going to deliver him, you know this key this method for encrypting without other people seeing it and then being able to decrypt that data themselves right. So this is the problem. And lots of lots of very clever math later we get asymmetric encryption where you have key pairs and this is really the magic here this is cool stuff so you have public and private keys. And a public key can be used to encrypt a message. So again if we have Alice and Bob. Alice has a private key she has a public key. Bob has a private key he has a public key. When Alice wants to send the message hello to Bob but he's on the other side of the world. What they first do is just share public keys with each other out in the open out in the clear they can post it to Twitter right Alice can say hey Bob here's my pub key. And Bob can say hey Alice here's my pub key now they might not want to do that they might want to keep some you know, they might not want to put that out in the open for other reasons but you could right. So someone could intercept that. But what Alice can do is take Bob's pub key and encrypt a message using Bob's pub key, send it through to him and then only Bob because we're assuming he's the only person that has that private key. Only Bob can then decrypt and read that message and vice versa. Bob can then respond with how's your day going and encrypt it using that Alice's public key and send that out on the clear on the internet someone could look at it and to be like yeah that's gibberish I don't understand it once it reaches Alice she can use her private key associated with that public key that Bob used to encrypt that message and she can decrypt it right. So this is really really important because it solves this problem of how you transmit this key the secret key to decrypt a message. And it's while this gets used in many different ways this is a really good example to keep in your head to just understand why this is so useful and the different components in this key pairs public and private keys. All right, yep we've covered that. All right, so that was a symmetric versus asymmetric cryptography very quickly let's just talk about signing messages and then we'll pause for questions. All right, so cryptographically signed messages. All right, so signatures provide strong assurance that a signed message is authentic and has not been tampered with. This is really really important for a lot of different types of Bitcoin transactions right. I can say okay the blockchain says that I have you know, five Bitcoin at this particular address right. And if I want to send a transaction out on the blockchain that says I'm taking one of my bitcoins and I'm sending it to Charlie right. And I, you know, well, there has to be some sort of mechanism for knowing that is actually me. That's the owner of that Bitcoin that does indeed intend to send it to Charlie right and how you do that, at least in a lot of cases. We'll talk about scripting in different ways of doing things in unit seven but in a lot of cases what you do is you just sign that transaction, right. And with cryptographic keys you can prove that it did indeed come from the person that's associated with that public key right so you know that that message had to come from the private key associated with a specific public key right that's the only mathematical way that signature could have been created, at least that we know of right. And we can also prove that that message that I signed hasn't been been altered right Charlie can't just change that transaction all of a sudden I'm sending him to bitcoins instead of one right. So we can prove. We know, like where the message came from and that it hasn't been tampered with. That's really important in this case. Alright, signatures can be used to prove that the message came from someone in control of the private key and the public private key pair. Yep. And to validate the signature right so we can go back to that think of that graphic where we have the message and then we have the signature on that message. And then next week with some Python will should be fine. But what you need is you need that message. You need the public key associated with the private key that sign that message, and you need the signature. That way if you have the message you have the signature and you have the pub key from the person that the message came from you can use all that data to verify that signature and be pretty well sure that that message has not been tampered with. That's how Bitcoin uses signing again this isn't slightly simplified version, but this is you know why it's so so important to Bitcoin is in many many cases it. It's used to be sure that the person sending that transaction out is the person that actually owns that Bitcoin right and the message has not been tampered with. Alright, let's pause here because we went through a lot of stuff pretty quickly and Mike do we have any questions. Mike can you hear me. Alright, there we go. Excellent so we will roll along then. Alright cryptographic signatures in Bitcoin I should have done this slide before we pause for questions. Alright, so cryptographic signatures are used in unlocking scripts. And again, we go in depth on Bitcoin scripting in I believe it's unit seven. So we'll just note that it's used quite a lot cryptographic signatures are used quite a lot there, and then we'll move along. Alright, now hashes. Bitcoin just hashes things constantly. So it's used in all sorts of different ways. Shaw 256 is the hashing algorithm most used in Bitcoin but there's also like 161 we're going to see it on a couple of pictures here and we talk about address generation, but let's talk about what a hash is briefly. So a hashing algorithm or function takes data, any amount of data, right, whether it's one letter or like you know all of Shakespeare's works and produces from it a fixed length unique identifier. So here's some qualities or properties of hashing algorithms or hashing methods or functions that we look for that makes makes it very useful for us right collision resistant. That means if I type up a message, right, if Alice types up a message to Bob just telling Bob about how her day went and Bob types up another message back to Alice telling Alice how his day went. These are two different messages, and you know they're different content in them and so you should be able to hash both of them with the same algorithm but get a different output. Sometimes called like digest should get a different output from the these two hashes from these two pieces of data should be different. Right. So with the good hashing algorithm you should not be able to hash two different messages but get the same output, right the same digest the same identification sort of ID for these pieces of data they should always be different. So that's important. Also deterministic meaning if Alice types up this letter to Bob, if she hashes it today she'll get a specific, you know, output from that, a specific identifier for that data she has has has the exact same data tomorrow to recent him that exact same message, she should get exactly the same identifier out that right so the same input should produce the same output every time. And fixed length digest. So, with a lot of this stuff the idea is to give away a little information as possible. So you want to hashing algorithm that takes all different types of data, you know, just a little bit of data very short message a hello to like a whole, you know, autobiography of my entire life right and all of Alice's life right shoved into text and then both of those things has should produce an output that is of the same length both, both unique collision resistant deterministic and of the same length. And when you get that then it becomes really really powerful for all different kinds of use cases. Alright, and hashing is a one way function, which is important to note that when you hash something, you don't unhash it right it's not like encryption where we can decrypt it. There is no way that we know of currently right to reverse engineer hash to take a specific hash and then turn it back around and take this just you know, 260 fit 256 bit string and then reverse that and turn it into all of Shakespeare's written right like there's no way to do that. That's not currently feasible. So that's just very important to keep in mind this is not like encryption it's one way there's no undoing it. And here's just a quick example of that. Here we have three different messages. Right so first one is just a very short one, Fox, right and we shove that through a hashing function and here we get just an example output of a specific length. And now we take a much longer message. The red fox runs across the ice we shove that through the exact same hashing function boom we get a different identifier right but of the same length. Then we take that message and we change just one tiny little thing about it and it can be as tiny is like changing a period to an exclamation point right exclamation mark. And if you change anything about the message here we just change it to the red fox walks across the ice, you run it through that hashing function. Boom, you get something different right of the same length the output is of the same length but it's different. And that's really important to note so here this just hopefully helpful visualization of how this works. All right, how do we have a question real quick here on okay, that's a good time I thought would be a good time I wanted to wait till we finished the the hashing present presentation. So the question we have here is. So my understanding is correctly. The message is confined somehow with the public key, then has to produce a signature. Is that the cotton. Sorry, is it is the combination techniques stand standardize standardized or something is the cut. Yeah, sorry. Is the combination of techniques standardized, I believe is how I should say that. You dropped out for a second there though might be my internet connection but can you walk through that question. Let me give it to you one more time I might have that might have been on me as well. Okay, so the question is the message is combined somehow with the public key and then hashed to produce a signature. Okay, so how do you use hashing and signing together right so there are two separate functions. They are sometimes used together in some situations. You may want to take a message, hash it run it through a specific hashing functions then take the output of that message like the has digest, then use a private key to produce a signature for that specific message. And then if someone wanted to, then you could send that out to someone and that would be that what you would send is that hash digest, or maybe you would even send. Well, depending on your use case right, but you could send that hash digest your pub key and the signature and someone be able to validate that you did sign that digest. But then you maybe also they already have the original information that went into creating that hash. So we would be able to if they knew what hashing algorithm used, take that message like recreate it take that message produce run it through the algorithm see that that hash matches that message so we can know that that message wasn't tampered with, then validate the signature on that hash to again be sure it wasn't tampered with and it came from who we thought it came from. Let's not get to into specifics there but there are sometimes you know people will do things that way maybe to save space or for a variety of different reasons but that is a way in which you would combine both hashing and signing. So that answers the question. Yeah, so there was a second part of the question which you might have answered but I don't know so I'll just ask anyway. So that's not like what you're talking about there that's not, you know, a standard a standardized technique. In a lot of places that that's used but you so there are libraries and functions and stuff that'll let you do those two things together but you don't have to write and it totally depends on your use case. But definitely I don't even know that might be libraries and stuff to. Yeah, I cannot thinking of a clear example right now but definitely there can be systems where you would want to use those together and you would, but you don't have to. Okay, well hopefully that answered the question and I'll let you know if we have any other questions and feel free to leave them in the chat as we go along. I'll go away. All right, talk to you again in a minute. Okay, so hashing uses so hashing can be used as unique identifiers for data. They can be used to represent data while obscuring the data itself so maybe you want to reference some data that some party has but don't want to, you know publicly post that specific data you could post a hash of it. And as far as unique identifiers a lot of times when we're talking about software. They'll be you know a particular build of software that then gets hashed to a unique identifier so that when you're downloading it you know you're getting that exact version and then you might even have a hash of that well then maybe that hash was signed right so that you know it came from the developer you know it came from and no one had tampered with it. In the meantime, as you were downloading it so that's the case where you might have both hashing and sign and signing going together. All right, and then we can detect data tampering right again if you change even like anything for change anything about the data going into that hashing function you're going to get a different result out the other end so you can't even see the situation mark without that digest the output of that hashing function being different so it will help you immediately spot any data tampering. All right, and of course this is used quite heavily in bitcoins proof of work mining which again we dive into a bit more in depth later in the course and Bitcoin addresses so let's take here we go. The graphic I was talking about earlier or we can see how. This is used here for generating Bitcoin addresses. Now there's a number of different types of Bitcoin addresses this one's a very simple one pay to public key hash right. This is the simplest sort of form of Bitcoin addresses and what you would do is you'd have a key pair public key and that private key. And what would happen is you would use that private key to create a Bitcoin address once Bitcoin gets locked to that address only the associated private key can create a transaction and produce a valid signature to, you know, then move or sort of unlock Bitcoin. So, once we generated the private key right the bitcoins off where we generate the private key then we would take that public key we run it through this double hash. So, first Shaw 256 and then ripe md 160. And this is used largely here because we get the security of Shaw 256 which is a very, you know, like heavyweight, you know, algorithm for hashing very secure and then you run it through the second one because it sort of creates a shorter digest shorter output. So it reduces the amount of data which goes on the blockchain to save space, etc. And so you take this hash and then you encode it with base 58 which is really cool and we talk about later in the course to I think want to say unit four but talk about that later. And then we add, you know, some checks on like we had some safety checks into there again we'll talk about that later, and you get this Bitcoin address. And you can see here how you take this pub key, run all this hashing algorithms then some encoding, and you get this Bitcoin address. Oh, this is the check encoding. We'll come back to this one. When we cover that later from this base 58. It's cool stuff. So, Mike, do we have any more questions on hashing before we are any of the stuff we covered. Let's go to this slide here. Again, now we talked about encryption talked about signing we talked about hashing. Let's cover any questions before we then get to go play around with this stuff in Python. I am not seeing any questions right now in the chat. I'll keep I'll keep an eye on it. I'll talk a little bit more right now just in case anyone has any questions and they're, they're going ahead and in typing away but I'm still not seeing a come through and so I guess I mean we can move on we can always you know we can answer questions while we're on the other screen as well. Okay, cool. Alright, so we are going to move along then to our exercise which is to encrypt and decrypt data and again while Bitcoin doesn't actually encrypt any data going through the process of encrypting and decrypting some data via Python will help us get familiar with the use of public and private key pairs. Right, so it'll be fun just it's a good intro to using this tech, and we'll walk through it in Python. This is the example found in unit two. And I'll just walk through it here, and we can answer any questions as they came come up. Alright, so first, we need to note that you have, you'd have Python three installed. I'm doing this here on my laptop which is running Ubuntu and I've installed Python on it. So I'll walk through that here. But there's also you can do this online there's all sorts of websites you can go to to run Python in the browser I was doing this the other day and replete. And that's you know they have Python so you don't have to do this on your machine. You can do it in a browser at various different websites to let you do that as well. Alright, so we also need to install this particular library here, because we're using some cryptography that isn't just standard to Python. Right, so we have to install that. And then we're going to create this file, and I'm going to drop out of this screen share. And I'm going to pull this up. Hopefully, you all can see this here. I have this example. And I have lots of stuff commented out but we'll go through it step by step. And then I have on my computer here, my terminal window. And you can see here this is the file that we're working with now and we're going to actually generate a key pair we're going to look at how to mess with the, how we're storing that etc. And then we're going to encrypt and decrypt some data. And again this just helps us get familiar with using public and private key pairs and what they look like and how we can generate them etc. Alright, so let's go ahead and get started so I'm kind of going to be bouncing back and forth between these three windows here. So the first thing we want to do is input the libraries that we'll need. So again coming back to this code here. Python we will, we're going to use from this library we're going to import these functions here. It's just Python syntax to say hey we want to use all of these things in our script below. Right. Now, if we're talking about encrypting and decrypting the first thing we're going to do here is create a message to encrypt. So there we go. And kept it nice and simple. I love cryptography. So we've done that we've got our libraries we're going to use we've created this message, but of course in order to encrypt we first need to generate a key pair. So let's comment this and have a look at it. So here we are generating a private key, and we're using sort of this function here to do it we're saying hey we're going to use this library we're going to generate function to interrupt real quick before we get started we had a question down in the comments. Is there a place where someone could find these files to follow along. So they are in. I'm taking these examples from the exercise for unit two. So this is all in unit two. And if you have to be linked in the description below. So make sure I do that right now. Okay, excellent. Yep. That was it. Cool. All right, where were we so we're generating a private key. So we're using this library and this function and then we're telling it a bit of data, telling it like what type of private key, we want to generate and we won't dive to into that here, but it's important to note. That there's all different varieties right so there's all different kinds of private keys and key pairs you can generate there's all different sorts of you know curves that they're using to generate them. I am I am not a cryptographer so I won't attempt to dive into the specifics or the merits of one versus the other but it's important to know that when you're you're writing this code and you're generating these keys. You have to select an appropriate key type for what you're looking to do. Right. So important to keep that in mind here. I think this is just pretty standard one we're generating right here. Okay, so we always start with that private key. And that's to kind of think of it just like sort of a random number but has to be a specific format and there's all sorts of you can really get into depth with how you truly randomly generate something. That's really interesting topic when it comes to Bitcoin because if you can figure out how someone's generating private keys then you could potentially find their private key and steal some of their funds so that becomes a really important topic of conversation in Bitcoin security. But another really really important thing to note here which I should probably put in the slides and continue to put in the slides for the rest of the course I'm actually going to note that hold on. Don't roll your own crypto crypto. Alright, and this is a saying that's commonly said in the Bitcoin world and it means that like, like you got to know your skill set right so I'm a developer I work with Bitcoin lightning all of this stuff I am not a cryptographer right so when I want to go and make use of this technology I am never going to be writing right you know my own cryptographic library right I'm going to go find a really well tested open source very you know respected cryptographic library that's appropriate for my use case and I'm not going to use that because you can get into self into so much trouble if you try and make your own right you know or even not even making the cryptography cryptographic functions yourself but maybe just using them in unusual ways. So if you do that, right if you roll your own crypto. You're quite likely like here. Sometimes people do this to try and increase security but it's much more likely that you're going to wind up losing your funds just due to error and having not tested well etc like you're more likely to. If you roll your own crypto, you're more likely to have funds lost due to your error than from someone stealing it from you so please just use you know well respected well tested established, you know open source libraries for this stuff and don't don't make them your own unless you are a cryptographer, then please do so. But if you're not don't right don't do it. Okay, so now we have a private key now we take this public key and we take this private key that we've generated here and then run this function on it to generate our public key. So public keys are always derived from private keys again that's discussed in the book we'll dive into it here that's in unit two. Now, we want to print out our keys to be sure that we've generated them properly right so went and generated them. And want to see that we've generated them properly right this is important we want to make sure we're getting out what we expected. And so, for this particular bit of Python what we're expecting is that this is a crypto public key RSA RSA key. That's the type of key we're using here right. So oops, I didn't mean to do that so I meant to hit this so I'm saving this file. Right, I'm coming back here and I'm just going to. There we go. I'm going to run this file and see what kind of output we got. Boom. There we go. I get the output I was expecting. This is the type of key that we have generated this is the class here for it. So far, so good. Let's roll along unless we have any questions specific to what we're doing here. Not hearing any questions right now. Cool. All right, so let's move along to the next one. All right. Now we want to convert our keys to strings and save them in dot PEM files, right. And this can be very important depending on what type of systems you're working with what you're doing, you're going to have to have some sort of way to not. Well, quite often you need to generate these keys and then you need to hold on to them store them in some fashion so we should know a little bit about how to not only generate them but how to put them in files and save them. So here that's what we're doing. We're taking this new variable here which is this private PEM we're taking the private key, and we're exporting the key and these are all, you know, Python functions which we won't drive in depth on here I don't even know is precisely the details of this one. But this is how here we're going to save these in these PEM files. And then we'll double check that that worked by printing out. Again, we're going to save we're going to come back here and run that again. Okay, cool, but let's see. Hold on. What did I do here. Oh, so right now we're just printing the type, right. So we've converted what we did here is we converted those keys into the type PEM. And then here, excuse me, I got ahead of myself. Here is where we are saving them we're actually writing them to the files. So write them save the strings right so we converted them to strings we save them to PEM files. And then here we're going to print them out. Okay, so let's see what we got going on here. Okay, so this is what these type of keys would look like and this is very standard key format right begin RSA private key. So this is what this RSA type key will look like. And then this is the public key generated from that. So we can go here and that there we go we created them and again we can do the same thing here. And we can have a look at them. Alright, so we're doing pretty good so far. We have created these key pairs we've converted them into this PEM format we've saved them into files and we can have a look at them. Now let's convert these key files back to RSA objects. Okay, so now we've got them saved as files and again this is just like stuff that might come in handy for you in the future as you're working with this tech right. Here's how you generate it here's how you change formats here's how you save it okay now you have this file saved somewhere. How do you then convert this back into something that we're then going to use right. So, then let's convert that back into RSA key objects and now we're taking a file and converting it into an RSA key objects, and then we're going to print that back out to make sure it's working. So now we're getting a lot of output every time we run this file but it's good to take a look at it. So let's run it again. Here you can see all the previous stuff we've done and then boom here we're doing at the end. Yep, these are now RSA key objects, right so we've successfully read those files and turn them into objects that we can now use. Okay, so now we get to the part we actually use them now let's encrypt our message. So, here we're saying what type of cipher right we want to use what sort of encryption message or method we want to use. So we're going to use this method here. We're going to create a new instance of it I believe is what's happening and then here's the key that we're using. Yep, pu key right there we go. Okay, and then here we create our cipher text using the message which again we created all the way back up here. All right, so now we've told Python which keys we want to use we've told it how we want to encrypt it and we've given it the message to encrypt. So, let's uncomment this save the file now we should be able to encrypt that and then print it out to the screen so we can see the encrypted version of it. And boom there we go. You can see to us that looks like gibberish right that is not human readable nor is it really machine readable it's just gibberish right it's not readable unless you have that decryption key. And here is where we do that once again. This is just giving you examples of how you can encrypt and decrypt in Python. Again, Bitcoin doesn't really do this but working with this tech you might want to do this in some cases. So, we're going to decrypt. Right here's the key that we're using to decrypt. Yep. So, we encrypted with our pub key we are decrypting with our private key. You can see here, doing private Keith we're reading from that file. All right, and this is you know the library we're using for we're using a new decrypt with this key, then we're taking the message, right which is our cipher text or encrypted text from up here that we just printed out and we're running it through the decryption algorithm. Then we're going to drop it back onto the screen to print it back out in a terminal to make sure that it worked. So again, we're taking these files that we generated we're reading them with Python we're converting them into these objects we are encrypting the data with this public key, then we're printing that out to the screen, then we're decrypting it with the private key and then we're also printing that back out to the screen, or to terminal here. There we go it works. We once again have our message I love cryptography so here it is we don't print it out of there. Okay, so here it is in this encrypted format. And here it is with its decrypted format and then I think I just added this last line in here for fun. So if we come back there and do it again. So we're done we successfully completed our exercise. And now is a great time to stop and go over questions on this. So Mike if there's any questions and you know we don't want to go to in depth on the specific math or functions here, but discuss more like how this stuff is used or where we might use it. So Mike do we have any questions. I will give people give people a chance I don't know if this falls into into your category or not but there is a bit of a discussion about the idea of public versus private keys and whether the same thing or different things but I don't know if that's. Yeah. So definitely different things and this is one of the key takeaways of this unit is that that, you know, for a lot of, you know, bitcoins really built on, you know, this asymmetric encryption using these key pairs. Private keys is a you know, a bit of information right sort of a string or really I guess a number that you generate hopefully randomly. And you have to keep that private and then with that key, you can do all such all kinds of things like you know sign data, etc. Yes, there are very different and it's very important to get those differences. Pub keys are used to encrypt private keys can be used to decrypt private private keys can be used to sign data and then pub keys can be used to verify the signature on that data. So, very, very cool stuff and there there's so much in Bitcoin in the Bitcoin world that depends upon that asymmetry asymmetry in, you know, cryptography using these keys asymmetry and how different scripts are used. So, yeah, very important to keep that in mind there's definitely a big difference between public and private keys. Thank you. I'm not seeing any other questions in the chat. Yeah, so if you guys have any questions, feel free to put in the chat if not if you're watching this later you can always put them in the comment section below and we can get them to another time. I just want to, you know, van for a little bit give everyone a chance but thank you everyone for joining us. Thank you of course, Hannah for taking us through all of this. And next week we will be doing unit three, the unit three of this course at the same time. So get excited. The link to the course is in the description below. And again, thank you so much.