我想给一些用户提供一百万个密码:
我应该在这里使用Random
吗?怎么样?
答案 0 :(得分:34)
RandomStringUtils提供了一些生成随机String的方法,可以用作密码。
以下是8个字符密码创建的一些示例:
// 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
当然,您还有一些方法可能会限制密码生成所允许的字符集:
for (int i = 0; i < 8; i++) {
System.out.println(RandomStringUtils.random(8, "abcDEF123"));
}
只会创建包含字符a,b,c,D,E,F,1,2或3的密码:
D13DD1Eb
cac1Dac2
FE1bD2DE
2ab3Fb3D
213cFEFD
3c2FEDDF
FDbFcc1E
b2cD1c11
答案 1 :(得分:24)
出于安全原因(即密码)使用Apache的RandomStringUtils
时,结合使用SecureRandom
来源非常重要:
RandomStringUtils.random(6, 0, 0, true, true, null, new SecureRandom());
答案 2 :(得分:10)
使用SecureRandom,它会提供更随机的密码。
您可以使用以下内容创建单个密码(注意:未经测试的代码)。
// put here all characters that are allowed in password
char[] allowedCharacters = {'a','b','c','1','2','3','4'};
SecureRandom random = new SecureRandom();
StringBuffer password = new StringBuffer();
for(int i = 0; i < PASSWORD_LENGTH; i++) {
password.append(allowedCharacters[ random.nextInt(allowedCharacters.length) ]);
}
请注意,此不保证每个密码都包含数字和字符。
答案 3 :(得分:5)
这是我写的一段时间:
package com.stackoverflow.does.my.code.for.me;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
public class PasswordUtil {
/** Minimum password length = 6 */
public static final int MIN_PASSWORD_LENGTH = 6;
/** Maximum password length = 8 */
public static final int MAX_PASSWORD_LENGTH = 8;
/** Uppercase characters A-Z */
public static final char[] UPPERS = new char[26];
/** Lowercase characters a-z */
public static final char[] LOWERS = new char[26];
/**
* Printable non-alphanumeric characters, excluding space.
*/
public static final char[] SPECIALS = new char[32];
public static final char[] DIGITS = new char[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
static {
// Static initializer block for populating arrays
int U = 'A';
int l = 'a';
int d = '0';
for (int i = 0; i < 26; i++) {
UPPERS[i] = (char) (U + i);
LOWERS[i] = (char) (l + i);
if (i < 10) {
DIGITS[i] = (char) (d + i);
}
}
int p = 0;
for (int s = 33; s < 127; s++) {
char specialChar = (char) 32;
if (s >= 'a' && s <= 'z')
s = 'z' + 1; // jump over 'a' to 'z'
else if (s >= 'A' && s <= 'Z')
s = 'Z' + 1; // jump over 'A' to 'Z'
else if (s >= '0' && s <= '9')
s = '9' + 1; // jump over '0' to '9'
specialChar = (char) s;
SPECIALS[p] = specialChar;
p++;
}
}
public String generatePassword() {
List<char[]> activeSets = new ArrayList<char[]>(4);
List<char[]> inactiveSets = new ArrayList<char[]>(4);
activeSets.add(UPPERS);
activeSets.add(LOWERS);
activeSets.add(SPECIALS);
activeSets.add(DIGITS);
SecureRandom random = new SecureRandom();
int passwordLength = 5 + random.nextInt(3);
StringBuffer password = new StringBuffer(passwordLength + 1);
for (int p = 0; p <= passwordLength; p++) {
char[] randomSet = null;
if (activeSets.size() > 1) {
int rSet = random.nextInt(activeSets.size());
randomSet = activeSets.get(rSet);
inactiveSets.add(randomSet);
activeSets.remove(rSet);
} else {
randomSet = activeSets.get(0);
inactiveSets.add(randomSet);
activeSets.clear();
activeSets.addAll(inactiveSets);
inactiveSets.clear();
}
int rChar = random.nextInt(randomSet.length);
char randomChar = randomSet[rChar];
password.append(randomChar);
}
return password.toString();
}
}
答案 4 :(得分:3)
这也是一个不错的选择:
String password = Integer.toString((int) (Math.random() * Integer.MAX_VALUE), 36);
然而,它并不保证密码总是包含两个数字和字母,但大多数上述建议也不会这样做。
答案 5 :(得分:1)
我会做的是这样的事情:
答案 6 :(得分:0)
这将是最简单的一个:)
String char_group = "abcdefghijklmnopqrstuvwxyz";
String digit_group = "123456789";
// first choose a len of pwd
Random ran = new Random();
int pwd_len = ran.nextInt(50); //50 is the max length of password,say
// check that pwd_len is not less than 6
// do the check here
// finally create the password..
StringBuffer pwd = new StringBuffer();
Random RNG = new Random();
for (int i = 0; i < pwd_len ; i++) {
int randomNum = RNG.nextInt(100);
char c = '';
// Here 25% is the ratio of mixing digits
// in the password, you can change 4 to any
// other value to change the depth of mix
// or you can even make it random.
if (randomNum % 4 == 0) {
c = digit_group[randomNum % digit_group.length];
} else {
c = char_group[randomNum % char_group.length];
}
pwd.append(c);
}
return pwd.toString();
答案 7 :(得分:0)
有点晚了,但我通常使用以下代码:
private static final int PASSWORD_SIZE = 16;
private static final String VALID_SPECIAL_CHARACTERS = "!@#$%&*()_-+=[]{}\\|:/?.,><"; // Note the double \ as escape
private static String createPassword() {
SecureRandom random = new SecureRandom();
StringBuilder password = new StringBuilder();
while (password.length() < PASSWORD_SIZE) {
char character = (char) random.nextInt(Character.MAX_VALUE);
if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9') || VALID_SPECIAL_CHARACTERS.contains(String.valueOf(character))) {
password.append(character);
}
}
return password.toString();
}
无法保证密码中始终存在数字,特殊字符,小写字母和大写字符。 这可以通过首先添加一个字符和一个数字来强制执行,但是这会创建更具可预测性的密码。
答案 8 :(得分:0)
以下是如何确保生成的密码符合您的密码标准,例如:在您的情况下,我会使用此正则表达式:
<code>String regex = "^(?=[a-zA-Z0-9ñÑ]*\d)(?=[a-zA-Z0-9ñÑ]*[a-z])(?=[a-zA-Z0-9ñÑ]*[A-Z])[a-zA-Z0-9ñÑ]{6,}$"</code>
此正则表达式符合以下条件:
1.-至少1个lowerCase字母
2.-至少1个upperCase字母
3.-至少1位数(数字)
4.-至少6个字符(请注意,在正则表达式结尾处使用逗号后添加大于6的数字,现在将符合至少6个字符和最大值的标准)< / p>
<code>char[] char = {'a','b','c','d','e','f','g','h',...};
SecureRandom random = new SecureRandom();
StringBuffer password = new StringBuffer();</code>
while(!password.toString().matches("your regex")){
for(int i = 0; i < 8; i++) {
password.append(char [ random.nextInt(char .length) ]);
}
}
System.out.println(password.toString());
此代码的作用是while
您生成的密码不符合您的条件,它会反复循环for
循环。
答案 9 :(得分:0)
实施了PasswordBuilder。支持passwrod限制,必须包含字符及其中的多少,字符范围及其列表。
用法示例:
left
示例结果:System.out.println(new PasswordBuilder().addCharsOption("!@#$%&*()_-+=[]{}\\|:/?.,><", 1).addRangeOption('A', 'Z', 1).addRangeOption('a', 'z', 0).addRangeOption('0', '9', 1).build());
QU1GY7p+j+-PUW+_
示例结果:System.out.println(new PasswordBuilder().addCharsOption("!@#$%&*()_-+=[]{}\\|:/?.,><", 1).addRangeOption('A', 'Z', 1).addRangeOption('a', 'z', 0).addRangeOption('0', '9', 1).setSize(5).build());
实现:
%,4NX