这是一个随机生成,自定义长度,自定义内容,自定义组合的Java密码随机生成工具!
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
/**
* @Description TODO 密码随机生成工具
* @Classname pw
* @Author qingjian.kong
* @Date 2023/4/12 16:45
* @Version V1.0
*/
public class PwUtil {
//随机长度
private static final int l = 16;
//是否需要符号
private static final String Y = "y";
private static final String N = "n";
public static void main(String[] args) {
SecureRandom secureRandom = new SecureRandom();
List<String> list = new ArrayList<String>();
StringBuffer sb = new StringBuffer();
// 循环下找个看的顺眼的
for (int i = 0; i < 50; i++) {
orderlyList(list,secureRandom,N,l);
System.out.println(disorderList(list,secureRandom,sb));
list.clear();
sb.setLength(0);
}
}
/**
* 有序: 数字+小写字母+大写字母+特殊符号
* @param list
* @param secureRandom
*/
private static void orderlyList(List<String> list, SecureRandom secureRandom,String flag,int length) {
// String[] a = {"0","1","2","3","4","5","6","7","8","9"};
// String[] b = {"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"};
// String[] c = {"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"};
// String[] d = {"~","!","@","#","$","^","&","*","_","+","\\","/","`","|",".","=","-",",","?",":","'","<",">","(",")","%",";","\""};
String[] f = {"0","1","2","3","4","5","6","7","8","9","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","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"};
String[] g = {"0","1","2","3","4","5","6","7","8","9","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","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","~","!","@","#","$","^","&","*","_","+","\\","/","`","|",".","=","-",",","?",":","'","<",">","(",")","%",";","\""};
int n = 0;
if (flag.equals("y")){
// 数字、小写字母、+大写字母、+特殊符号各四位
for (int i = 0; i < length; i++) {
// // 随机的范围
// String a1 = a[secureRandom.nextInt(a.length)];
// String b1 = b[secureRandom.nextInt(b.length)];
// String c1 = c[secureRandom.nextInt(c.length)];
// String d1 = d[secureRandom.nextInt(d.length)];
// list.add(i+n,a1);
// n++;
// list.add(i+n,b1);
// n++;
// list.add(i+n,c1);
// n++;
// list.add(i+n,d1);
// 随机的范围
String g1 = g[secureRandom.nextInt(g.length)];
list.add(i+n,g1);
}
}else {
// 数字、小写字母、+大写字母、+特殊符号各四位
for (int i = 0; i < length; i++) {
// 随机的范围
String f1 = f[secureRandom.nextInt(f.length)];
list.add(i+n,f1);
}
}
}
/**
* 无序
* @param list
* @param secureRandom
* @param sb
* @return
*/
private static StringBuffer disorderList(List<String> list, SecureRandom secureRandom, StringBuffer sb) {
int size = list.size();
for (int i = 0; i < size; i++) {
int index = secureRandom.nextInt(list.size());
String str = list.get(index);
sb.append(str);
list.remove(index);
}
return sb;
}
}