如何创建密码?

2022-01-22 00:00:00 random passwords security java

我想给一些应该像这样的用户提供一百万个密码:

I want to give maybe a million password to some users that should be like:

  1. 必须至少有 6 个字符
  2. 它必须有数字和字母

我应该在这里使用 Random 吗?怎么样?

Should I use Random here? How?

推荐答案

RandomStringUtils 提供了一些生成随机字符串的方法,可以用作密码.

RandomStringUtils from Apache Commons Lang provide some methods to generate a randomized String, that can be used as password.

以下是一些创建 8 字符密码的示例:

Here are some examples of 8-characters passwords creation:

// Passwords with only alphabetic characters.
for (int i = 0; i < 8; i++) {
    System.out.println(RandomStringUtils.randomAlphabetic(8));
}
System.out.println("--------");
// Passwords with alphabetic and numeric characters.
for (int i = 0; i < 8; i++) {
    System.out.println(RandomStringUtils.randomAlphanumeric(8));
}

创建以下结果:

zXHzaLdG
oDtlFDdf
bqPbXVfq
tzQUWuxU
qBHBRKQP
uBLwSvnt
gzBcTnIm
yTUgXlCc
--------
khDzEFD2
cHz1p6yJ
3loXcBau
F6NJAQr7
PyfN079I
8tJye7bu
phfwpY6y
62q27YRt

当然,您也有一些方法可以限制密码生成所允许的字符集:

Of course, you have also methods that may restrict the set of characters allowed for the password generation:

for (int i = 0; i < 8; i++) {
    System.out.println(RandomStringUtils.random(8, "abcDEF123"));
}

将只创建包含字符 a、b、c、D、E、F、1、2 或 3 的密码:

will create only passwords with the characters a, b, c, D, E, F, 1, 2 or 3:

D13DD1Eb
cac1Dac2
FE1bD2DE
2ab3Fb3D
213cFEFD
3c2FEDDF
FDbFcc1E
b2cD1c11

相关文章