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

时间:2018-10-26 15:35:54

标签: java

假设我将输入参数设为int,并且我想返回String。我怎样才能做到这一点?生成密码后,我每次都要强制使用一个特殊字符,一个大写字母,一个小写字母。我该怎么办?

我在下面编写了这段代码,但得到了解决方案,但它不会每次都在生成的输出中添加大写,小写,特殊字符。

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

    String pwd = RandomStringUtils.random(length , characters );
    System.out.println("pwd:="+pwd );

    return 0;
}

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

这是使用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}

答案 2 :(得分:0)

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

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;
}