-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordGenerator.java
More file actions
69 lines (55 loc) · 2.72 KB
/
Copy pathPasswordGenerator.java
File metadata and controls
69 lines (55 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.security.SecureRandom;
import java.util.Scanner;
public class PasswordGenerator {
// Character sets for password generation
private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
private static final String DIGITS = "0123456789";
private static final String SPECIAL_CHARACTERS = "!@#$%^&*()-_=+[]{}|;:'\",.<>?/";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Taking input for password length and character type preferences
System.out.print("Enter the length of the password: ");
int length = scanner.nextInt();
System.out.print("Include uppercase letters? (y/n): ");
boolean includeUppercase = scanner.next().toLowerCase().charAt(0) == 'y';
System.out.print("Include lowercase letters? (y/n): ");
boolean includeLowercase = scanner.next().toLowerCase().charAt(0) == 'y';
System.out.print("Include digits? (y/n): ");
boolean includeDigits = scanner.next().toLowerCase().charAt(0) == 'y';
System.out.print("Include special characters? (y/n): ");
boolean includeSpecialCharacters = scanner.next().toLowerCase().charAt(0) == 'y';
// Generating the password using the selected options
String password = generatePassword(length, includeUppercase, includeLowercase, includeDigits, includeSpecialCharacters);
System.out.println("Generated password: " + password);
}
// Method to generate password
private static String generatePassword(int length, boolean includeUppercase, boolean includeLowercase, boolean includeDigits, boolean includeSpecialCharacters) {
String characterPool = "";
if (includeUppercase) {
characterPool += UPPERCASE;
}
if (includeLowercase) {
characterPool += LOWERCASE;
}
if (includeDigits) {
characterPool += DIGITS;
}
if (includeSpecialCharacters) {
characterPool += SPECIAL_CHARACTERS;
}
// If no option selected, throw error
if (characterPool.isEmpty()) {
throw new IllegalArgumentException("At least one character set must be selected.");
}
// Using SecureRandom for strong randomness
SecureRandom random = new SecureRandom();
StringBuilder password = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = random.nextInt(characterPool.length());// Get a random index
password.append(characterPool.charAt(index)); // Add random character from pool
}
// Final password
return password.toString();
}
}