gpt4 book ai didi

java - 需要改进密码系统的逻辑

转载 作者:行者123 更新时间:2023-12-02 03:30:49 25 4
gpt4 key购买 nike

所以我已经有了这个基本的密码系统,到目前为止,我已经整理出了我的长度检查器代码(在我的系统中,密码的长度只能在 6 到 12 个字符之间。)

但是,强度检查器更复杂,因为我想将密码分为三类:弱、强和中。类别由密码中的字符类型决定,因此“alpha”为“弱”,“Alpha”为“中”,“Alpha1234”为“强”。

如果密码强度为弱,我希望程序提示用户返回并输入另一个密码,如果强度为中,则我让用户选择输入另一个密码或保留他们输入的密码,强度强则自动保存密码

到目前为止,我已经编写了三个定义字符集的数组:

public static String[] uppercase = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

public static String[] lowercase = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

public static int[] numbers; {
numbers = new int[10];
numbers[0] = 0;
numbers[1] = 1;
numbers[2] = 2;
numbers[3] = 3;
numbers[4] = 4;
numbers[5] = 5;
numbers[6] = 6;
numbers[7] = 7;
numbers[8] = 8;
numbers[9] = 9;
}

以下是字符检查器方法:

public static boolean containsUppercase(String p) {
for (int i=0; i < p.length(); i++) {
for (int j = 0; j < uppercase.length; j++) {
if (Character.toString(p.charAt(i)).equals(uppercase[j])) {
return true;
}
}
}
return false;
}
public static boolean containsLowercase(String p) {
for (int i=0; i < p.length(); i++) {
for (int j = 0; j < lowercase.length; j++) {
if (Character.toString(p.charAt(i)).equals(lowercase[j])) {
return true;
}
}
}
return false;
}
public static boolean containsNumbers(String p) {
for (int i=0; i < p.length(); i++) {
for (int j = 0; j < numbers.length; j++) {
if (Character.toString(p.charAt(i)).equals(numbers[j])) {
return true;
}
}
}
return false;
}

这里是密码强度检查器:

if ((containsUppercase(password)) || (containsLowercase(password)) || (containsNumbers(password))) {
JOptionPane.showMessageDialog(null, "Your password strength is WEAK. You must enter another password");
passwordreenter = 0;
}
if ((containsUppercase(password) && (containsLowercase(password)) || (containsUppercase(password)) && (containsNumbers(password)) || (containsLowercase(password)) && (containsNumbers(password)))) {
passwordreenter = JOptionPane.showConfirmDialog(null, "Your password strength is MEDIUM. Would you like to enter another password anyway?");
System.out.println(passwordreenter);
}
if ((containsUppercase(password)) && (containsLowercase(password) && (containsNumbers(password)))) {
JOptionPane.showMessageDialog(null, "Your password strength is STRONG. The program will now close");
System.exit(0);
}

当我运行程序时,如何让它直接进入正确的密码强度,因为现在如果我输入强密码,它会遍历每个 if 语句

最佳答案

如果您的密码“强”,那么每次检查都会返回 true,因此当然每个 if 语句都会通过。您要做的是,如果密码仅通过一项检查,则将其分类为“弱”;如果通过两项检查,则将其分类为“中”;如果通过全部三项检查,则将密码分类为“强”。实现此目的的一种方法是从将 int 设置为 0 开始,并在每次通过检查时递增它,然后在 if 中使用该最终数字。

int level = 0;
if (containsUppercase(password)) {
level++;
}
if (containsLowercase(password)) {
level++;
}
if (containsNumbers(password)) {
level++;
}

if (level <= 1) {
JOptionPane.showMessageDialog(null, "Your password strength is WEAK. You must enter another password");
passwordreenter = 0;
} else if (level == 2) {
passwordreenter = JOptionPane.showConfirmDialog(null, "Your password strength is MEDIUM. Would you like to enter another password anyway?");
System.out.println(passwordreenter);
} else if (level == 3) {
JOptionPane.showMessageDialog(null, "Your password strength is STRONG. The program will now close");
System.exit(0);
}

关于java - 需要改进密码系统的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38129847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com