Password Generator & Manager Using Python

Password Generator & Manager Using Python

Introduction

Hey everyone. It's been a long time since I started. I haven't posted anything but I hope to cover up for it next few days, anyways.

A few days ago I decided to test my current skills. I haven't worked on an independent project on my own in a long time and the only ones I did work on were related to school assignments and stuff. I got some ideas and decided to work on them one by one. One of them was a Password generator. Now, you would be thinking that's too basic so I tried to make it a little bit more challenging for me and interesting. My goal was to build something that would be actually used. Now, one big concern after creating passwords for different websites & apps is their management. They can easily get lost or mixed up(at least mine does). So, I decided to integrate MySQL with the program to easily manage the passwords as well.

So, I started working on it. My goal was to create a program that would generate a 12-character password based on the user's requirement, save it in the database(if the user wants to) and would also allow the user to directly extract a password for a website or app and delete any record/data.

I divided the whole process into three Parts:

  1. Password Generation

  2. Password Saving & Management

  3. Extraction of Passwords and Deletion

Part 1: Password Generation

The first task was password generation. A good password length is usually recommended to be between 10 to 14 characters. So, I choose the middle ground and decided to go with a 12-character length for the password. Now sometimes you cannot use special characters in a password. So, the program will ask the user whether special characters are allowed or not and then the rest of the password generation process will begin based on user input.

import random
passwd_genetion= input("Do you want to generate a new password?:")
while passwd_genetion.upper()=="YES":
    def passwd_restrictions(): # Checking if password can contain special characters
     a = input("Are special characters allowed(Enter yes or no):")
     if a.upper()=="YES":
        print("Special characters are allowed")
        return True
     else:
        return False

    restrictions = passwd_restrictions()    
# If True Generated Password will have special characters
# Else it will contain only alphanumeric characters

If the special characters were allowed, then the 12-character password will contain randomly generated 4 special characters, 2 uppercase and 2 lowercase alphabets and 4 numeric characters. If not allowed then the length will remain the same and will instead be replaced by 2 extra alphabets and numeric characters.

I tried to keep the password generation process in such a way that no two special characters or numeric characters can have double occurrences.

special_chr = ["!","@","#","$","%","^","&","*","?"]
passwd_spchr= []
rep_value = None
# The loop will go through a list of special characters and will select four(4) random special characters 
# The elements of the the new list will be used in the password generation and will be concatenated later on
if restrictions is True:    
   for sp_chr in range (0,4):
       a =  random.choice(special_chr)
       while a==rep_value:
           a = random.choice(special_chr)
       rep_value = a
       passwd_spchr.append(a)

Part 2: Password Saving & Management

So after the password generation was all set and done, it was time to move to the next part ie. Management and Saving of Passwords. If the user intends to save the password(s), he/she can easily save them in the MySQL database. I established the connection between Python and MySQL using the "MySQL Connector" driver for Python.

import datetime
import mysql.connector
passwd_save = input("Do you want to save this password?(Or any other Password):")
now=datetime.datetime.utcnow()
modify_notif=now.strftime("%Y-%m-%d %H:%M:%S") # Object/Variable to store the current date and time directly from system.
# Now if the user has agreed to save the password the next block of code will run to establish the connection with database and follow the next steps.
def sql_connect():
    global con1
    global my_cursor
    sql_passwd = input("Enter your MySQL Password:") 
    #Password for sql connection to be taken as input 
    con1= mysql.connector.connect(host="localhost",user="root",passwd=sql_passwd)
    if con1.is_connected():
        print("Connections with database established successfully")
    my_cursor = con1.cursor()

Then if the user agrees, a database called "password_storage" will be created in MySQL (if it does not exist). Along with it, three tables will be created in the database named:

  • Social: For saving passwords related to social media accounts.

  • Work: For saving passwords related to work and professional stuff such as Gmail accounts, Linkedin etc.

  • Bank: For saving passwords related to bank accounts.

# Now the database will be created for the password storage if it does not exists.
# Three different tables will be created for password management: Social Media accounts,Work accounts,Bank accounts
while passwd_save.upper()=="YES":
    sql_connect()
    my_cursor.execute("Create Database if not exists Password_Storage;")
    my_cursor.execute("use Password_Storage;")
    my_cursor.execute("create table if not exists Social(App_name varchar(40) NOT NULL, email_id varchar(50) NOT NULL, password varchar(20) NOT NULL, username varchar(25), Last_Modified datetime);")
    my_cursor.execute("create table if not exists Work(App_name varchar(40) NOT NULL, email_id varchar(50) NOT NULL, password varchar(20) NOT NULL, username varchar(25),  Last_Modified datetime);")
    my_cursor.execute("create table if not exists BANK(Bank_name  varchar(40) NOT NULL, Account_No  int(25) UNIQUE NOT NULL, Account_holder char(40), password varchar(20) NOT NULL,  Last_Modified datetime);")
    table_use=int(input("Which type of password do you want to save:Social,Work or bank?(Enter 1 for Social,2 for work and other for bank):"))

Part 3: Extraction of Password and Deletion

Now only the final part was remaining. The saved passwords had to be made accessible to users for extraction or deletion. The saved passwords would be extracted based on the App name and email id for work and social media accounts and the Bank ones through the Bank name and Account number.

For Ex:

passwd_extract = input("Do you want to an extract a Password?:")
# Now if the user wants to extract a password he can extract it.
while passwd_extract.upper()=="YES":
    sql_connect()
    my_cursor.execute("use Password_Storage;")
 # Input is taken from the user to select the table from which he wants to extract the password from.
    table_name = input("Enter the password type you want to extract (Or the table name):")
    if table_name =="social":
  # The Password is extracted based on the app name and email id.
      app_name= input("Enter the App name:")
      email_id = input("Enter the email id:")
      query = "Select password from social WHERE App_name = %s AND email_id = %s" # Parametrized Query
      tup1= (app_name,email_id) 
      my_cursor.execute(query,tup1) # The values from tup1 are used as the values for the parameters set in the query.
      records = my_cursor.fetchall()
      print("Extracting Password ")
      for row in records:                   
         print("The Password is:",row[0])

Conclusion

I loved doing this project. It made me feel a lot more confident in my skills. However, this is not the end of it as there are some upgrades that I want to add to this project.

You can look up the full code here: https://github.com/KaustubhUpadhy/Password-Generator-and-Manager

I won't immediately start updating this project as there are some other cool project ideas as well on which I want to work. However, I will make sure to follow up with any updates I will be making on this project in the future.

I would love to hear any feedback or suggestions from you. I would highly appreciate it.

Project link: https://github.com/KaustubhUpadhy/Password-Generator-and-Manager