如何在JAVA中生成一个特殊字符,一个大写,一个小写的强制密码

  • 本文关键字:一个 密码 特殊字符 JAVA java
  • 更新时间 :
  • 英文 :


假设我将输入参数作为int,并希望以字符串形式返回。我该怎么做?我想要强制性的一个特殊字符,一个大写,一个小写,每次生成密码后。我该怎么做?

我在下面写了这段代码,得到了一个解决方案,但它并没有每次在生成的输出中添加大写、小写和特殊字符。

public int GeneratePassword(int length) {
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu vwxyz0123456789~`!@#$%^&*()-_=+[{]}\|;:'",<.>/?";
String pwd = RandomStringUtils.random(length , characters );
System.out.println("pwd:="+pwd );
return 0;
}

Jose Martinez已经提供了一个很好的方法来实现这一点,这是他的方法的代码片段

public static String getPassword(int length) {
assert length >= 4;
char[] password = new char[length];
//get the requirements out of the way
password[0] = LOWERCASE[rand.nextInt(LOWERCASE.length)];
password[1] = UPPERCASE[rand.nextInt(UPPERCASE.length)];
password[2] = NUMBERS[rand.nextInt(NUMBERS.length)];
password[3] = SYMBOLS[rand.nextInt(SYMBOLS.length)];
//populate rest of the password with random chars
for (int i = 4; i < length; i++) {
password[i] = ALL_CHARS[rand.nextInt(ALL_CHARS.length)];
}
//shuffle it up
for (int i = 0; i < password.length; i++) {
int randomPosition = rand.nextInt(password.length);
char temp = password[i];
password[i] = password[randomPosition];
password[randomPosition] = temp;
}
return new String(password);
}

如果你想看看整个方法,一定要看看他的答案——所有学分都归功于他。

https://stackoverflow.com/a/51823845/6895166

这里有一种使用SecureRandomStringBuilder的方法

private static String generateRandomString(int length)
{
StringBuilder sb = new StringBuilder();
SecureRandom rnd = new SecureRandom();
String uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
String specialChars = "0123456789~`!@#$%^&*()-_=+[{]}\\|;:\'\",<.>/?";
boolean nextIsUppercase = false;
boolean nextIsLowercase = false;
boolean nextIsSpecial = true;
for (int i = 0; i < length; i++) {
if (nextIsSpecial) {
sb.append(specialChars.charAt(rnd.nextInt(specialChars.length())));
nextIsUppercase = true;
nextIsSpecial = false;
continue;
}
if (nextIsUppercase) {
sb.append(uppercaseChars.charAt(rnd.nextInt(uppercaseChars.length())));
nextIsUppercase = false;
nextIsLowercase = true;
continue;
}
if (nextIsLowercase) {
sb.append(lowercaseChars.charAt(rnd.nextInt(lowercaseChars.length())));
nextIsLowercase = false;
nextIsSpecial = true;
continue;
}
}
return sb.toString();
}

样本输出:

System.out.println(generateRandomString(1)); -> 7
System.out.println(generateRandomString(2)); -> :Q
System.out.println(generateRandomString(3)); -> 8St
System.out.println(generateRandomString(4)); -> =Lv%
System.out.println(generateRandomString(16)); ->  %Uf-Hs<Ea|Wp;Rt}

这是您的问题的解决方案:

public static String GeneratePassword(int length) {
String[] characters = {"0123456789","~!@#$%^&*()-_=+[{]}|;:'",<.>/?","ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz"};
Random rand = new Random();
String password="";
for(int i=0;i<length;i++) {
int random = rand.nextInt(4);//choose a number from 0 to 3(inclusive)
int string_size = characters[random].length();//get length of the selected string
int random1 = rand.nextInt(string_size);//choose a number from0 to length-1 of selected string
char item = characters[random].charAt(random1);//selects the character
password=password+item;//Concatenates with the password
}
return password;
}

相关内容

最新更新