forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJAVA Generate Secure Password
31 lines (24 loc) · 1.12 KB
/
JAVA Generate Secure Password
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
import java.security.SecureRandom;
import java.util.Random;
public class GeneratePasswordExample1 {
public static void main(String[] args) {
// Generate and print a secure random password
String securePassword = generateSecurePassword(12); // You can specify the desired password length here
System.out.println("The generated secure password is: " + securePassword);
}
public static String generateSecurePassword(int length) {
String uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
String digitChars = "0123456789";
String specialChars = "!@#$%^&*()_+";
String allCharacters = uppercaseChars + lowercaseChars + digitChars + specialChars;
// Use a secure random number generator
Random random = new SecureRandom();
StringBuilder password = new StringBuilder();
for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(allCharacters.length());
password.append(allCharacters.charAt(randomIndex));
}
return password.toString();
}
}