Java program to check password strength

This article covers one of the most important programs in Java, which 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 users to sign up.

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

Check the Strength of the 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 the user at the runtime 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 the password is less than 8 characters. The password falls under the weak category if it does not contain at least one uppercase character, one lowercase character, one digit, and a special character. And the password comes under the strong category if it contains all these characters.

import java.util.Scanner;

public class CodesCracker
{
   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 the above Java program, with user input CodesCracker@123 as the 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 provide a good user experience, we need to modify the above program. Today, I don't think that an 8-character password is safe anymore. Yes, it is safe if you use the combination of characters very smartly, but try to always create at least a 12-character password. Therefore, I've categorized 8–12 character long passwords as medium strength, whereas more than 12 characters long passwords are considered strong strength. You can modify according to your needs.

import java.util.Scanner;

public class CodesCracker
{
   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 a 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 for checking the strength of an entered password in Java is just a demo version and shows how the job can be done. But you can modify the code according to your needs and use it in your Java application where users need to create an account.

Java Online Test


« Previous Program Next Program »


Liked this post? Share it!