-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom_password_Generator.java
More file actions
60 lines (45 loc) · 2.27 KB
/
Random_password_Generator.java
File metadata and controls
60 lines (45 loc) · 2.27 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
import java.security.SecureRandom;
import java.util.Scanner;
public class Random_password_Generator {
private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String NUMBERS = "0123456789";
private static final String SPECIAL_CHARACTERS = "!@#$%^&*()-_+=<>?/";
public static String generatePassword(int length, boolean useLower, boolean useUpper, boolean useNumbers, boolean useSpecial) {
String characterPool = "";
if (useLower) characterPool += LOWERCASE;
if (useUpper) characterPool += UPPERCASE;
if (useNumbers) characterPool += NUMBERS;
if (useSpecial) characterPool += SPECIAL_CHARACTERS;
if (characterPool.isEmpty()) {
throw new IllegalArgumentException("At least one character type must be selected.");
}
SecureRandom random = new SecureRandom();
StringBuilder password = new StringBuilder();
for (int i = 0; i < length; i++) {
int index = random.nextInt(characterPool.length());
password.append(characterPool.charAt(index));
}
return password.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter desired password length: ");
int length = scanner.nextInt();
System.out.print("Include lowercase letters? (true/false): ");
boolean useLower = scanner.nextBoolean();
System.out.print("Include uppercase letters? (true/false): ");
boolean useUpper = scanner.nextBoolean();
System.out.print("Include numbers? (true/false): ");
boolean useNumbers = scanner.nextBoolean();
System.out.print("Include special characters? (true/false): ");
boolean useSpecial = scanner.nextBoolean();
try {
String password = generatePassword(length, useLower, useUpper, useNumbers, useSpecial);
System.out.println("Generated Password: " + password);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
scanner.close();
}
}