Java随机生成长度不少于6的指定长度的密码,且这个字符串必须包含大小写字母、数字和特殊字符,四种的任意三种
2016-08-22 01:36
1341 查看
Java随机生成长度不少于6的指定长度的密码,且这个字符串必须包含大小写字母、数字和特殊字符,四种的任意三种
入参为指定的长度,出参为随机生成的密码
JAVA代码 RandomPassword.java
import java.util.Random;
public class RandomPassword {
public static void main(String[] args) {
String password = getRandomPassword(8);
System.out.println(password);
}
//获取验证过的随机密码
public static String getRandomPassword(int len) {
String result = null;
/*if(len >= 6) {
for(result = makeRandomPassword(len);len >= 6;result = makeRandomPassword(len)){
if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*\\d{1,}.*") && result.matches(".*[~!@#$%^&*\\.?]{1,}.*")) {
return result;
}
}
}*/
while(len>=6){
result = makeRandomPassword(len);
if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*\\d{1,}.*") && result.matches(".*[~!@#$%^&*\\.?]{1,}.*")) {
return result;
}
result = makeRandomPassword(len);
}
return "长度不得少于6位!";
}
//随机密码生成
public static String makeRandomPassword(int len){
char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*.?".toCharArray();
//System.out.println("字符数组长度:" + charr.length); //可以看到调用此方法多少次
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();
}
}
入参为指定的长度,出参为随机生成的密码
JAVA代码 RandomPassword.java
import java.util.Random;
public class RandomPassword {
public static void main(String[] args) {
String password = getRandomPassword(8);
System.out.println(password);
}
//获取验证过的随机密码
public static String getRandomPassword(int len) {
String result = null;
/*if(len >= 6) {
for(result = makeRandomPassword(len);len >= 6;result = makeRandomPassword(len)){
if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*\\d{1,}.*") && result.matches(".*[~!@#$%^&*\\.?]{1,}.*")) {
return result;
}
}
}*/
while(len>=6){
result = makeRandomPassword(len);
if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*\\d{1,}.*") && result.matches(".*[~!@#$%^&*\\.?]{1,}.*")) {
return result;
}
result = makeRandomPassword(len);
}
return "长度不得少于6位!";
}
//随机密码生成
public static String makeRandomPassword(int len){
char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*.?".toCharArray();
//System.out.println("字符数组长度:" + charr.length); //可以看到调用此方法多少次
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();
}
}
相关文章推荐
- Java随机密码生成器,Java随机生成长度不少于6的指定长度的密码,且这个字符串必须包含大小写字母、数字和特殊字符,四种的任意三种
- Java指定长度随机生成的字符串,且这个字符串必须包含大小写字母和数字
- Java指定长度随机生成的字符串,且这个字符串必须包含大小写字母和数字
- Java生成指定长度并且包含大小写字母数字字符串示例
- 生成一个任意长度的随机字符串,包含数字和字母[C语言]
- JAVA随机生成指定长度的密码(大小写和数字)
- Java生成随机密码(包含大小写字母和数字)
- C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密
- 设计一个函数,根据指定长度生成对应的验证码(由数字和大小写英文字母构成的随机字符串)
- Java 按指定概率生成由字母、数字、特殊符号组成的密码
- Java生成随机字符串,必须包含数字、小写字母、大写字母
- php随机生成指定长度的字符串 可以固定数字 字母 混合
- java得到指定位数随机密码(由数字,区分大小写的字母组成)
- iOS 设置密码8-16位,必须包含字母和数字(可包含其他特殊字符)
- JS 验证密码 不能为空,必须含有数字、字母、特殊字符,长度在8-12位
- Java生成随机字符串,必须包含数字、小写字母、大写字母
- 随机产生由特殊字符,大小写字母以及数字组成的字符串,且每种字符都至少出现一次
- 密码同时包含6~18位数字和大小写字母,不包含特殊字符的判断方法(正则表达式)
- 密码同时包含8~20位数字和大小写字母,不包含特殊字符的判断方法(正则表达式)
- 随机生成指定长度的数字+字符的密码