forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordStrengthAnalyzer.java
More file actions
245 lines (208 loc) · 8.87 KB
/
PasswordStrengthAnalyzer.java
File metadata and controls
245 lines (208 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import java.util.Scanner;
import java.util.regex.Pattern;
/**
* Password Strength Analyzer
* A program that analyzes password strength and provides recommendations
*
* Features:
* - Checks password length, complexity, and common patterns
* - Provides strength score and recommendations
* - Interactive CLI interface
*
* @author Pranav Verma
* @version 1.0
*/
public class PasswordStrengthAnalyzer {
private static final String[] COMMON_PASSWORDS = {
"password", "123456", "password123", "admin", "qwerty",
"letmein", "welcome", "monkey", "1234567890", "abc123"
};
private static final String[] WEAK_PATTERNS = {
"1234", "abcd", "qwer", "asdf", "zxcv"
};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PasswordStrengthAnalyzer analyzer = new PasswordStrengthAnalyzer();
System.out.println("🔐 PASSWORD STRENGTH ANALYZER 🔐");
System.out.println("=====================================");
while (true) {
System.out.print("\nEnter a password to analyze (or 'quit' to exit): ");
String password = scanner.nextLine();
if (password.equalsIgnoreCase("quit")) {
System.out.println("Thank you for using Password Strength Analyzer!");
break;
}
PasswordResult result = analyzer.analyzePassword(password);
analyzer.displayResult(result);
}
scanner.close();
}
public PasswordResult analyzePassword(String password) {
PasswordResult result = new PasswordResult(password);
// Check various criteria
result.hasMinLength = password.length() >= 8;
result.hasUppercase = Pattern.compile("[A-Z]").matcher(password).find();
result.hasLowercase = Pattern.compile("[a-z]").matcher(password).find();
result.hasNumbers = Pattern.compile("[0-9]").matcher(password).find();
result.hasSpecialChars = Pattern.compile("[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]").matcher(password).find();
result.isCommonPassword = isCommonPassword(password);
result.hasWeakPattern = hasWeakPattern(password);
result.hasRepeatingChars = hasRepeatingCharacters(password);
// Calculate strength score
result.strengthScore = calculateStrengthScore(result);
result.strengthLevel = getStrengthLevel(result.strengthScore);
// Generate recommendations
result.recommendations = generateRecommendations(result);
return result;
}
private boolean isCommonPassword(String password) {
String lowerPassword = password.toLowerCase();
for (String common : COMMON_PASSWORDS) {
if (lowerPassword.equals(common) || lowerPassword.contains(common)) {
return true;
}
}
return false;
}
private boolean hasWeakPattern(String password) {
String lowerPassword = password.toLowerCase();
for (String pattern : WEAK_PATTERNS) {
if (lowerPassword.contains(pattern)) {
return true;
}
}
return false;
}
private boolean hasRepeatingCharacters(String password) {
for (int i = 0; i < password.length() - 2; i++) {
if (password.charAt(i) == password.charAt(i + 1) &&
password.charAt(i + 1) == password.charAt(i + 2)) {
return true;
}
}
return false;
}
private int calculateStrengthScore(PasswordResult result) {
int score = 0;
// Length scoring
if (result.password.length() >= 12) score += 25;
else if (result.password.length() >= 8) score += 15;
else score += 5;
// Character variety scoring
if (result.hasUppercase) score += 15;
if (result.hasLowercase) score += 15;
if (result.hasNumbers) score += 15;
if (result.hasSpecialChars) score += 20;
// Penalty for weak characteristics
if (result.isCommonPassword) score -= 30;
if (result.hasWeakPattern) score -= 15;
if (result.hasRepeatingChars) score -= 10;
return Math.max(0, Math.min(100, score));
}
private String getStrengthLevel(int score) {
if (score >= 80) return "VERY STRONG";
else if (score >= 60) return "STRONG";
else if (score >= 40) return "MODERATE";
else if (score >= 20) return "WEAK";
else return "VERY WEAK";
}
private String generateRecommendations(PasswordResult result) {
StringBuilder recommendations = new StringBuilder();
if (!result.hasMinLength) {
recommendations.append("• Use at least 8 characters (12+ recommended)\n");
}
if (!result.hasUppercase) {
recommendations.append("• Add uppercase letters (A-Z)\n");
}
if (!result.hasLowercase) {
recommendations.append("• Add lowercase letters (a-z)\n");
}
if (!result.hasNumbers) {
recommendations.append("• Add numbers (0-9)\n");
}
if (!result.hasSpecialChars) {
recommendations.append("• Add special characters (!@#$%^&*)\n");
}
if (result.isCommonPassword) {
recommendations.append("• Avoid common passwords and dictionary words\n");
}
if (result.hasWeakPattern) {
recommendations.append("• Avoid sequential patterns (1234, abcd, qwerty)\n");
}
if (result.hasRepeatingChars) {
recommendations.append("• Avoid repeating characters (aaa, 111)\n");
}
if (recommendations.length() == 0) {
recommendations.append("• Your password looks great! Consider using a password manager.");
}
return recommendations.toString();
}
private void displayResult(PasswordResult result) {
System.out.println("\n📊 ANALYSIS RESULTS");
System.out.println("==================");
System.out.println("Password: " + maskPassword(result.password));
System.out.println("Length: " + result.password.length() + " characters");
System.out.println();
// Strength indicator
System.out.printf("Strength Score: %d/100 [%s]\n", result.strengthScore, result.strengthLevel);
printStrengthBar(result.strengthScore);
System.out.println();
// Criteria checklist
System.out.println("✅ CRITERIA CHECKLIST:");
System.out.println(getCheckmark(result.hasMinLength) + " Minimum 8 characters");
System.out.println(getCheckmark(result.hasUppercase) + " Contains uppercase letters");
System.out.println(getCheckmark(result.hasLowercase) + " Contains lowercase letters");
System.out.println(getCheckmark(result.hasNumbers) + " Contains numbers");
System.out.println(getCheckmark(result.hasSpecialChars) + " Contains special characters");
System.out.println(getCheckmark(!result.isCommonPassword) + " Not a common password");
System.out.println(getCheckmark(!result.hasWeakPattern) + " No weak patterns");
System.out.println(getCheckmark(!result.hasRepeatingChars) + " No repeating characters");
System.out.println();
// Recommendations
System.out.println("💡 RECOMMENDATIONS:");
System.out.println(result.recommendations);
}
private String maskPassword(String password) {
if (password.length() <= 2) return "**";
return password.charAt(0) + "*".repeat(password.length() - 2) + password.charAt(password.length() - 1);
}
private String getCheckmark(boolean condition) {
return condition ? "✅" : "❌";
}
private void printStrengthBar(int score) {
int barLength = 20;
int filled = (score * barLength) / 100;
System.out.print("[");
for (int i = 0; i < barLength; i++) {
if (i < filled) {
if (score >= 80) System.out.print("█");
else if (score >= 60) System.out.print("▓");
else if (score >= 40) System.out.print("▒");
else System.out.print("░");
} else {
System.out.print(" ");
}
}
System.out.println("]");
}
}
/**
* Data class to hold password analysis results
*/
class PasswordResult {
String password;
boolean hasMinLength;
boolean hasUppercase;
boolean hasLowercase;
boolean hasNumbers;
boolean hasSpecialChars;
boolean isCommonPassword;
boolean hasWeakPattern;
boolean hasRepeatingChars;
int strengthScore;
String strengthLevel;
String recommendations;
public PasswordResult(String password) {
this.password = password;
}
}