Python

Python password generator

Password is used in any application for authentication. Creating a strong password is very important to keep the user’s account secure. Any account can be hacked easily if a very simple password is used for the account, such as 12345 or the user’s name. A strong password can be created by combining an uppercase letter, lowercase letter, digits, and special symbols. A password generator is a program that is used to generate random passwords. The Password generated by this application is very strong, and it can’t be guessed easily by the hacker. It is better to use the Password generated from the password generator for any normal or administrative account to keep the account safe. The password generator program can be implemented in different ways using the python script shown in this tutorial.

Install the necessary module:

The pyperclip module is used in this tutorial to copy the randomly generated Password to use it somewhere. Run the following command to install pyperclip.

$ pip install pyperclip

Example-1: Implement a simple password generator

The simple way to create a password generator has shown in the following script. A large text of mixed characters has been declared to generate the Password by selecting the particular length characters. The length of the Password will be taken from the user. sample() function of the random module has been used to select the character from the text and join together randomly. The randomly generated password will be printed later.

# Import random module
import random

# Set the character list for generating the password
characters = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ&*(){}[]|/\?[email protected]#$%^abcdefghijklmnopqrstuvwxyz"
# Take the length of the password from the user
password_length = int(input('Enter the length of the password: '))
# Generate the password
password = "".join(random.sample(characters, password_length))
# Print the generated password
print("Gernerated password: %s" %password)

Output:

12 has given as the value of the Password length, and a password of 12 characters has been generated after executing the script.

Example-2: Implement Password Generator with GUI

GUI-based password generator application is helpful for the user to generate the random Password. Tkinter module has been imported into the script to create the user-friendly password generator application. Random and string modules have been imported to generate the random Password by mixing the uppercase, lowercase, digit, and special characters. The pyperclip module is used to copy the generated Password for creating different types of user accounts. Password_Generator() function has defined to generate the Password and store it in the password variable. CopyPassword() function has defined to copy the generated Password after generating the Password. A Tkinter object has been declared to define the application window where the required widgets for designing the password generator application will be added. The first label widget has defined setting the label for the spin box widget to enter the Password length. The second label widget has defined the text box widget label where the generated Password will be displayed. Two-button widgets have been defined to generate the Password and copy the Password. When the ‘Generate Password’ button is pressed, the Password_Generator() function will be called, and the randomly generated password will be displayed in the text box. After generating the Password, if the ‘Copy’ button will be pressed, then the CopyPassword() function will be called to copy the Password, and the text ‘Password copied‘ will be shown above the buttons.

# Import tkinter module
import tkinter
# Import random and string modules
import random, string
# Import pyperclip module
import pyperclip

# Define function to generate random password
def Password_Generator():
    password = ''
    for n in range(lenPassword.get()):
        password = password+random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits + string.punctuation)
    strPassword.set(password)

# Define function to copy the generated password
def CopyPassword():
    if strPassword.get() != '':
        pyperclip.copy(strPassword.get())
        lblmsg2 = tkinter.Label(win, text='Password copied', font='Ubuntu 12 bold',width=15)
        lblmsg2.place(x=105, y=120)
    else:
        lblmsg2 = tkinter.Label(win, text='Nothing to copy', font='Ubuntu 12 bold', width=15)
        lblmsg2.place(x=105, y=120)

# Define object to display the main window of the application
win = tkinter.Tk()
# Set the title of the main window
win.title("Random Password Generator")
# Set the height and width of the main window
win.geometry("350x220")
# Set the position of the window
win.eval('tk::PlaceWindow . center')

# Set label for the password length
lblPassword = tkinter.Label(win, text='Set Password Length', font='Ubuntu 15 bold')
lblPassword.pack()
# Set the data type of the password length
lenPassword = tkinter.IntVar()
# Set the limit for the length value
length = tkinter.Spinbox(win, from_=6, to_=16, textvariable=lenPassword, width=2)
length.pack(pady=10)

# Set the label for the password field
lblmsg1 = tkinter.Label(win, text='Generated Password', font='Ubuntu 12')
lblmsg1.pack()

# Set the data type of the password field
strPassword = tkinter.StringVar()
# Define the variable for the password field
textData=tkinter.Entry(win, textvariable=strPassword, width=15)
textData.pack()

# Define button to call the function to generate the random password
btnPassword=tkinter.Button(win, text="Generate Password", command=Password_Generator)
btnPassword.pack(padx=50,pady=5,side=tkinter.LEFT)

# Define button to call the function to copy the password
btnCopy=tkinter.Button(win, text='Copy', command = CopyPassword)
btnCopy.pack(side=tkinter.LEFT)

# Call the mainloop of Tkinter to open the main window
win.mainloop()

Output:

The following window will appear after executing the script. 6 has been set as the default length value of the Password. The user can increase or decrease the value before generating the Password.

The Password of 8 characters has been generated after setting the length value to 8 and pressing the Generate Password button.

Password copied message has appeared after pressing the Copy button. Now, this Password can be used in other applications to create a user account.

Conclusion:

Two different ways of creating random Passwords have been explained in this tutorial. The first example can be used if you want to create a text-based password generator, and the second example can be used if you want to create GUI based password generator.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.