如何创建密码? [英] How can I create a password?

查看:34
本文介绍了如何创建密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于如何创建密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆