Java-生成带有特殊字符的随机密码

时间:2019-03-20 15:36:14

标签: java

嗨,我想从下面提到的每个字符串中选择2个随机字符。

    String chars = "abcdefghijklmnopqrstuvwxyz";
    String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String NUMS = "1234567890";
    String SPEC = "@#$%^&+=";

例如。预期-cdAB43#-

我在下面尝试过,但是没有用。

        Random rnd = new Random();
        index = (int) (rnd.nextFloat() * chars.length());
        pass.append(chars.charAt(index));
        index = (int) (rnd.nextFloat() * NUMS.length());
        pass.append(NUMS.charAt(index));
        index = (int) (rnd.nextFloat() * SPEC.length());
        pass.append(SPEC.charAt(index));

        String password = pass.toString();
        return password;

输出-

VNVLZt5#

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

这可以解决问题:

private String chars = "abcdefghijklmnopqrstuvwxyz";
private String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String NUMS = "1234567890";
private String SPEC = "@#$%^&+=";
private Random rnd = new Random();

private String getTwoFrom(String src) {
    int index1 = (int) (rnd.nextFloat() * src.length()),
            index2 = (int) (rnd.nextFloat() * src.length());
    return "" + src.charAt(index1) + src.charAt(index2);
}

public String createPassword() {
    return getTwoFrom(chars) + getTwoFrom(CHARS) + getTwoFrom(NUMS) + getTwoFrom(SPEC);
}

答案 1 :(得分:0)

我从您的问题推断出,您想从每个字符集中选择2个随机字符,但您只选择了1个字符。此外,我使用nextInt代替nextFloat,因为我认为它更易读,并且我认为您使用的是ListStringBuffer,所以我删除了它,因为这里不需要它(只能使用String来完成)。最终结果如下所示:

static String chars = "abcdefghijklmnopqrstuvwxyz";
static String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String NUMS = "1234567890";
static String SPEC = "@#$%^&+=";


public static String passwordGenerator() {
    int index;
    String pass = "";
    Random rnd = new Random();

    // 2 random chars from 'chars'
    index = rnd.nextInt(chars.length());
    pass += chars.charAt(index);
    index = rnd.nextInt(chars.length());
    pass += chars.charAt(index);

    // 2 random chars from 'CHARS'
    index = rnd.nextInt(CHARS.length());
    pass += CHARS.charAt(index);
    index = rnd.nextInt(CHARS.length());
    pass += CHARS.charAt(index);

    // 2 random chars from 'NUMS'
    index = rnd.nextInt(NUMS.length());
    pass += NUMS.charAt(index);
    index = rnd.nextInt(NUMS.length());
    pass += NUMS.charAt(index);

    // 2 random chars from 'SPEC'
    index = rnd.nextInt(SPEC.length());
    pass += SPEC.charAt(index);
    index = rnd.nextInt(SPEC.length());
    pass += SPEC.charAt(index);

    return pass;
}

答案 2 :(得分:0)

这是一个可行的解决方案:

package com.mycompany.app;

import java.util.Random;

public class Solution {
    public static String twoRandChars(String src) {
        Random rnd = new Random();
        int index1 = (int) (rnd.nextFloat() * src.length());
        int index2 = (int) (rnd.nextFloat() * src.length());
        return "" + src.charAt(index1) + src.charAt(index2);
    }

    public static void main(String args[]) {
        String chars = "abcdefghijklmnopqrstuvwxyz";
        String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String NUMS = "1234567890";
        String SPEC = "@#$%^&+=";

        String pass = twoRandChars(chars) + twoRandChars(CHARS) + twoRandChars(NUMS) + twoRandChars(SPEC);

        System.out.print(pass);
    }
}

Check the demo