Generate Strong Random Password using Python

The best way to learn a language is to practically use it. TechVidvan is back with another beginners project where we will create a Password Generator using Python. So let’s get started with the project.

About Password Generator

Nowadays security is the most important thing while working on a system. To keep a system secure on user-level one should use highly protective passwords. By protective passwords, it means that one should not use any predictive things like your date of birth, name, surname, etc.

Then the question arises what should we use? We should use a random password generating application so that no one could guess and misuse the password we have set. Now let us create a password generating application using python.

Python Password Generator Project Details

While creating the Password Generator, we will be using the Tkinter Module to create an easy GUI window and adding necessary fields to it. For generating the password we will be using the random module so that a random password is generated by choosing words from a string that we will specify. We are generating a password randomly so that it is secure to use.

Prerequisites for Password Generator using Python

To proceed with the project, you need the following :

  • Understanding of the basic concept of python.
  • Install Tkinter Module – For creating the GUI in python.
    Pip install tk
  • Install Random Module – For generating Random Number
    Pip install random

Download Python Password Generator Project

Please download the source code of python password generator project: Python Password Generator Project Code

Steps to Create a Password Generator using Python

1. Importing Required Libraries:

#importing required libraries for TechVidvan Password Generator project using Python
from msilib.schema import CheckBox
import random
from tkinter import *
import string
import tkinter
  • Checkbox – This is for creating a checkbox.
  • Random – This is for generating random things using random() method.
  • Tkinter – This is used to create a GUI using python.
  • String – This helps in accessing string literal.

2. Creating GUI Window:

#creating a window
window = Tk()
window.title('TechVidvan')#window title
window.geometry('500x500')#window geometry

Label(window,font=('bold',10),text='PASSWORD GENERATOR').pack()#giving label to window
  • Here we use the Tk() method to create a window named window. We use the title() method to give title to this window and geometry() to give the window a specific size.
  • Label() method is used to create a label that we are going to place on the window using the pack() method. We can specify the font, text, background color, etc of the label.

3. Creating Password_Generator() Function:

#function to generate password
def password_generate(leng):
valid_char='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@_'#characters of the password
     password=''.join(random.sample(valid_char,leng))#random generation of password
     l =Label(window, text = password, font=('bold', 20)) #displaying password
     l.place(x=190,y=50)
  • This function is created to generate a random string which will be our password. Every time we click on the button a new password will be generated. Here we have passed an argument leng which denotes the length.
  • Here we have created a string valid_char which contains all the possible characters that will make a password using the random() method.
  • random.sample() – This method is used to generate a random string using a sample. Here a sample is the valid_char string.
    Then create a label for displaying the password we have generated using this random.sample() method.

4. Creating CheckBoxes:

Checkbutton(text='4 character',onvalue=4, offvalue=0,variable=len1).place(x=200,y=150)#creating checkbox
Checkbutton(text='6 character',onvalue=6, offvalue=0,variable=len2).place(x=200,y=170)#creating checkbox
Checkbutton(text='8 character',onvalue=8, offvalue=0,variable=len3).place(x=200,y=190)#creating checkbox
  • Checkbox() method is used to create checkboxes in our window. We have created three checkboxes (4 characters, 6 characters, and 8 characters). We have assigned a variable name and a value to each of these checkboxes so that when a checkbox is selected it performs a desired task. The main concern to create a checkbox is to make sure what length of the password the user wants.
  • Here we have specified text, on value, offvalue and variable(variable name) for each of these checkboxes. Place() method is used to place these on the window using the x and y coordination system.
#converting string input to integer
len1=tkinter.IntVar()
len2=tkinter.IntVar()
len3=tkinter.IntVar()
  • As we want to use the values on the checkbox as integers, we will convert each checkbox variable value to integer using the IntVar() method.

5. Function to get the length of Password:

#function to check the checkbox
def get_len():
  if len1.get() == 4:
     password_generate(4)
  elif len2.get() == 6:
     password_generate(6)
  elif len3.get() == 8:
     password_generate(8)
  else:
     password_generate(6)
  • This function defines what length will be passed on to the password_generate() function.
  • We have used if else statements to specify what should happen. Using if else we check which checkbox is selected and according to the selected checkbox we perform password_generate() function.
  • If there is no checkbox selected then generate a 6 character password.

6. Creating Button and Main Command to Run:

Button(window,text='Generate',font=('normal',10), bg='yellow',command=get_len).place(x=200,y=100)
  • Using the Button() method we create a button named ‘Generate’. When we click on this button the get_len() function will get executed. We have specified the background colour as yellow for the button and we place it using the place() method specifying it’s x and y coordinates.
window.mainloop()#run the window

Finally we execute the mainloop() method that runs our window and displays what we have created.
Hurray! We have successfully created a Password Generator using Python. Now let us have a look at how our GUI window will look.

Python Password Generator Output

This is how a password will be generated based on the user choice of it’s length.

python password generator output

Summary

We successfully completed the creation of a Password Generator using Python. During the creation of this project, we learned the following-

  • How to use a Random Module?
  • Basic concepts of Tkinter and creating GUI using Python
  • About checkboxes