 Hey everybody, this is Brian. Welcome to the 150 first cute tutorial C++ and GUI programming. We're gonna dive right in here What we're gonna do is we're gonna make a new project We're gonna make a console application And we're gonna say slash code or wherever you keep your stuff This is actually a little important this video on where you put your things because we're gonna be linking to a library I'm gonna call this open SSL demo and what do I mean by it's important where you put it? You're gonna have to Link to the open SSL library. So let me I've got mine. Where is it? Right here open SSL demo, but I also have this folder called libraries and inside of there I have my open SSL lib that's been compiled Once again, I'm not gonna go over how to compile this you need to follow their directions I won't cover how to compile it because their directions may change and I don't want someone to you know Follow the wrong directions and then say hey your video sucks when somebody else changed whatever So all right first things first what we need to do is We're gonna generate some RSA keys And we do that by let's just open in terminal We're gonna work with the open SSL command line So if you've never worked with the open SSL command line, it's actually very simple you type in open SSL Then you just hit tab to auto do that So Jen RSA out And we're gonna say private that PEM we're gonna generate our private key with a strength of 2048 bits What this does is well you guessed it generates your private key now remember your private key is Well private meaning never give that out to anybody. It's top secret. You keep it just to yourself Now what we need to do is take that private key and Extract a public key out of it PEM I'm gonna say pub out And we're gonna redirect that to public dot PEM Now I forget the actual name of it, but there's a mathematical principle about public and private keys meaning they're mathematically similar So what this does is it takes your private key and from that it actually extracts if you will The public key and let's look at these real quick here. So you can see the difference I'm gonna Pull my notepad over here. This is a private key. This is what it looks like You can go ahead and screenshot this all you want because I'm gonna delete these keys and this is not what I use for my encryption This is strictly for this tutorial. I just I know there's people out there going. Oh my god I got his private key. Well, no, whatever So you can see it's fairly lengthy Now what we're gonna do is we're gonna look at the public key. We're gonna say I I did not want to live your office. Thank you. Live your office. Oh boy. Get it. There we go There's a public key There's the private key. You can see how the public key is quite a bit smaller than the private key So the reason for that is the private key is well Stronger it's much stronger Whereas the public key has a lot less into it. It's more of a I don't want to call it a hash But it's a simplified version of the private key They're mathematically similar, but they're totally different. All right Pull that out of there. Gosh got my notes all screwed up here. There we go Can't believe I opened the office on an accident here So why are we generating the public and private keys? Well, we're gonna embed those in the application In a production one you would not do that But for our little example we're going to So first thing first we need to crack open this profile here and I'm using a static build I'm gonna use this just because I'm playing around with static And I'm gonna probably do it like a static tutorial in the future But for your purposes you don't have to do static if you don't want to I should note that If you do a static build you are going to need To compile open SSL in static mode That's not actually accurate but When you compile it and you're compiling with static You're going to need to do two things. You're gonna need to include the header files This is what we're doing here include path and then we're gonna need to include the libs And I'm just for the sake of time. I'm gonna copy and paste here and I'll explain it So we've probably done this before in other tutorials if you followed along Include path will tell us where the header files are see libraries open SSL include if we go out to here Libraries open SSL and then include And then it'll search through this path and voila. There's all our headers Now you see this crypto a and SSL a those are the two Compiled binaries is not the actual libraries though. This is what we can link to Notice how you don't release. I don't think you see the SO files in here because I didn't do that So when we compile static these are going to go directly into the program There'll be no need to ship these with anything else I know people are going to scream make a static tutorial. I'm going to just bear with me guys I got to play around with it for a bit. So I'm going to give this a good build Make sure it picks all those things up. There's no errors And next thing I want to do is we are going to make a class Maybe if my mouse quits acting crazy I swear this tutorial is cursed. This is like the third time I've tried to make this tutorial It was like there was a Really loud ambulance that went by and then my girlfriend decided to mow the lawn and man, it's just nuts So what we're going to do here is we're just going to include some things and I'm going to say from this point forward really it's just me typing out Functions Not going to do a lot of hard code coding. So if you want you can just stop the video But if you want some explanation, I'm going to keep talking as I'm typing But the bulk of the program is going to be in the next few videos So in short if you are already an expert in cryptography and open ssl And you came just strictly to critique my typing then feel free to skip this video So we're going to start including the open ssl stuff Now the first one we're going to include is rsi because well we're going to work with rsi Whoopsie We're going to include engine Which I don't really know all of these do I think engine is like the core engine of the cryptographic library Pem is for keys I think it's for keys comf I think is for the configuration of open ssl And then evp evp takes a little bit of explanation RSA has kind of gotten a bad rap for being very complex and hard to use Well, they have these functions called envelopes and that's what you need to use is the envelopes that way you're not writing all this hardcore code I mean if you're a masochist by all means go ahead a self-explanatory by the way But use the envelope that way it's much easier And ran because it has a random engine built in Now i'm going to just save a little bit of typing here and through the magic of copy and paste for my notes Voila, we're going to add some some notes in here Um, if you've never seen this before I should probably explain These are obviously normal comments Open ssl or I should say rsa has a concept of padding. Um, it needs to be padded a specific way Either you're going to use oaep padding or normal padding or no padding, which is dangerous because you have to pad it yourself Or you use pkcs1 padding, which if all that sounds great to you, don't worry I don't know what they all mean either. I'm sure somebody out there is an expert and they'll put it in the comments But I've left those in there. So if you want to play around with it, you can just simply uncomment and use a different padding I would recommend staying with the default that I have in here Um, I've got some defines like the key size because we're going to be working with 256 bit encryption It's going to be 32 initialization vector 32 block size because it's a cypher block chaining Um 256 and then the salt size is always eight So if you remember from our previous video, I'm not going to go into a whole lot of theory You have to have a key and an initialization vector and a salt in order to work with aes Also, it has a block size because it's cypher block chaining mode is what we're going to be using And this if you don't know what this is Um, this is doxygen format doxygen is an automated Well documentation engine it can document your code, but you have to use special markup So you'll see a lot of that in this tutorial So basically this wraps the rsa and aes crypto functions in open ssl. It's compatible with command line It's the exact same thing as doing these commands So we do open ssl aes 256 cypher block chaining with assault the message digested shaw one We have an in file an out file and then a password Um, I should note that it's not really a password We're going to we're going to derive a cryptographic key from the password It's much stronger than using an actual bits from a password itself And just some notes. Hey if you're going to use this thing because I'm going to post this out on my website Be sure to link to the libraries and have the include path Now that that's done, we're going to give this a good rebuild just to make sure that we are Getting to everything that we need here compile output. I love solid state drives so fast. All right So we're good there Now the rest of this tutorial is just me kind of talking and typing as boring as it is I know you guys don't like to watch me copy and paste so rsa Let's see. We're going to make a couple functions. We're going to say get public key whoopsie Q byte array Now you notice how we're returning a pointer whoops a pointer to an rsa What is that? Well, that is a class. Actually, I think it's a structure inside of the open ssl library Um, we do this because what we're going to do is load the public key from either a q file or a q byte array And then we're going to do the same thing with the private key get pro vikki. I don't think so I started playing a new video game called overwatch from blizzard and my key bindings are all kind of messed up and my my muscle memory is all messed up because I Am not paying attention what I'm doing apparently So yeah, I mean some of you gamers out there know what I'm talking about whenever you switch video games You kind of get all messed up So we're just going to write in some notes here Load from a biter way. It's going to return data All right. I'm sorry the pram is the byte array I uh in my professional life. I actually Do comment my code quite a bit. I know in these tutorials. I really don't Um simply because it's time consuming but for this specific little mini series. I'm going to Because Encryption is very complex and you need to know what's going on under the hood here So I'm curious while I'm just typing away at this if anybody else Is playing overwatch because I think it's actually kind of nice I shouldn't call that data. I should call that Oh, yeah data. Why not? That shouldn't be named data And we can't do the copy constructor on that All right, so just bear with me here Definitely feel free to fast forward to this little part here Probably would have been easier if I just would have copied and pasted that to begin with Someone will ask me. Um, there's this thing out there called twitch Um, if you don't know what it is it allows you to live stream like video games Well, some programmers are actually uh They're live streaming themselves coding and someone said brian could you do that'd be so awesome? And I was like no I would be horrible at that Um simply because um a lot of times I actually like kind of curse and sometimes I'll have like the perfect video And I'll like say a very bad word like shazi boo boo or something like that And then I have to start all over because I hate video editing all right, so We have got our Load public load private if you're wondering why I have a byte array and a file It's because well from the file version We'll actually open the file load the byte array and then pump it into this guy right here and then just return that rsa pointer We're going to do that for both public and private then Let's see here We need to encrypt rsa and decrypt rsa Now this is a little different. So we're going to say q byte array And we need an rsa key. You guessed it All right, I'm just going to speed this up a little bit and copy that right out of my notes here Like making sound effects now just to make things more exciting. Someone said my video is a very dry and boring I need to do sound effects So anyways, so what encrypt rsa is going to do is we're going to give it an rsa key that we loaded from You know, you guessed it public key and we're going to hand it that pointer and then we're going to Give it some bytes and it's going to encrypt those bytes and return the encrypted byte array So and then we're going to say q byte array decrypt rsa And obviously what decrypt does is you will take your private key And you will then you know Decrypt that data out. What did I goof up here? Magic of copying paste ladies and gentlemen, there we go. Why is that freaking out on me? Unexpected token. Hmm Probably help if I told it what it was, right? There we go. All right So what we're going to do now is we're going to say We've got encrypt rsa and we've got decrypt rsa now remember we're only using rsa to protect Protect the aes key because that aes key has to remain private at all times. So of course we need you guessed it Not byte order. What are you thinking here keyboard? Jeez Q byte array. Well, if you guessed byte order, you were absolutely wrong We're going to say encrypt aes And we're going to do the aes encryption. So we're going to say q byte array passphrase and q byte array The data and let me grab my notes here I'm trying to save a little bit of boredom for you guys by just copy and pasting the comments here That's not actually right. Hmm. Yeah. What did I do there? So data there we go Oh goodness kitty go away We're just going to copy and paste this guy. So whenever we have an encrypt. We obviously need to decrypt Oops, maybe We'll see what else is new Loaded up diablo 3 again Fun game. I don't know why I ever quit playing that Decrypt. All right. So decrypt This is kind of the boring part of documentation. You got to make sure everything lines up So we got our encrypt. We got our decrypt Now we also want a random bytes. Gosh, why does it keep doing that? I'm telling you this tutorial is cursed. Why is it? There do it do it. What I do what I said All right, there we go So what this will do is it'll call the open ssl function Where we're going to actually you guessed it We're going to generate random bytes and we're going to call the Open ssl function to do that. We're not going to do anything internal with q to see plus plus directly Free RSA key So what free rsa key is going to do is well, you guessed it's going to take an rsa key and it's going to free it from memory We could get fancy and use like a qscope pointer or something like that. But honestly, I just want to keep it nice and simple um, I mean our main focus here isn't really memory it's more You know, how do you work with open ssl here? So we're going to also make Bunch of spaces so I can scroll down. You can see what's going on here I'm going to say avoid in niche allies You're going to have to initialize the open ssl library And of course if we're going to initialize we also need to finalize That's basically When you work with libraries a lot of times, um, they'll have you do what's called an initializer So you actually initialize the library and then you'll need to do a finalizer So you'll have to clean up or you know remove things from memory that sort of stuff And then I'm just going to have some helper functions in here. I don't know why I just did that. That was stupid I'm just wasting you time at this point. All right, there we go. So Cubite array read file Whoops And this is just going to very simply just read the file and then return a cubite array or say avoid We are having uh, what I call the battle of the air conditioner here in the household Um, she doesn't like the air conditioner and I love the air conditioner So one minute it's freezing cold the next minute. It's like boiling hot Cubite array. All right, so Going to go over a few little things real quick This is the basic structure of the program that we're going to be working with here So we got our public keys where we can load them our private keys where we can load them We use the public and private keys to encrypt and decrypt with rsa Um aes we're going to give it a pass phrase notice that's a pass phrase not a pass word Um, the reason is just terminology Then we are going to encrypt and decrypt with aes And we also have random bytes because we're going to randomly, you know, generate things for like the salt things of that nature And the rsa key or i'm sorry the aes key We're going to have to initialize the open ssl lib and we're going to have to clean up after it And we're just going to read file and write file Um, and then in the main which we haven't gotten to yet I'm going to actually add the public and private keys, but i'm not really going to waste you guys's time with that So i'm going to actually flesh out a lot of these functions And when I say flesh out i'm literally just going to go right click and then Add definition in and i'm going to do that for each one just to get these built up But i'm going to stop the video here. So you don't just watch me click for two minutes Um the next video We're going to actually start working with the internal code and you know loading the keys and working with rsa And if we have time going into aes and then probably the video after that We're going to pull everything together and actually write some test functions and test it versus the command line All right. So, uh, where am I at here? That's it for this. I'm going to try and keep that kind of you know short Um next video we're going to get into the meat potatoes of it Be sure to visit my website for source code for this and other tutorials Also go to the void rooms facebook group out on facebook There's over 600 some odd programmers out there all different sorts of languages. So Instead of emailing me your questions like if you say oh my god, brian show How do I do a static build just ask in the facebook group that everybody will start pitching in