[英]Password strength validation java
I need to write a program to check the strength of a password based on following conditions and output the type of password as "Too short", "Weak", "Medium" or "Strong" 我需要编写一个程序来根据以下条件检查密码的强度,并将密码的类型输出为“太短”,“弱”,“中等”或“强”
Conditions: 1) Password should be 8 characters long 2) it should contain at least one uppercase and one lowercase letter 3) it should contain one or more of these special characters: ~, !, @, #, $, %, ^, &, * 4) it should contain one or more digits in it The output is defined as: if first condition fails, then output = "Too short" if only two conditions including first one 条件:1)密码应为8个字符长2)密码应至少包含一个大写字母和一个小写字母3)密码应包含一个或多个以下特殊字符:〜,!,@,#,$,%,^, &,* 4)它应包含一个或多个数字。输出定义为:如果第一个条件失败,则如果只有两个条件(包括第一个条件),则输出=“ Too short”
is met, then output = "Weak" if only three conditions including first one is met, then output = "Medium" if all four conditions are met, then output = "Strong" Below is the code I have written: 满足,如果只有三个条件(包括第一个条件)被满足,则输出=“弱”,如果所有四个条件都满足,则输出=“中等”,然后输出=“强”下面是我编写的代码:
class PasswordStrengthValidation
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Enter the password");
String inputPassword = scan.next();
int uppercase = 0;
int lowercase = 0;
int specialcharacters = 0;
int digits = 0;
char[] Password = inputPassword.toCharArray();
for (int index = 0; index < inputPassword.length(); index++)
{
if (Character.isUpperCase(Password[index]))
{
uppercase = 1;
}
if (Character.isLowerCase(Password[index]))
{
lowercase = 1;
}
if (Character.isDigit(Password[index]))
{
digits = 1;
}
}
if (inputPassword.contains("~") || inputPassword.contains("!") || inputPassword.contains("@")
|| inputPassword.contains("#") || inputPassword.contains("$") || inputPassword.contains("%")
|| inputPassword.contains("^") || inputPassword.contains("&") || inputPassword.contains("*")) ;
{
specialcharacters = 1;
}
if (inputPassword.length() < 8)
System.out.println("Too Short");
if (inputPassword.length() >= 8 && (((uppercase == 1) && (lowercase == 1)) || (digits == 1) || (specialcharacters == 1)))
System.out.println("Weak");
if ((inputPassword.length() >= 8 && (((uppercase == 1) && (lowercase == 1)) || (digits == 1) && (specialcharacters == 1)))
&&
(inputPassword.length() >= 8 && (((uppercase == 1) && (lowercase == 1)) && (digits == 1) || (specialcharacters == 1))))
System.out.println("Medium");
if (inputPassword.length() >= 8 && (uppercase == 1) && (lowercase == 1) && (digits == 1) && (specialcharacters == 1))
System.out.println("Strong");
}
}
However when I try to implement the below code by giving an input of "Password" which falls under the "Weak" category I get the output as Weak followed by Medium. 但是,当我尝试通过提供属于“弱”类别的“密码”输入来实现以下代码时,输出为“弱”,后跟“中等”。 Can someone please help me as to where am I going wrong.
有人可以帮我解决我要去哪里吗。 Thanks in advance!
提前致谢!
I suggest use a regex 我建议使用正则表达式
if (inputPassword.matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})")){
System.out.println("Strong");
} else if (inputPassword.matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})")){
System.out.println("Medium");
} else if (inputPassword.matches("^(?=.*[a-z])(?=.*[0-9])(?=.{8,})")){
System.out.println("Weak");
} else if (inputPassword.matches("^(?=.*[A-Z])(?=.*[0-9])(?=.{8,})")){
System.out.println("Weak");
} // etc
you can adjust conditions removing or change conditions in the expression 您可以调整条件以删除或更改表达式中的条件
(?=.*[az]) The string must contain at least 1 lowercase alphabetical character (?=。* [az])字符串必须包含至少1个小写字母字符
(?=.*[AZ]) The string must contain at least 1 uppercase (?=。* [AZ])字符串必须包含至少1个大写字母
alphabetical character 字母字符
(?=.*[0-9]) The string must contain at least 1 numeric character (?=。* [0-9])字符串必须包含至少1个数字字符
(?=.[!@#\\$%\\^&]) The string must contain at least one special character, but we are escaping reserved RegEx characters to avoid conflict (?=。[!@#\\ $%\\ ^&])字符串必须至少包含一个特殊字符,但是我们转义了保留的RegEx字符以避免冲突
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.