public static String builderVerificationCode(int codeLength){
String verificationCode = "";
Random random = new Random();
if(codeLength <= 0){
return verificationCode;
}
//生成6位验证码
for(int i = 0; i < codeLength; i++) {
//0为字符,1为数字
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
if( "char".equalsIgnoreCase(charOrNum) ) {
//大写或者小写
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
verificationCode += (char)(random.nextInt(26) + temp);
} else if( "num".equalsIgnoreCase(charOrNum) ) {//数字
verificationCode += String.valueOf(random.nextInt(10));
}
}
return verificationCode;
}
String verificationCode = "";
Random random = new Random();
if(codeLength <= 0){
return verificationCode;
}
//生成6位验证码
for(int i = 0; i < codeLength; i++) {
//0为字符,1为数字
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
if( "char".equalsIgnoreCase(charOrNum) ) {
//大写或者小写
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
verificationCode += (char)(random.nextInt(26) + temp);
} else if( "num".equalsIgnoreCase(charOrNum) ) {//数字
verificationCode += String.valueOf(random.nextInt(10));
}
}
return verificationCode;
}
本文详细阐述了一种用于生成指定长度的个性化验证码的算法,包括字符与数字的随机组合,确保了验证码的安全性和可读性。通过随机选择字符类型(大写、小写或数字),并使用Java的Random类生成随机数,该算法能够有效防止自动化破解。验证代码的生成过程简洁高效,适用于各类应用中对验证码的需求。
522

被折叠的 条评论
为什么被折叠?



