Java Program to Check Password Strength

This article covers one of the important program in Java, that is to check whether the strength of a password is strong or not. This is an important topic, because in many applications, we need to allow user to SignUp.

To SignUp, user need to create a password along with other details. Therefore while creating password, it is developer's job to create some code that checks whether the entered password is strong or not. Therefore I've created the program in Java that helps in checking the strength of a password created by user.

Check Strength of Password in Java

The question is, write a Java program to check whether the password is valid or not. If it is valid, then whether its strength is strong or not. The password must be received by user at run-time of the program. The program given below is the answer to this question:

In this program, the password is categorized into three categories, invalid, weak, and strong. The password becomes invalid, if the length of password is less than 8 characters. The password comes under weak category, if it does not contains at least one uppercase character, one lowercase character, one digit, and a special character. And the password comes under strong category, if it contains all these characters.

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      int passwordLength=8, upChars=0, lowChars=0;
      int special=0, digits=0;
      char ch;
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter the Password: ");
      String password = s.nextLine();
      
      int total = password.length();
      if(total<passwordLength)
      {
         System.out.println("\nThe Password is invalid!");
         return;
      }
      else
      {
         for(int i=0; i<total; i++)
         {
            ch = password.charAt(i);
            if(Character.isUpperCase(ch))
               upChars = 1;
            else if(Character.isLowerCase(ch))
               lowChars = 1;
            else if(Character.isDigit(ch))
               digits = 1;
            else
               special = 1;
         }
      }
      if(upChars==1 && lowChars==1 && digits==1 && special==1)
         System.out.println("\nThe Password is Strong.");
      else
         System.out.println("\nThe Password is Weak.");
   }
}

The snapshot given below shows the sample run produced by above Java program, with user input fresherearth@123 as password to check its strength:

java check password strength

The above program is the basic version of checking password strength. Because the program does not provides good user experience, therefore we need to modify the above program. Now-a-days, I don't think, that 8 characters long password is safe anymore. Yes it is safe, if you use the combination of characters very smartly, but try to create always at least 12 characters long password. Therefore I've categorized 8-12 character long password as medium strength, whereas more than 12 characters long password as strong strength. You can modify according to your need.

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      int passwordLength=8, upChars=0, lowChars=0;
      int special=0, digits=0;
      char ch;
      Scanner s = new Scanner(System.in);
      
      System.out.println("----Direction to Create a Password----");
      System.out.println("1. The Password must be at least 8 characters long.");
      System.out.println("2. The Password must contain at least one uppercase character.");
      System.out.println("3. The Password must contain at least one lowercase character.");
      System.out.println("4. The Password must contain at least one digit (0-9).");
      System.out.println("5. The Password must contain at least one special characters.");
      System.out.println("6. The Password must not contain < or >.");
      
      System.out.print("\nCreate a Password: ");
      String password = s.nextLine();
      
      int total = password.length();
      if(total<passwordLength)
      {
         System.out.println("\nThe Password's Length has to be of 8 characters or more.");
         return;
      }
      else
      {
         for(int i=0; i<total; i++)
         {
            ch = password.charAt(i);
            if(Character.isUpperCase(ch))
               upChars++;
            else if(Character.isLowerCase(ch))
               lowChars++;
            else if(Character.isDigit(ch))
               digits++;
            else
            {
               if(ch=='<' || ch=='>')
               {
                  System.out.println("\nThe Password is Malicious!");
                  return;
               }
               else
                  special++;
            }
         }
      }
      if(upChars!=0 && lowChars!=0 && digits!=0 && special!=0)
      {
         if(total>=12)
         {
            System.out.println("\nThe Strength of Password is Strong.");
         }
         else
         {
            System.out.println("\nThe Strength of Password is Medium.");
         }
         System.out.println("\n----The Password Contains----");
         System.out.println("UpperCase Character: " +upChars);
         System.out.println("LowerCase Character: " +lowChars);
         System.out.println("Digit: " +digits);
         System.out.println("Special Character: " +special);
      }
      else
      {
         if(upChars==0)
            System.out.println("\nThe Password must contain at least one uppercase character.");
         if(lowChars==0)
            System.out.println("\nThe Password must contain at least one lowercase character.");
         if(digits==0)
            System.out.println("\nThe Password must contain at least one digit.");
         if(special==0)
            System.out.println("\nThe Password must contain at least one special character.");
      }
   }
}

Here is its sample run with user input Java@12 as password to check its strength:

java program check password strength

Here is another sample run with user input Java@123:

check password strength java

The snapshot given below shows another sample run with user input Ja12@va*oF.all.Lang:

java check strength of password example

The code given above on checking the strength of entered password in Java, is just a demo version, shows how the job can be done. But you can modify according to your need and use the code in your Java application where user need to create an account.

Java Online Test


« Previous Program Next Program »