- Java Basic Programs
- Java Programming Examples
- Java Print Hello World
- Java Get Input from User
- Java Print Integer
- Java Add two Numbers
- Java Check Even or Odd
- Java Check Prime or Not
- Java Check Alphabet or Not
- Java Check Vowel or Not
- Check Reverse equal Original
- Java Fahrenheit to Celsius
- Java Celsius to Fahrenheit
- Java Perfect Number Program
- Java Find Quotient Remainder
- Java Days to Seconds
- Java Count Digits in Number
- Java Binary Number Addition
- Java Discount Program
- Java Compute Courier Charge
- Java Find Telephone Bill
- Java Print ASCII Values
- Java Check Palindrome or Not
- Java Check Armstrong or Not
- Generate Armstrong Numbers
- Add two Numbers using Pointers
- Java Mathematical Programs
- Add Subtract Multiply & Divide
- Java Make Calculator
- Java Add Digits of Number
- Java Check Leap Year or Not
- Java Check Divisibility
- Java Find Simple Interest
- Java Find Compound Interest
- Java Print Fibonacci Series
- Java Find nCr nPr
- Calculate Average & Percentage
- Java Calculate Arithmetic Mean
- Java Calculate Student Grade
- Java Print Table of Number
- Java Print Prime Numbers
- Java Add n Numbers
- Java Interchange two Numbers
- Java Reverse Numbers
- Java Swap two Numbers
- Count Positive Negative & Zero
- Find Largest of two Numbers
- Find Largest of three Numbers
- Java Find Factorial of Number
- Java Find HCF & LCM
- Area & Perimeter of Square
- Area & Perimeter of Rectangle
- Area & Circumference of Circle
- Java Conversion Programs
- Java Decimal to Binary
- Java Decimal to Octal
- Java Decimal to Hexadecimal
- Java Binary to Decimal
- Java Binary to Octal
- Java Binary to Hexadecimal
- Java Octal to Decimal
- Java Octal to Binary
- Java Octal to Hexadecimal
- Java Hexadecimal to Decimal
- Java Hexadecimal to Binary
- Java Hexadecimal to Octal
- Java Pattern Programs
- Java Pattern of Stars
- Java Pattern of Alphabets
- Java Pattern of Numbers
- Java Pyramid of Stars
- Java Pyramid of Alphabets
- Java Pyramid of Numbers
- Java Print Diamond Pattern
- Java Print Floyd Triangle
- Java Print Pascal Triangle
- Java Array Programs
- One Dimensional Array Program
- Java Linear Search
- Java Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Java Reverse Array
- Insert Element in Array
- Delete Element from Array
- Java Merge two Array
- Java Bubble Sort
- Java Selection Sort
- Java Insertion Sort
- Java Find Common Elements
- Java Count Even/Odd Number
- Two Dimensional Array Program
- Java Add two Matrices
- Java Subtract two Matrices
- Java Transpose Matrix
- Multiply two Matrices
- Three Dimension Array Program
- Java String Programs
- Java Print String
- Find Length of String
- Java Compare two String
- Java Copy String
- Java Concatenate String
- Java Reverse String
- Delete Vowels from String
- Delete Words from Sentence
- Find Occurrence of a Character
- Java Find Occurrence of a Word
- Occurrence of Each Character
- Java Occurrence of Each Word
- Java Count Repeated Characters
- Java Count Repeated Words
- Java Capitalize Each Word
- Java Count Vowels/Consonants
- Java Extract Numbers
- Java Count Word in String
- Remove Spaces from String
- Java Sort a String
- Java Uppercase to Lowercase
- Java Lowercase to Uppercase
- Java Swap two Strings
- Java Check Anagram or Not
- Java Check Balance Parentheses
- Java Check Password Strength
- Java File Programs
- Java Read File
- Java Write to File
- Read & Display File Content
- Java Copy File
- Java Append Text to File
- Java Merge two File
- List files in Directory
- Java Delete File
- Java Miscellaneous Programs
- Generate Random Numbers
- Java Print Time & Date
- Java Get IP Address
- Java Shutdown Computer
- Java Programming Tutorial
- Java Tutorial
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:
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:
Here is another sample run with user input Java@123:
The snapshot given below shows another sample run with user input Ja12@va*oF.all.Lang:
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.
« Previous Program Next Program »