工具(十一)随机密码生成工具-可选择字符种类:
废话少絮,上代码:
一、可选择字符种类随机密码生成工具的实现思路:
- 准备好要包含的字符串,去掉易混淆字符;
- 随机生成字符串,按照长度,不同类型的字符串随机取;
- 将必须出现的字符类型先放到前几个位置,后面的位置随机取;
- 将生成的字符打乱顺序,以免前几个字符的类型是固定的导致密码强度降低。
import java.util.*;
/**
* 随机密码生成工具
*
* @author wangdy
*/
public class RandomPwdUtil {
// 特殊字符
private static final String SPECIAL_CHARS = "!@#$%&*_=";
// 去掉I、L、O、Q易混淆字符
private static final String UPPER_WORD_CHARS = "ABCDEFGHJKMNPRSTUVWXYZ";
// 去掉i、l、o、q易混淆字符
private static final String LOWER_WORD_CHARS = "abcdefghjkmnprstuvwxyz";
// 去掉1、0易混淆字符
private static final String NUMBER_CHARS = "23456789";
/**
* @param length 生成密码的长度 建议至少6位
* @param isLowerWord 是否包含小写字母
* @param isUpperWord 是否包含大写字母
* @param isSpecialChar 是否包含特殊字符
* @param isNum 是否包含数字
* @return String 随机密码
* @description: 生成随机密码的工具方法
*/
public static String randomPassword(int length, Boolean isLowerWord, Boolean isUpperWord, Boolean isSpecialChar, Boolean isNum) {
Random rnd = new Random();
// 先取必填项1个
Map<Integer, String> map = new HashMap<>();
if (isLowerWord) {
map.put(1, LOWER_WORD_CHARS);
}
if (isUpperWord) {
map.put(2, UPPER_WORD_CHARS);
}
if (isSpecialChar) {
map.put(3, SPECIAL_CHARS);
}
if (isNum) {
map.put(4, NUMBER_CHARS);
}
StringBuilder sb = new StringBuilder();
for (Map.Entry<Integer, String> entry : map.entrySet()) {
String value = entry.getValue();
sb.append(value.charAt(rnd.nextInt(value.length())));
}
char[] chars = new char[length - sb.length()];
for (int i = 0; i < length - sb.length(); i++) {
chars[i] = nextChar(rnd, isLowerWord, isUpperWord, isSpecialChar, isNum);
}
StringBuilder resultSb = sb.append(new String(chars));
// 对结果数据乱序处理(前面固定放到前几个了,故需乱序)
String[] split = resultSb.toString().split("");
List<String> strings = Arrays.asList(split);
Collections.shuffle(strings);
StringBuilder stringBuilder = new StringBuilder(strings.size());
strings.forEach(stringBuilder::append);
System.out.println("乱序前:" + resultSb + "-----乱序后:" + stringBuilder);
return stringBuilder.toString();
}
private static char nextChar(Random rnd, Boolean isLowerWord, Boolean isUpperWord, Boolean isSpecialChar, Boolean isNum) {
List<Integer> list = new ArrayList<>();
if (isLowerWord) {
list.add(1);
}
if (isUpperWord) {
list.add(2);
}
if (isSpecialChar) {
list.add(3);
}
if (isNum) {
list.add(4);
}
if (list.size() == 0) {
// 默认数字密码
list.add(4);
}
// 随机list索引
int index = rnd.nextInt(list.size());
Integer integer = list.get(index);
switch (integer) {
case 1:
return LOWER_WORD_CHARS.charAt(rnd.nextInt(LOWER_WORD_CHARS.length()));
case 2:
return UPPER_WORD_CHARS.charAt(rnd.nextInt(UPPER_WORD_CHARS.length()));
case 3:
return SPECIAL_CHARS.charAt(rnd.nextInt(SPECIAL_CHARS.length()));
default:
return NUMBER_CHARS.charAt(rnd.nextInt(NUMBER_CHARS.length()));
}
}
/**
* 测试main
**/
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
// String s = randomPassword(6, false, false, false, false);
String s = randomPassword(6, true, true, true, true);
System.out.println("" + s);
}
}
}
二、结果查看:
以下为结果输出:
乱序前:uC*99=-----乱序后:=9C9u*
=9C9u*
乱序前:jU%67&-----乱序后:%6U7&j
%6U7&j
乱序前:rV@8n&-----乱序后:r&8@nV
r&8@nV
乱序前:xM!37_-----乱序后:7!3Mx_
7!3Mx_
乱序前:gJ!562-----乱序后:g62!J5
g62!J5
乱序前:vA$74s-----乱序后:v$7A4s
v$7A4s
乱序前:jC*4PF-----乱序后:C4jF*P
C4jF*P
乱序前:dU$55$-----乱序后:55$dU$
55$dU$
乱序前:fV%26$-----乱序后:2$6fV%
2$6fV%
乱序前:xA%47*-----乱序后:x4*A%7
x4*A%7
乱序前:nY_3Vx-----乱序后:3VYn_x
3VYn_x
乱序前:nZ#2C7-----乱序后:Zn27#C
Zn27#C
乱序前:uK#94e-----乱序后:#K49eu
#K49eu
乱序前:kE&8@D-----乱序后:k&D@E8
k&D@E8
乱序前:fP_92u-----乱序后:P2_f9u
P2_f9u
乱序前:vD_8ae-----乱序后:8vDae_
8vDae_
乱序前:dG&9%J-----乱序后:&JdG9%
&JdG9%
乱序前:aZ!8hX-----乱序后:X!8Zha
X!8Zha
乱序前:hT&57s-----乱序后:h&s5T7
h&s5T7
乱序前:dU_9=6-----乱序后:Ud_96=
Ud_96=
乱序前:kF%23%-----乱序后:3%2k%F
3%2k%F
乱序前:mE$4_2-----乱序后:42_$mE
42_$mE
乱序前:fJ#5$f-----乱序后:f$J5#f
f$J5#f
乱序前:eD@7P#-----乱序后:P@7#De
P@7#De
乱序前:zD!8S&-----乱序后:z!8S&D
z!8S&D
乱序前:dJ@6%C-----乱序后:@dJ6%C
@dJ6%C
乱序前:sB&6c9-----乱序后:c9Bs6&
c9Bs6&
乱序前:bJ!3FD-----乱序后:!FJbD3
!FJbD3
乱序前:nB#28e-----乱序后:82#neB
82#neB
乱序前:cG!7_9-----乱序后:G!_9c7
G!_9c7
乱序前:cW*6ux-----乱序后:x6uc*W
x6uc*W
乱序前:xZ_93n-----乱序后:3xn9_Z
3xn9_Z
乱序前:yB&2Y3-----乱序后:23yBY&
23yBY&
乱序前:mK@6p4-----乱序后:4p6mK@
4p6mK@
乱序前:eJ@54=-----乱序后:5=@J4e
5=@J4e
乱序前:xY_62*-----乱序后:_xY26*
_xY26*
乱序前:sK$5!p-----乱序后:!$Ksp5
!$Ksp5
乱序前:nN!6ec-----乱序后:nN!6ec
nN!6ec
乱序前:gH%7Xf-----乱序后:%7HgXf
%7HgXf
乱序前:vK*93v-----乱序后:K3v*v9
K3v*v9
乱序前:gZ%38h-----乱序后:Z38g%h
Z38g%h
乱序前:mE_265-----乱序后:6mE5_2
6mE5_2
乱序前:wB@4S%-----乱序后:%wB4@S
%wB4@S
乱序前:rC!7eR-----乱序后:rReC7!
rReC7!
乱序前:eS_3ep-----乱序后:e_e3Sp
e_e3Sp
乱序前:jH!7bM-----乱序后:Mbj!7H
Mbj!7H
乱序前:rY@78j-----乱序后:7Yr8@j
7Yr8@j
乱序前:mW$8pe-----乱序后:$pmeW8
$pmeW8
乱序前:cE%4Yr-----乱序后:4YcE%r
4YcE%r
乱序前:vM@8aJ-----乱序后:v8MaJ@
v8MaJ@
乱序前:fF@5Z*-----乱序后:5Z@*Ff
5Z@*Ff
乱序前:fK@9v5-----乱序后:9Kf5@v
9Kf5@v
乱序前:vF!83%-----乱序后:%3v8!F
%3v8!F
乱序前:xM_3Y@-----乱序后:_Yx@3M
_Yx@3M
乱序前:sP$2%r-----乱序后:rP%$s2
rP%$s2
乱序前:wX@5A$-----乱序后:A$w@5X
A$w@5X
乱序前:kY_6Vn-----乱序后:n_6YkV
n_6YkV
乱序前:wW#7*2-----乱序后:2#W*7w
2#W*7w
乱序前:nJ!6$N-----乱序后:n!N$J6
n!N$J6
乱序前:bJ_4Ed-----乱序后:b_Ed4J
b_Ed4J
乱序前:rS&2J2-----乱序后:2S2&Jr
2S2&Jr
乱序前:jN_8&7-----乱序后:j8_N7&
j8_N7&
乱序前:vV&9G&-----乱序后:v&9V&G
v&9V&G
乱序前:sX!3kt-----乱序后:Xk3st!
Xk3st!
乱序前:tH!97@-----乱序后:@!t9H7
@!t9H7
乱序前:gS#6ya-----乱序后:S6ayg#
S6ayg#
乱序前:eM&635-----乱序后:63e&M5
63e&M5
乱序前:rX$2%*-----乱序后:2X$*%r
2X$*%r
乱序前:zJ=83A-----乱序后:J3z=A8
J3z=A8
乱序前:gR*2NB-----乱序后:*2BRgN
*2BRgN
乱序前:jS=2zc-----乱序后:2Szc=j
2Szc=j
乱序前:pC&5ED-----乱序后:E&DC5p
E&DC5p
乱序前:xJ%89M-----乱序后:8J%9xM
8J%9xM
乱序前:jT!5HB-----乱序后:j5!BHT
j5!BHT
乱序前:dN_7#2-----乱序后:d_2#N7
d_2#N7
乱序前:gE@524-----乱序后:E42g5@
E42g5@
乱序前:fV&9%#-----乱序后:9%&V#f
9%&V#f
乱序前:nK*9jK-----乱序后:9j*nKK
9j*nKK
乱序前:wV!5_*-----乱序后:5!w*V_
5!w*V_
乱序前:eD@53s-----乱序后:@53Dse
@53Dse
乱序前:tD!2s5-----乱序后:!2stD5
!2stD5
乱序前:sB*89_-----乱序后:B8_s*9
B8_s*9
乱序前:gB&2a8-----乱序后:a8&g2B
a8&g2B
乱序前:aE&6z!-----乱序后:!z&6Ea
!z&6Ea
乱序前:tG$8a9-----乱序后:9$taG8
9$taG8
乱序前:tK_8Ke-----乱序后:e8KtK_
e8KtK_
乱序前:gR#84Y-----乱序后:RY8#4g
RY8#4g
乱序前:kD$4@2-----乱序后:4$@D2k
4$@D2k
乱序前:vF=5XF-----乱序后:v5FXF=
v5FXF=
乱序前:pU=77b-----乱序后:pb7U=7
pb7U=7
乱序前:sW=6!Z-----乱序后:W=!Z6s
W=!Z6s
乱序前:aJ&7N@-----乱序后:7JN@a&
7JN@a&
乱序前:bE*639-----乱序后:E*3b96
E*3b96
乱序前:zB%2j*-----乱序后:Bj*z2%
Bj*z2%
乱序前:sR%7=j-----乱序后:j=Rs%7
j=Rs%7
乱序前:pT*9#v-----乱序后:T*p#v9
T*p#v9
乱序前:sM!3!c-----乱序后:3!sMc!
3!sMc!
乱序前:vY#8$7-----乱序后:#$v78Y
#$v78Y
乱序前:nS&7V%-----乱序后:S7n&V%
S7n&V%
乱序前:eS_78H-----乱序后:8SH_e7
8SH_e7
Process finished with exit code 0
以上输出表示密码已经成功输出,并打乱顺序,密码强度很高,可以提高位数的方式增加密码强度。
END