0% found this document useful (0 votes)
5 views

Password Generator

Password generator

Uploaded by

tewarishrestha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Password Generator

Password generator

Uploaded by

tewarishrestha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Random Password Generator

CSN203
Introduction to Java Programming

Submitted By
Name : Divyanshu Pranabh Singh
Roll no : 2301021190 230105092
SAP I’d : 1000021780 1000021839
Section : M M

Submitted to
Mr. Ravi Shankar Jha, Assistant Professor,
School of Computing
Introduction

Random Password Generator is an application use to generates a password randomly this


password will be having the limited digits based on the user requirement. This the following
program is creates using the Java coding language.
Explanation:
1. User Input:
- The program asks the user for the desired password length.
- If the length is less than 4, the program informs the user that the password
length should be at least 4 to include one symbol, one number, one uppercase, and
one lowercase letter.

2. Random Password Generation:


- The program uses Random to generate random characters.
- It ensures the inclusion of at least one uppercase letter, one lowercase letter,
one digit, and one symbol by adding one character from each set initially.
- The remaining characters are filled by randomly picking from a combined set
of all characters.

3. StringBuilder:
- StringBuilder is used to construct the password dynamically.
- It allows efficient appending of characters without creating multiple string
objects.

4. Shuffling:
- The shuffleString method shuffles the characters of the password to ensure
randomness.
- This step uses ArrayList and Collections.shuffle to randomize the order of
characters.

The password will be generated by randomly selected the Symbols, Characters and Numbers
that are pre provided to the program.
Code and Output Screenshots

Java Code:

import java.util.*;

public class PasswordGenerator {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter the desired password length: ");


int length = scanner.nextInt();

// Ensuring the minimum length to satisfy the condition


if (length < 4) {
System.out.println("Password length should be at least 4 to include one symbol, one
number, one uppercase, and one lowercase letter.");
return;
}

String password = generatePassword(length);


System.out.println("Generated Password: " + password);
}

private static String generatePassword(int length) {


String upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowerCase = "abcdefghijklmnopqrstuvwxyz";
String numbers = "0123456789";
String symbols = "!@#$%^&*()-_=+<>?";
Random random = new Random();
StringBuilder password = new StringBuilder();

password.append(upperCase.charAt(random.nextInt(upperCase.length())));
password.append(lowerCase.charAt(random.nextInt(lowerCase.length())));
password.append(numbers.charAt(random.nextInt(numbers.length())));
password.append(symbols.charAt(random.nextInt(symbols.length())));

String allCharacters = upperCase + lowerCase + numbers + symbols;


for (int i = 4; i < length; i++) {
password.append(allCharacters.charAt(random.nextInt(allCharacters.length())));
}

return shuffleString(password.toString());
}

private static String shuffleString(String input) {


List<Character> characters = new ArrayList<>();
for (char c : input.toCharArray()) {
characters.add(c);
}
Collections.shuffle(characters);
StringBuilder output = new StringBuilder();
for (char c : characters) {
output.append(c);
}
return output.toString();
}
}
Output:
Conclusion

This Java program effectively demonstrates how to generate a secure random password while
ensuring the inclusion of essential character types: at least one uppercase letter, one lowercase
letter, one digit, and one symbol. By leveraging the StringBuilder class for efficient string
manipulation, the Random class for randomness, and strategic character manipulation, the
program meets security criteria and user requirements.

Key Concepts:

- User Input Handling: Ensures the password length meets minimum security requirements.

- Randomness: Uses the Random class to generate a mix of characters.

- StringBuilder: Efficiently constructs the password without creating multiple string objects.

- Character Inclusion: Guarantees the presence of required character types.

- Shuffling: Ensures the randomness of character order for added security.

The program showcases the integration of multiple fundamental Java concepts and illustrates
how to build a robust utility for generating secure passwords. This approach can be applied to
other scenarios requiring secure random string generation.

You might also like