/**
* 随机出用户输入的密码位数的密码, 从大小写字母, 数字, 特殊符号中取值
*/
public static String randomPassword(int num) {
char[] password = new char[num];
Random rand = new Random();
//在ASCII码表中,48-57:数字, 65-90:大写字母, 97-122:小写字母, 33-47: 特殊符号
for (int i = 0; i < password.length; i++) {
int choice = rand.nextInt(4);
int lowercase = rand.nextInt(26) + 65;
int uppercase = rand.nextInt(26) + 97;
int figure = rand.nextInt(10) + 48;
int special = rand.nextInt(15) + 33;
//将 " 和 ' 替换成#
if (special == 34 || special == 39) {
special = 35;
}
//从大写字母.小写字母.数字.特殊字符中随机取值
switch (choice) {
case 0:
password[i] = (char) lowercase;
break;
case 1:
password[i] = (char) uppercase;
break;
case 2:
password[i] = (char) figure;
break;
case 3:
password[i] = (char) special;
break;
default:
}
}
return new String(password);
}
Java随机密码生成器
于 2022-07-08 15:07:54 首次发布