top of page

RANDOM PASSWORD GENERATOR

Writer's picture: madrasresearchorgmadrasresearchorg

AUTHOR: JENEEFA

With growing technology everything has been relied on data and the main concern is to secure the data by creating strong password. Here we will create a simple application to generate unique password and also graphical user interface using Tkinter module to generate the same.
 

Passwords are being a real security threat. A strong password is a combination of all lowercase and uppercase alphabets, digits, and symbols. The main objective of this article is to provide a complete guide to create an unpredictable password using python to keep private information safe and to prevent passwords from being hacked. Let’s get started to generate a random password.



FIG:1

RANDOM PASSWORD GENERATION USING PYTHON:

PRE-REQUISITES: To implement this project we will need basic knowledge about the libraries in Python and an IDE (Integrated Development Environment) to generate.

STEPS TO IMPLEMENT:

STEP 1: Import Libraries:

To generate a simple password we will import a module called random (to select random elements) and string libraries (consists of several constants and classes).

import random  #import libraries
import string

STEP 2: Access Constants:

import string                                                                  
lower_alpha=string.ascii_lowercase                                      #access constants  
print(lower_alpha)
upper_alpha=string.ascii_uppercase
print(upper_alpha)
num_ber=string.digits
print(num_ber)
sym_bols=string.punctuation
print(sym_bols)

From the above code we imported string library constants and we used variables like lower_alpha, upper_alpha, num_bers and sym_bols to print the below output.

FIG:2

We will use this set of characters to generate password.Clear the print statement and proceed to the next step.


STEP 3: Get Password Length:

lower_alpha = string.ascii_lowercase  # access the constants  in string modules 
upper_alpha = string.ascii_uppercase
num_ber = string.digits
sym_bols = string.punctuation
pass_len = int(input("Enter password length\n")) # get the length of the password

Here python built in input ( ) is used to retrieve the user input and return the value in string. To convert string to numerical values python built in int ( ) is used.


STEP 4: Generate Password:

 pass_gen=[] #create an empty string to hold password
 pass_gen.extend(list(lower_alpha)) # concatenate
 pass_gen.extend(list(upper_alpha))
       pass_gen.extend(list(num_ber))
       pass_gen.extend(list(sym_bols))

To add multiple elements we will use a method called .extend ( ). This method returns only in the list so we will convert the string to list by using python built in list ( ). Thus all the elements are concatenated in the empty string.


STEP 5: Print Password:

print("Random password is: ")
print("".join(random.sample(pass_gen,pass_len)))       #print password

Here we will print the password by using a function in a random library called .sample ( ) which will generate a random elements (pass_gen) and iterate up to the length of the password (pass_len), and then the function .join ( ) is used to concatenate the returned value in a single string and print the password. Let’s run and execute the code.


Observe the output:

FIG:3

NOTE: The output will be different from this as the password is randomly generated!!

The output is pretty good. Let’s create a GUI (Graphical user interface) to create a random password generator application. For this application we will use a module in python called Tkinter. Let’s learn detail about this!!!

RANDOM PASSWORD GENERATOR USING TKINTER:

WHAT IS TKINTER?

Tkinter is an open source GUI library in python which allows the user to communicate with an electronic device. It is a form of user interface to create graphical elements such as windows, icons, buttons etc. Tkinter GUI library consists of widget which refers to individual GUI elements. Here we will use widgets like,

  • Frame: Frame provides a structure to the Tkinter window with a fixed size.

  • Label: This widget is used to display the text inside the frame in GUI.

  • Entry: An entry box is created to get input from the user through user interface.

  • Buttons: This widget used as a way for the user to interact with the user interface. When the button is clicked it triggers an action to be done.

STEPS TO IMPLEMENT:

The main objective is to generate the password in GUI using Tkinter and also copying the generated password in clipboard.

STEP 1: Import Libraries:

from tkinter import * # import libraries
from random import *

STEP 2: Initialize Window:

Root = Tk()   #initialize window
root.title('Generate random password')
root.geometry("500x600")

In the above code a window is created using Tk ( ), and it is titled as ‘Generate random password’ and the height and width is fixed using .geometry ( ).


STEP 3:Using Widgets:

label= LabelFrame(root, text="Enter number of Characters")#create label frame
label.pack(pady=20)

my_entry = Entry(label, font=("Arial"))                                  #create input entry box
my_entry.insert(0,'Password length')                                       #optional
my_entry.pack(padx=20, pady=20)

gen_entry= Entry(root,font=("Arial"))  #create output entry box
gen_entry.pack(pady=20)

  • Label frame is created with text and padding, and to organize the widget in block .pack () is used.

  • Two entry boxes (my_entry, gen_entry) are created for user input and the output to be generated.

frame=Frame(root, bg="black", bd=2)                                   # create frame for buttons
frame.pack(pady=10)

button=Button(frame,text="Generate Password",command=gen_pass)
button.grid(row=0,column=0,padx=20)                                 # create generate button

copy_button=Button(frame,text="Copy to Clipboard",command=copy_pass)
copy_button.grid(row=0, column=1,padx=20) #create copy button

  • Two buttons are created to generate and copy the password.

  • .grid ( ) is to align the button next to each other.

  • When the button is clicked .command ( ) triggers the action and generate the required output.

STEP 4: Function to generate password

def gen_pass():  #function to generate password
gen_entry.delete(0,END)     #clear the entry box
pass_len=int(my_entry.get()) #get password length
password=[]    #initialize empty string
for i in range(pass_len):  #loop till the length
password += chr(randint(33,126))
gen_entry.insert(0,password)    #return the random password

Here ASCII characters are used to generate password. randint () selects the random integer from the ASCII value and python built in chr ( ) convert numbers to character.

FIG:2

STEP 6: Copy the Password:

def copy_pass():  #function to copy
root.clipboard_clear() #clear the clipboard
root.clipboard_append(gen_entry.get())#copy

The Clipboard_append ( ) get the generated password and copy to the clip board.

The output looks like:

FIG:3

Hope this article is helpful to generate random password. There are many ways to do this. We used popular Tkinter library which render graphics in our output window. In this sample solution we have made the point to make it simple, elegant and clear. Hope you enjoyed it. HAPPY LEARNING!!!!


GitHub Code:

  • https://github.com/JENEEFATHOMAS/Generate-Random-Password.git


REFERENCES:

  • https://docs.python.org/3/library/index.html

  • https://docs.python.org/3/library/tkinter.html#module-tkinter

  • https://coderslegacy.com/python/python-gui/

  • https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html




Recent Posts

See All

Comments


Madras Scientific  Research Foundation

About US

 

Contact

 

Blog

 

Internship

 

Join us 

Know How In Action 

bottom of page