How secure is your password?
A computer could try to guess your password by using 'brute force' - this means trying out lots of passwords until it guesses the right one.
Let's explore how to create secure passwords using Python, and learn about password security along the way!
Random Characters
Let's create a program to choose random characters for your password.
#!/bin/python3
import random
chars = 'abcdefghijklmnopqrstuvwxyz'
password = random.choice(chars)
print(password)
This code will generate a single random lowercase letter.
Adding Numbers and Punctuation
Let's make our passwords stronger by adding more character types:
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?.-'
Creating a Random Password
Now let's generate a longer password by combining multiple random characters:
password = ''
for c in range(10):
password += random.choice(chars)
print(password)
Example output: Kb7?mN9.pQ
Choosing a Password Length
Let's allow users to specify their desired password length:
length = input('password length? ')
length = int(length)
password = ''
for c in range(length):
password += random.choice(chars)
print(password)
password length? 15
Kj9?mN.pQ2xY5vZ
Kj9?mN.pQ2xY5vZ
Generating Multiple Passwords
Generate multiple passwords at once:
length = int(input('password length? '))
num_passwords = int(input('number of passwords? '))
for p in range(num_passwords):
password = ''
for c in range(length):
password += random.choice(chars)
print(password)
password length? 10
number of passwords? 3
Kj9?mN.pQ2
xY5vZ8!aB4
Qw3$kL9nM7
number of passwords? 3
Kj9?mN.pQ2
xY5vZ8!aB4
Qw3$kL9nM7