Java密码生成器

问题描述 投票:9回答:10

我正在尝试创建一个创建密码的java程序,全部是小写,小写和大写,小写和大写以及数字,小写和大写以及数字和标点符号,程序还必须创建用户选择的密码之一并且必须根据用户选择的内容生成密码长度。我已经为用户选择了密码选项,并提示他选择一个。我现在仍然坚持如何创建上面提到的密码类型。一个人建议我使用ASCII值,然后将它们转换为文本。我知道如何将它们转换为文本,但它会显示数字,字母和标点符号。有没有什么办法可以只为小写字母生成ASCII值?另外,我将如何根据用户提供的密码生成密码?

java passwords generator
10个回答
0
投票
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class PassGen {

    private String str;
    private int randInt;
    private StringBuilder sb;
    private List<Integer> l;

    public PassGen() {
        this.l = new ArrayList<>();
        this.sb = new StringBuilder();

        buildPassword();
    }

    private void buildPassword() {

        //Add ASCII numbers of characters commonly acceptable in passwords
        for (int i = 33; i < 127; i++) {
            l.add(i);
        }

        //Remove characters /, \, and " as they're not commonly accepted
        l.remove(new Integer(34));
        l.remove(new Integer(47));
        l.remove(new Integer(92));

        /*Randomise over the ASCII numbers and append respective character
          values into a StringBuilder*/
        for (int i = 0; i < 10; i++) {
            randInt = l.get(new SecureRandom().nextInt(91));
            sb.append((char) randInt);
        }

        str = sb.toString();
    }

    public String generatePassword() {
        return str;
    }
}

1
投票

您可以随机选择具有维度的数字,字母和标点符号。 Ansi编号从30到39,小写字母从61-7A,依此类推。使用qazxsw poi


0
投票

如果是我,我会建立代表你将允许的各种字符集的字符数组(ansii tables),然后在你的生成器方法中选择适当的字符数组,并从中生成密码。复杂的部分然后变得创建字符数组......

char[] ...

然后你的问题只是生成char []数组,代表你拥有的各种规则,以及如何将该集合传递给generate方法。

一种方法是设置一个与你允许的规则匹配的正则表达式规则列表,然后通过规则发送每个字符....如果它们符合规则,那么添加它们.....

考虑一个看起来像这样的函数:

public String generate(char[] validchars, int len) {
    char[] password = new char[len];
    Random rand = new Random(System.nanoTime());
    for (int i = 0; i < len; i++) {
        password[i] = validchars[rand.nextInt(validchars.length)];
    }
    return new String(password);
}

然后,您可以获得一个字母字符数组(仅限小写):

public static final char[] getValid(final String regex, final int lastchar) {
    char[] potential = new char[lastchar]; // 32768 is not huge....
    int size = 0;
    final Pattern pattern = Pattern.compile(regex);
    for (int c = 0; c <= lastchar; c++) {
        if (pattern.matcher(String.valueOf((char)c)).matches()) {
            potential[size++] = (char)c;
        }
    }
    return Arrays.copyOf(potential, size);
}

或者,所有“单词”字符的列表:

getValid("[a-z]", Character.MAX_VALUE);

然后,选择正则表达式以匹配您的要求,并“存储”每次要重用的有效字符数组。 (每次生成密码时都不要生成字符....)


0
投票

您可以尝试Unix“pwgen”的Java实现。 getValid("\\w", Character.MAX_VALUE); 它包含了在Bitbucket上使用CLI进行jpwgen库实现的链接以及指向GAE部署样本的链接。


0
投票

我做了一个简单的程序,用ASCII数字填充https://github.com/antiso/pwgen-gae,然后使用ArrayList数字生成器在SecureRandom循环中随机化,在其中你可以设置你想要的字符数。

for

希望这可以帮助! :)

© www.soinside.com 2019 - 2024. All rights reserved.