当前位置:   article > 正文

Java密码随机生成工具_java 密码生成 工具包

java 密码生成 工具包

 这是一个随机生成,自定义长度,自定义内容,自定义组合的Java密码随机生成工具!

  1. import java.security.SecureRandom;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /**
  5. * @Description TODO 密码随机生成工具
  6. * @Classname pw
  7. * @Author qingjian.kong
  8. * @Date 2023/4/12 16:45
  9. * @Version V1.0
  10. */
  11. public class PwUtil {
  12. //随机长度
  13. private static final int l = 16;
  14. //是否需要符号
  15. private static final String Y = "y";
  16. private static final String N = "n";
  17. public static void main(String[] args) {
  18. SecureRandom secureRandom = new SecureRandom();
  19. List<String> list = new ArrayList<String>();
  20. StringBuffer sb = new StringBuffer();
  21. // 循环下找个看的顺眼的
  22. for (int i = 0; i < 50; i++) {
  23. orderlyList(list,secureRandom,N,l);
  24. System.out.println(disorderList(list,secureRandom,sb));
  25. list.clear();
  26. sb.setLength(0);
  27. }
  28. }
  29. /**
  30. * 有序: 数字+小写字母+大写字母+特殊符号
  31. * @param list
  32. * @param secureRandom
  33. */
  34. private static void orderlyList(List<String> list, SecureRandom secureRandom,String flag,int length) {
  35. // String[] a = {"0","1","2","3","4","5","6","7","8","9"};
  36. // 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"};
  37. // 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"};
  38. // String[] d = {"~","!","@","#","$","^","&","*","_","+","\\","/","`","|",".","=","-",",","?",":","'","<",">","(",")","%",";","\""};
  39. 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"};
  40. 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","~","!","@","#","$","^","&","*","_","+","\\","/","`","|",".","=","-",",","?",":","'","<",">","(",")","%",";","\""};
  41. int n = 0;
  42. if (flag.equals("y")){
  43. // 数字、小写字母、+大写字母、+特殊符号各四位
  44. for (int i = 0; i < length; i++) {
  45. // // 随机的范围
  46. // String a1 = a[secureRandom.nextInt(a.length)];
  47. // String b1 = b[secureRandom.nextInt(b.length)];
  48. // String c1 = c[secureRandom.nextInt(c.length)];
  49. // String d1 = d[secureRandom.nextInt(d.length)];
  50. // list.add(i+n,a1);
  51. // n++;
  52. // list.add(i+n,b1);
  53. // n++;
  54. // list.add(i+n,c1);
  55. // n++;
  56. // list.add(i+n,d1);
  57. // 随机的范围
  58. String g1 = g[secureRandom.nextInt(g.length)];
  59. list.add(i+n,g1);
  60. }
  61. }else {
  62. // 数字、小写字母、+大写字母、+特殊符号各四位
  63. for (int i = 0; i < length; i++) {
  64. // 随机的范围
  65. String f1 = f[secureRandom.nextInt(f.length)];
  66. list.add(i+n,f1);
  67. }
  68. }
  69. }
  70. /**
  71. * 无序
  72. * @param list
  73. * @param secureRandom
  74. * @param sb
  75. * @return
  76. */
  77. private static StringBuffer disorderList(List<String> list, SecureRandom secureRandom, StringBuffer sb) {
  78. int size = list.size();
  79. for (int i = 0; i < size; i++) {
  80. int index = secureRandom.nextInt(list.size());
  81. String str = list.get(index);
  82. sb.append(str);
  83. list.remove(index);
  84. }
  85. return sb;
  86. }
  87. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/897111
推荐阅读
相关标签
  

闽ICP备14008679号