Check Password Strength in Java
Password strength checking is an essential concept in real-world applications like login systems, banking apps, and secure platforms.
A strong password reduces the risk of brute-force and dictionary attacks. In this program, we validate a password based on common security rules.
Password Strength Rules
A password is considered strong if it contains:
- At least 8 characters
- At least one uppercase letter (A–Z)
- At least one lowercase letter (a–z)
- At least one digit (0–9)
- At least one special character
Example 1
Input: Java@123
Output: Strong Password
Example 2
Input: java123
Output: Weak Password
Logic Explanation
- Check password length.
- Traverse each character in the password.
- Identify uppercase, lowercase, digit, and special characters.
- If all conditions are satisfied, password is strong.
Java Program to Check Password Strength
import java.util.Scanner;
public class PasswordStrengthChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter password: ");
String password = sc.nextLine();
boolean hasUpper = false;
boolean hasLower = false;
boolean hasDigit = false;
boolean hasSpecial = false;
if (password.length() < 8) {
System.out.println("Weak Password (Minimum 8 characters required)");
return;
}
for (int i = 0; i < password.length(); i++) {
char ch = password.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
hasUpper = true;
}
else if (ch >= 'a' && ch <= 'z') {
hasLower = true;
}
else if (ch >= '0' && ch <= '9') {
hasDigit = true;
}
else {
hasSpecial = true;
}
}
if (hasUpper && hasLower && hasDigit && hasSpecial) {
System.out.println("Strong Password");
} else {
System.out.println("Weak Password");
}
}
}
Sample Output
Enter password: Java@123
Strong Password
Enter password: hello123
Weak Password
Important Notes
- Spaces are treated as special characters.
- ASCII comparison avoids regex usage.
- Logic can be customized based on security needs.
Practice Challenges
- Classify password as Weak, Medium, or Strong.
- Disallow spaces in passwords.
- Check for repeated characters.
- Enforce at least one character from a custom symbol set.
Password validation is a real-world use case of string manipulation. Mastering this logic helps build secure Java applications.