Reputation: 31
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
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!
Upvotes: 3
Views: 11353
Reputation: 781
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
(?=.*[a-z]) The string must contain at least 1 lowercase alphabetical character
(?=.*[A-Z]) The string must contain at least 1 uppercase
alphabetical character
(?=.*[0-9]) The string must contain at least 1 numeric character
(?=.[!@#\$%\^&]) The string must contain at least one special character, but we are escaping reserved RegEx characters to avoid conflict
Upvotes: 6