zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java随机获取32位密码且必须包含大小写字母、数字和特殊字符,四种的任意三种详解编程语言

JAVA密码密码编程语言 详解 获取 数字 三种
2023-06-13 09:11:43 时间

Java随机获取32位密码且必须包含大小写字母、数字和特殊字符,四种的任意三种

Java随机获取32位密码且必须包含大小写字母、数字和特殊字符,四种的任意三种,代码如下:

import java.util.Random; 

public class GetRandomPwd{ 

 /** 

 * @Title: getRandomPwd 

 * @Description:获取制定长度的密码,包含大小写字母、数字和特殊字符,四种的任意三种 

 * @param len 

 * @return String 

 * @throws 

 public static String getRandomPwd(int len) { 

 String result = null; 

 while(len==32){ 

 result = makeRandomPwd(len); 

 if (result.matches(".*[a-z]{1,}.*") result.matches(".*[A-Z]{1,}.*") result.matches(".*//d{1,}.*") result.matches(".*[~; @#: %^]{1,}.*")) { 

 return result; 

 result = makeRandomPwd(len); 

 return "长度不得少于32位!"; 

 /** 

 * @Title: makeRandomPwd 

 * @Description:随机密码生成 

 * @param len 

 * @return String 

 * @throws 

 public static String makeRandomPwd(int len) { 

 char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~; @#: %^".toCharArray(); 

 StringBuilder sb = new StringBuilder(); 

 Random r = new Random(); 

 for (int x = 0; x len; ++x) { 

 sb.append(charr[r.nextInt(charr.length)]); 

 return sb.toString(); 


 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/16848.html

cjava