Python
Python
Module Name:
ST4061CEM Programming and Algorithms
2 | 14805851
Abstract
Password security is paramount in safeguarding online accounts against unauthorized access,
necessitating the use of strong and randomized passwords. Python-based password generators
offer a versatile solution by leveraging the language's built-in modules and functionalities. This
paper explores the fundamentals of password generation using Python, highlighting key concepts
such as string manipulation, randomization techniques, and user input validation. The script
outlined demonstrates how to create robust passwords within user-defined parameters, ensuring
both security and usability. Despite challenges such as entropy and usability concerns, future
enhancements hold promise for improving password generation techniques. By addressing these
challenges, Python-based password generators can continue to evolve, offering enhanced
effectiveness and contributing to a safer online environment.
3 | 14805851
Contents
Acknowledgement............................................................................................................................2
Abstract.............................................................................................................................................3
Introduction......................................................................................................................................5
What is a Password Generator?........................................................................................................5
Literature review..............................................................................................................................6
Key Concepts....................................................................................................................................6
Code Structure..................................................................................................................................7
Code explanation.............................................................................................................................8
Algorithm.........................................................................................................................................9
Challenges and Future Directions:.................................................................................................10
Future Enhancement.......................................................................................................................10
Conclusion......................................................................................................................................11
Reference........................................................................................................................................11
4 | 14805851
Introduction
This Python script is designed to generate a random password according to user-defined
specifications. It begins by asking the user to input their desired password length, ensuring it falls
within the range of 4 to 20 characters, thereby accommodating both security and usability
considerations. Once the user provides the desired length, the script proceeds to create a password
by combining various character types. These character types include lowercase letters, uppercase
letters, digits, and special characters, ensuring that the generated password is diverse and robust.
To achieve this, the script leverages Python's built-in string module, which provides convenient
access to predefined constants representing different character sets. By combining these character
sets, the script ensures that the generated password contains a mix of characters, making it
resistant to common password cracking techniques.
Furthermore, the script utilizes Python's random module to shuffle the characters, enhancing the
randomness of the generated password. This randomization process adds an extra layer of
security by making it more difficult for potential attackers to predict the password.
The user is then presented with the generated password, allowing them to use it for their desired
purposes, whether it be for creating a new online account or updating an existing password.
Overall, this Python script provides a simple yet effective solution for generating strong and
random passwords. By allowing users to specify the desired length and incorporating a mix of
character types, it ensures that the generated passwords are both secure and user-friendly.
Additionally, the script's reliance on Python's built-in modules makes it easy to understand and
implement, even for those with limited programming experience.
5 | 14805851
Literature review
Password generators implemented in Python offer a versatile solution for creating robust
passwords tailored to individual preferences. Leveraging Python's flexibility and extensive
libraries, these generators combine various character sets to produce randomized passwords. Key
concepts in password generation using Python include:
1. String Module: The string module in Python provides a collection of string constants
representing different character sets, such as lowercase and uppercase letters, digits, and
special characters.
2. Random Module: Python's random module enables the generation of random numbers
and the shuffling of sequences, facilitating the creation of unpredictable passwords.
3. User Input: Password generators typically prompt users to input their desired password
length and may offer additional options such as including or excluding certain character
types.
4. Password Generation Logic: The generation process involves combining selected
character sets, shuffling them randomly, and extracting characters based on the specified
length to create unique passwords.
Key Concepts
1. String Module: The script imports the string module to access predefined constants
like ascii_lowercase, ascii_uppercase, digits, and punctuation.
2. Random Module: The random module is used to shuffle the characters to create a
random password.
3. User Input: The script prompts the user to enter the desired password length and validates
the input to ensure it falls within the specified range.
4. Password Generation: It combines all character sets, shuffles them, and selects
characters based on the user-defined length to create a unique password.
6 | 14805851
Code Structure
1. Import necessary modules: string and random.
2. Define character sets for lowercase letters, uppercase letters, digits, and special characters.
3. Prompt the user to enter the password length within the range of 4 to 20 characters.
4. Validate the user input to ensure it is a valid integer within the specified range.
5. Combine all character sets, shuffle them randomly.
6. Generate a password by selecting characters based on the user-defined length.
7. Display the generated password to the user.
7 | 14805851
Code explanation
Imports the string module, which contains several constant strings of ASCII characters, such as
lowercase and uppercase letters, digits, and punctuation.
Imports the random module, which provides functions for generating random numbers and
shuffling sequences.
Assigns the constant strings containing lowercase letters, uppercase letters, digits, and
punctuation marks from the string module to variables s1, s2, s3, and s4, respectively.
Starts an infinite loop to handle user input for the password length.
Prompts the user to enter the desired length for the password as an integer.
Checks if the entered password length is within the specified range (4 to 20 characters). If not, it
prompts the user to enter a valid length. If the length is valid, it breaks out of the loop.
8 | 14805851
Handles the exception if the user enters a value that cannot be converted to an integer.
Concatenates all the strings of lowercase letters, uppercase letters, digits, and
punctuation marks and converts the result to a list.
Prints a message indicating that the generated password is being displayed. Converts the first
plen elements of the shuffled list s back into a string and prints it. This string represents the
generated password.
Algorithm
9 | 14805851
10. Print the first `plen` characters from the shuffled list `s` to generate the password.
11. End of the algorithm.
Future Enhancement
Future enhancements in password generation could include:
1. Advanced Entropy Sources: Exploring advanced entropy sources, such as hardware-
based random number generators or machine learning algorithms, to improve the
randomness of generated passwords.
2. Personalized Password Generation: Incorporating user behavior analysis and
preferences to generate personalized passwords that are both secure and memorable for
individual users.
3. Integration with Password Managers: Integrating password generators with password
management ecosystems to provide seamless password generation and management
capabilities within a single platform.
4. Enhanced Usability Features: Adding features such as password strength meters,
password policies, and support for biometric authentication to enhance usability and
security for users.
10 | 14805851
5. Cross-Platform Compatibility: Ensuring compatibility with various operating systems,
browsers, and devices to provide a consistent user experience across different platforms.
Conclusion
In conclusion, Python-based password generators provide a robust means for generating strong
and personalized passwords. Leveraging Python's capabilities and vast libraries, these tools
empower users to create complex passwords that meet their individual needs. Despite facing
challenges like entropy, biases, and usability issues, ongoing research and development endeavors
hold promise for refining password generators, thereby bolstering online security globally. By
addressing these challenges, future iterations of password generators can continue to evolve,
offering enhanced effectiveness and usability, and ultimately contributing to a safer online
environment for users worldwide.
GitHub link
https://github.com/R00052931D/password-generator
11 | 14805851
Reference
Shreya. (2023, July 15). Create random password generator program using Python.
Codingal. https://www.codingal.com/coding-for-kids/blog/create-random-
password-generator-program-using-python/
Follow, C. (2022, June 1). Create a Random Password Generator using Python.
GeeksforGeeks. https://www.geeksforgeeks.org/create-a-random-password-
generator-using-python/
How to create a random password generator using Python. (2022, September 3).
Hackernoon.com. https://hackernoon.com/how-to-create-a-random-password-
generator-using-python
12 | 14805851