w3resource

Python: Cyber Security - Exercises, Practice, Solution

Python: Password cracking, hashing, policy enforcement and recovery [10 exercises with solution]


[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

1. Write a Python program that defines a function and takes a password string as input and returns its SHA-256 hashed representation as a hexadecimal string.
Click me to see the sample solution

2. Write a Python program that defines a function to generate random passwords of a specified length. The function takes an optional parameter length, which is set to 8 by default. If no length is specified by the user, the password will have 8 characters.
Click me to see the sample solution

3. Write a Python program to check if a password meets the following criteria:
At least 8 characters long and
Contains at least one uppercase letter, one lowercase letter, one digit, and one special character (!, @, #, $, %, or &)
If the password meets the criteria, print a message that says "Valid Password." If it doesn't meet the criteria, print a message that says "Password does not meet requirements."
Click me to see the sample solution

4. Write a Python function that takes a password as input and returns a list of common character substitutions that could be used to create a stronger password.
Click me to see the sample solution

5. Write a Python program that reads a file containing a list of passwords, one per line. It checks each password to see if it meets certain requirements (e.g. at least 8 characters, contains both uppercase and lowercase letters, and at least one number and one special character). Passwords that satisfy the requirements should be printed by the program.
Click me to see the sample solution

6. Write a Python program that reads a file containing a list of usernames and passwords, one pair per line (separatized by a comma). It checks each password to see if it has been leaked in a data breach. You can use the "Have I Been Pwned" API (https://haveibeenpwned.com/API/v3) to check if a password has been leaked.
Click me to see the sample solution

7. Write a Python program that creates a password strength meter. The program should prompt the user to enter a password and check its strength based on criteria such as length, complexity, and randomness. Afterwards, the program should provide suggestions for improving the password's strength.
Click me to see the sample solution

8. Write a Python program that generates a password using a random combination of words from a dictionary file.
Click me to see the sample solution

9. Write a Python program that simulates a dictionary attack on a password by trying out a list of commonly used passwords and their variations.
Click me to see the sample solution

10. Write a Python program that simulates a brute-force attack on a password by trying out all possible character combinations.
Click me to see the sample solution


Python Code Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.