需求:想要生成位数不低于16的随机密码,而且要包含大小写字母,数字,特殊字符四类
用别人的在线生成器,生成的密码有个别没有数字或者特殊字符,验证方式就是,生成几个长度是4的密码,看一下生成的密码是否都包含这四类
自己简单写了一个js版和java版的
特点1:保证每个密码都包含四类,实现方式:先随机生成这四类各一个字符,比如aA1*,然后放到随机的位置上去,最后给剩余的位置随机取一个字符。
特点2:设置黑名单字符,有的字体或者软件上显示大写i和小写L分不清,如Il,把这两个排除,1可能也会分不清,也可以修改源码加上黑名单。
特点3:四种类型的字符出现的概率一样
js源码如下:
<html>
<head>
</head>
<body>
<div id="data">
</div>
</body>
<script>
function getRandom(start,end){
// 随机数[start,end)
return parseInt(Math.random()*(end-start)+start);
}
function getRandomWithout(start,end,without){
// 随机数[start,end) TODO
var r = parseInt(Math.random()*(end-start)+start);
if(without.includes(r)){
return getRandomWithout(start,end,without);
}
return r;
}
var special = ['$','%','*','+','-',',','.','[',']'];
var blacklist = ['I','l'];
// a-z:97-122 A-Z:65-90 数字:48-57 特殊符号:
function getLetterLow(blacklist){
var v = String.fromCharCode(getRandom(97,123));
if(blacklist.includes(v)){
return getLetterLow(blacklist);
}
return v;
}
function getLetterUp(blacklist){
var v = String.fromCharCode(getRandom(65,91));
if(blacklist.includes(v)){
return getLetterUp(blacklist);
}
return v;
}
function getNumber(blacklist){
var v = String.fromCharCode(getRandom(48,58));
if(blacklist.includes(v)){
return getNumber(blacklist);
}
return v;
}
function getSpecial(blacklist){
var v = special[getRandom(0,special.length)];
if(blacklist.includes(v)){
return getSpecial(blacklist);
}
return v;
}
var length = 16;
var count = 100;
for(var i=0;i<count;i++){
var pwd = new Array(length);
var r_index = new Array(4); // 随机的位置
var r_index_index = 0;
var r_index_data = new Object(); // 随机的位置上的数据
// 随机一个小写字母,并到随机位置
var r1 = getLetterLow(blacklist);
var r1_index = getRandom(0,length);
r_index[r_index_index] = r1_index;
r_index_index++;
r_index_data[r1_index] = r1;
// 随机一个大写字母,并到随机位置
var r2 = getLetterUp(blacklist);
var r2_index = getRandomWithout(0,length,r_index);
r_index[r_index_index] = r2_index;
r_index_index++;
r_index_data[r2_index] = r2;
// 随机一个数字,并到随机位置
var r3 = getNumber(blacklist);
var r3_index = getRandomWithout(0,length,r_index);
r_index[r_index_index] = r3_index;
r_index_index++;
r_index_data[r3_index] = r3;
// 随机一个特殊字符,并到随机位置
var r4 = getSpecial(blacklist);
var r4_index = getRandomWithout(0,length,r_index);
r_index[r_index_index] = r4_index;
r_index_index++;
r_index_data[r4_index] = r4;
for(var j=0;j<length;j++){
if(r_index.includes(j)){
pwd[j] = r_index_data[j];
}else{
var type = getRandom(1,5);
var data = '0';
if(type==1){
data = getLetterLow(blacklist);
}else if(type==2){
data = getLetterUp(blacklist);
}else if(type==3){
data = getNumber(blacklist);
}else if(type==4){
data = getSpecial(blacklist);
}
pwd[j] = data;
}
}
var data = document.getElementById("data");
var p=document.createElement("span");
p.innerHTML=pwd.join('');
data.appendChild(p);
data.appendChild(document.createElement("br"));
}
</script>
</html>
java代码如下:
package com.kingdee.eas.myutil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RandomPasswordUtil {
private static final char[] LetterLow = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
private static final char[] LetterUP = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
private static final char[] Number = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
private static char[] Special = { '!', '#', '$', '%', '*', '+', '-', ',', '.', '?', '[', ']' };
private static List<String> BlackList = new ArrayList<String>();
public static List<String> getRandomPassword(int count, int length) {
BlackList.add("I");
BlackList.add("l");
List<String> list = new ArrayList<String>();
for (int i = 0; i < count; i++) {
char[] pwd = new char[length];
List<Integer> index = new ArrayList<Integer>();
Map<Integer, Character> r_index_data = new HashMap<Integer, Character>();
// 随机一个小写字母,并到随机位置
char r1 = getLetterLow(BlackList); // 数据
int r1_index = getRandom(0, length); // 位置
index.add(r1_index);
r_index_data.put(r1_index, r1);
// 随机一个大写字母,并到随机位置
char r2 = getLetterUp(BlackList); // 数据
int r2_index = getRandomWithout(0, length, index);// 位置
index.add(r2_index);
r_index_data.put(r2_index, r2);
// 随机一个数字,并到随机位置
char r3 = getNumber(BlackList); // 数据
int r3_index = getRandomWithout(0, length, index);// 位置
index.add(r3_index);
r_index_data.put(r3_index, r3);
// 随机一个特殊字符,并到随机位置
char r4 = getSpecial(BlackList); // 数据
int r4_index = getRandomWithout(0, length, index);// 位置
index.add(r4_index);
r_index_data.put(r4_index, r4);
for (int j = 0; j < length; j++) {
if (r_index_data.containsKey(j)) {
pwd[j] = r_index_data.get(j);
} else {
int type = getRandom(1, 5);
char data = '0';
if (type == 1) {
data = getLetterLow(BlackList);
} else if (type == 2) {
data = getLetterUp(BlackList);
} else if (type == 3) {
data = getNumber(BlackList);
} else if (type == 4) {
data = getSpecial(BlackList);
}
pwd[j] = data;
}
}
list.add(String.valueOf(pwd));
}
return list;
}
private static char getLetterLow(List<String> blacklist) {
char v = LetterLow[getRandom(0, LetterLow.length)];
if (blacklist.contains(v)) {
return getLetterLow(blacklist);
}
return v;
}
private static char getLetterUp(List<String> blacklist) {
char v = LetterUP[getRandom(0, LetterUP.length)];
if (blacklist.contains(v)) {
return getLetterUp(blacklist);
}
return v;
}
private static char getNumber(List<String> blacklist) {
char v = Number[getRandom(0, Number.length)];
if (blacklist.contains(v)) {
return getNumber(blacklist);
}
return v;
}
private static char getSpecial(List<String> blacklist) {
char v = Special[getRandom(0, Special.length)];
if (blacklist.contains(v)) {
return getSpecial(blacklist);
}
return v;
}
private static int getRandom(int start, int end) {
return (int) (Math.random() * (end - start) + start);
}
private static int getRandomWithout(int start, int end, List<Integer> without) {
int r = (int) (Math.random() * (end - start) + start);
if (without.contains(r)) {
return getRandomWithout(start, end, without);
}
return r;
}
public static void main(String[] args) {
List<String> list = getRandomPassword(100, 4);
for (String s : list) {
System.out.println(s);
}
}
}