Python - Coding a Password Manager

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hope you guys are all doing well today i'm going to walk you guys through how to make a password manager application in python so go ahead and open up your pycharm i go up here to file new projects uh you can name your project whatever i'm just gonna name it password manager i deselect this create my main dot pi welcome script go ahead and hit create open it up here in this window and then just give it a few moments for it to open up create the environment and such now that your project is loaded up under the vanv of your project go new python file i'll just name it whatever i'm going to name mine my main open it here on the side and it should pop up and we can get started with our password manager application so first things first what do we want the application to do so uh i would like this application to take in the software name the username and the password that an individual would like to save let's say for like github they can't remember their password so they type in when they first make it they type in github they type in their username type in their password the application here will do some encryption save it to a file and we'd be able to get that all later if we ever need or if we ever forget our password i guess we want to have two options we want the option to add a new password and we want the option to view all the passwords or maybe just view a password for software so maybe you type in github and then it will return the github password so let's start off here let's i guess make a menu for the user input so let's go enter the name of the software you are using you can do one for the username and one for the password perfect uh now let's throw this in a menu because i guess this will be the only only the option we want when you want to add a new password so let's just instantiate a blank menu here and then let's say wow menu since we only want two options all menu is not equal to one or menu is not equal to two then display the menu which will say would you like to save a new password or view your old ones perfect and we can say if menu is equal to one it will display these options and if the menu is equal to two we'll just say print display passwords because we don't know or we don't have any code for that yet uh and let's even make an exit function too so if the menu option is equal to three it will just exit and up here we can say exit nice let's go ahead and test run that so we can go ahead and run that and test it out so if we went and we hit one number one will pop up so software we are using let's just say some random stuff well it says hey username it can be the super student these don't really matter because they're not going anywhere but it works perfect it goes right back to the menu if we hit two it will just say display passwords yep and then oops if we hit three it'll just exit the application let's do the saving to a file so let's save all the data to the file and then i will let's get displaying that data so to open a file in python is relatively easy just type in any variable uh you could do name f or file name it open or equals open and then the name of the file so i will do secure password data dot txt followed by a comma and then the option you want to open that file in so we're going to open it in append mode you can open it in write create or read but for this application we're going to do append and then to write to the file just do file.write or whatever the name of your variable is dot write and then whatever you want to write to the file so we wanted to write the software name the username and the password and then of course close the file to stop any memory leakage and let's go ahead and try running that and see if it creates our file so let's input a new password let's just do test software user and pass oh and it gives us an error it says it takes exactly one argument that is because we did commas rather than plus which is what should have been done so let's try that again test user pass and it goes through and we see the txt is made up here now we can go ahead and open that up and there it is test user pass so our user username here password here and the software named test oops so we know that's working well so before we go on to reading in uh from this file uh let's make a few changes so we're going to want to separate the software name the username and the password from each other and then we're going to want to create a new line each time a line is entered so the next entry is always on a new line so back on this file.right line let's create a delimiter to put between our uh entries here so i'm gonna do a semicolon and a pipe the reason i'm doing two characters is because in a like if you enter a comma right within here and you just do a regular comma separated value uh if one of your software name username or passwords has a comma in it then your application it will just break because it will try and take the wrong element from your list so and then at the end here let's do a backslash n to create a new line at the end of that perfect and then within menu two to read in the files as of now we're going to want to open the file again so file equals open to that same file this time let's open it in read mode and then we're going to want to read each line in this file oh i forgot to close that we want to read each line in this file so let's say let's make a loop for each line so for i in file we can say like either the software name username or password is the line separated by though that semicolon and pipe combination so i will read in this line all right let's sorry let's delete this for now run it again input a new password just put in something silly super username super password and then let's exit there so we will have our new pieces of information here separated by those combos and there's a new line at the end now when we read it in we're going to want to say print data at position zero data position one data position two so this should print the software name username then the password let's test that out yep so there's saved super username super password and of course these have been removed thanks to the split function so there's an easy way to view it from the file now now to clean this up real quick let's just add in some other stuff up here let's make it a little table so i'll do software username password and then let's separate each one of these by a couple tabs perfect let's try that again so it nicely displays everything now let's get rid of this data here the sample data let's run the application again and nothing should appear this time perfect other than of course you have the software using the path we just put just go straight back to the menu so everything's working well as of now now to create a function within python and to create a function within python it's relatively simple i just let's start our function with def to define a function and then within our encryption function we're gonna want to add a input parameter so the input parameter is going to be either the password the username or the software because we're going to want to encrypt all that data to make sure no one knows anything about anything so let's take in the data and was also taking a key value or shift a shift and let's create our empty string here within that function so for this application i'm going to be using a caesar cipher encryption so to create an encryption algorithm i'm going to be doing a function within python so to create a function you just start with def then the name of the function so def encrypt and then i'm going to take in some input parameters some data and a key value or a shift value so we're going to loop through the data so we're going to want each character within let's say the password right in the caesars cipher the way it works is if the character is like a b the character would be shifted based on this value that many characters in the alphabet so if it was a b and the shift value was 2 it would go from a c to a d so the b would become a d right if the shift was 1 and the character was an a then the character would become a b if that makes sense so the way we're going to do that we're going to take the ring the length of the uh sorry the data and take the length of the data so for each care this pretty much says for each character in there we're going to say yeah this is a sign card to each position and if that character is an uppercase character we're going to perform some encryption so we need to make a string let's say encrypted encrypted string let's just make it blank up here and we're going to want to append this character to the encrypted string so this is the string we're going to return at the end of this function uh as the the actual string that's being encrypted so the way we're gonna do this is we're gonna transfer the character into the uh number value for that uh character so chr so occur ord that takes uh whatever is within here takes car and it takes that character's number value rather than its like letter value so every character has an assigned number pretty much so if you take that number and you add it by the shift value and for uppercase characters you need to subtract 65. if you take uh this character value and then subtract 65 that will get the correct value right and then you shifted that many characters up in the alphabet so uh back here whenever we uh whenever we're encrypting we need to make a shift value uh let's make it just two for now and then we can finish the statement off so we need to modulus by 26 so that is per each character in uh the sequence of characters so this is a to z in all capitals so there's 26 characters and we need to also test if the character is lowercase so we can say character dot is lower add in the same calculations plus shift for lowercase u minus 97 and then again there's 26 characters in the alphabet so you modulus by 26. and so for the caesar cipher you need to actually check if it is a number as well because if you just check if it's an uppercase or lowercase if it's a number it will just break so we need to check if it's number oops i think i've been doing this backwards too nope i'm good all right so we need to check if it is a digit so the way to do that we can create a quick number variable assign it to an int of car of the character that this is at and then we simply just shift it over whoops using the shift and then modulus is uh there's only zero to nine rather than a to z you do ten instead of twenty six let's get all that in there and then yeah you just simply add number to that encrypted string return encrypted and that should work the way that we want it oh that could have been it was resetting the string every time so we want to do it outside so we can go ahead and try this out and see if it encrypts our data so let's input a new password oh before we do this let's go ahead and stop this application we actually need to call our function so down here when we do our file right to the file we can actually now say encrypt and replace the data input parameter with uh the actual piece of data so for the first one is software name for the second one it will be of course username and then comma shift of course the shift value you can we can set this to be like three four five six two one seven nine like any number to 25 or 26 i think any number you want that's how many characters it will shift in the alphabet so let's now encrypt the password and that should be good let's go ahead and run that and put a new password the software let's do sate so we need to make sure we have a capital letter a lowercase and a number oh as well as a special character which will probably break this application as of right now because we did not define what to do for a special character so let's just leave that out for now so let's do pass one two three user and say okay we got problem one line eleven encrypted plus equals number you can only concatenate string so let's cast that over to a string and let's try that again let's do the same inputs make sure it didn't save yep and then pass one two three all right so it works so that was the problem let's go ahead everything has been encoded so we have our what is it our shift of five so s turns into x so s q q r s t u v w x that's five look at that that's crazy a to f a b c d e f that's crazy i the n j k l m n oh my goodness that's five absolutely crazy t to y that's crazy sate that's been encrypted so now say that you don't know what the heck any of this means you need to decrypt that data you need to be able to actually remember what that is because if we go ahead and view our passwords right now it's just going to give us that encrypted data so we're going to want to somehow decrypt this data first let's actually try and input a password with a character like that like a special character and see if it explodes no but it didn't actually save in any information so that's not what we want it doesn't so we're going to want in our encryption to have a final else so if it's not a number it's not a lowercase it's not uppercase we don't want it to just eat that away we want to actually just add it just add it like if it's a symbol it's a symbol you know we don't need to encrypt that right so let's just just add car so in this case if we input a new software let's say we're using uh linkedin or something and then username is whatever and then our password is like super awesome exclamation mark exclamation mark 2 3 right and we go and view what that says the exclamation marks are still in the password so that's exactly what we want perfect so now to decrypt the data so we don't actually just lose it or if we forget the key then we will or will still be able to access it let's make a decrypter so along the same lines of our encrypter let's actually just copy and paste that why not and then change every occurrence of encrypted to decrypted and then all we need to do is reverse our shift so instead of going up in alphabet it goes back in the alphabet so just replace those with a minus and in reality we should be good to go just need to add our decrypt function down here into our data into our read section so let's add that in getting the shift there let's also define shift here to make sure it's the same final one decrypt shift perfect so we should now last time we ran this we got xfny cxjw ufxx678 a bunch of random crap now if we run it with our decrypt theoretically it should appear with the original password and look at that there it is now this works very well as a little password manager application uh if anyone tries to steal your file that's all they get is just a bunch of gibberish unless they know that you're using a caesar cipher and that you're using the key of five there's no way that they can get this information uh just make sure that your that this application is safe really so like make sure someone can't walk on up to your computer for some reason open up your password manager run it hit view passwords and then and then sit at your screen and write them all down so just don't as long as you don't let someone do that this is a pretty great app uh very simple to code only 50 lines yeah that's about it for my application thank you very much for tuning in to this video thanks for reading the blog post i hope you guys stick around for my future projects and thanks for checking out this video take care see you
Info
Channel: jordan klassen
Views: 9,177
Rating: undefined out of 5
Keywords:
Id: 6DSHWjnicRA
Channel Id: undefined
Length: 30min 4sec (1804 seconds)
Published: Sun Apr 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.