EXPERIMENT NO:- 01
NAME:- Hansal
Bhangale
CLASS:- TY-03
BATCH:- A
ROLL NO: 07
AIM:-Implement a password strength checker that evaluates the strength of a given password
based on several security criteria.
PROGRAM:-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;
public class PasswordStrengthChecker {
public static void main(String[] args) {
// Create the main frame for the GUI
JFrame frame = new JFrame("Password Strength Checker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// Create the panel to hold components
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 10, 10));
// Create the labels and text fields
JLabel passwordLabel = new JLabel("Enter Password:");
JPasswordField passwordField = new JPasswordField();
JLabel strengthLabel = new JLabel("Password Strength:");
// Create a label to display password strength
JLabel resultLabel = new JLabel(" ");
resultLabel.setFont(new Font("Arial", Font.BOLD, 14));
// Add components to the panel
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(strengthLabel);
panel.add(resultLabel);
// Add panel to the frame
frame.add(panel);
// Create the "Check Strength" button
JButton checkButton = new JButton("Check Strength");
panel.add(checkButton);
// Add action listener to the button to check password strength
checkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get the entered password
String password = new String(passwordField.getPassword());
// Check password strength
String strength = checkPasswordStrength(password);
// Update the result label with the password strength
resultLabel.setText(strength);
updateStrengthLabel(strength, resultLabel);
}
});
// Set the frame visible
frame.setVisible(true);
}
// Method to check password strength
public static String checkPasswordStrength(String password) {
if (password.length() < 8) {
return "Weak: Too short (min 8 characters)";
}
boolean hasUpperCase = false;
boolean hasLowerCase = false;
boolean hasDigit = false;
boolean hasSpecialChar = false;
// Check each character of the password
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (Character.isUpperCase(c)) {
hasUpperCase = true;
} else if (Character.isLowerCase(c)) {
hasLowerCase = true;
} else if (Character.isDigit(c)) {
hasDigit = true;
} else if (!Character.isLetterOrDigit(c)) {
hasSpecialChar = true;
}
}
// Evaluate password strength based on criteria
if (hasUpperCase && hasLowerCase && hasDigit && hasSpecialChar) {
return "Strong";
} else if ((hasUpperCase || hasLowerCase) && hasDigit) {
return "Medium";
} else {
return "Weak: Include uppercase, lowercase, digit, and special
character.";
}
}
// Method to update strength label based on result
private static void updateStrengthLabel(String strength, JLabel label)
{
if (strength.equals("Strong")) {
label.setForeground(Color.GREEN);
} else if (strength.equals("Medium")) {
label.setForeground(Color.ORANGE);
} else {
label.setForeground(Color.RED);
}
}
}
OUTPUT:-
● GUI of Password Strength Checker
● Password Strength Checker indicating Password Strength as “Strong”
● Password Strength Checker indicating Password Strength as “Medium”
● Password Strength Checker indicating Password Strength as “Weak” and giving
a warning of including “Uppercase”
● Password Strength Checker indicating Password Strength as “Weak” and giving
a warning of password being “Too Short”
Review Questions:
Conclusion:In conclusion, we have successfully implemented a password strength checker to
help users create strong and secure passwords. By evaluating passwords based on criteria such
as length, complexity, the inclusion of uppercase and lowercase letters, numbers, special
characters, and the avoidance of common words or sequences, the checker can effectively
assess password strength.