【问题标题】:Need to improve logic of password system需要改进密码系统的逻辑
【发布时间】:2016-06-30 17:34:28
【问题描述】:

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

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

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

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

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 语句

【问题讨论】:

  • "password can only be [...] 12 characters long" - "Alpha1234 would be STRONG" - 嗯...真的吗?
  • 为什么不在每个按键的每个按键事件上引发一个标志。假设您可以举起 3 个不同的旗帜。 1 标志升起 = 弱,2 标志中等,3 标志强。
  • 我建议调查“密码熵” - 你的分类远非正常。此外,切勿将密码限制在这么小的数字。
  • 您可以考虑使用库,因为这样会更强大:),请参阅 this

标签: java


【解决方案1】:

如果您的密码是“强”,那么每个检查都将返回 true,所以当然每个 if 语句都会通过。您要做的是将密码分类为“弱”,如果它只通过一项检查,“中等”如果它通过两项检查,“强”如果它通过所有三个检查。一种方法是从 int 设置为 0 开始,并在每次通过检查时递增它,然后在 ifs 中使用该最终数字。

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);
}

【讨论】:

    【解决方案2】:

    只需将最后 2 个 if 语句替换为 else。还要重新排序您的语句,以便首先处理强密码。

    【讨论】:

      【解决方案3】:

      您的强度检查逻辑不正确。如果给定的密码包含大写、小写和数字,则所有 3 个if 语句都为真。你没有使用if-else 语句,所以它们都会执行。

      我还建议您查看https://codereview.stackexchange.com/;他们可以帮助清理您的代码。

      【讨论】:

        【解决方案4】:
        public static void main(String[] args) {
            String password = "A";
            String passwordStrength = "";
            if(password.length() >= 6 && password.length() <= 12)
                passwordStrength= passStrength(password);
        
        }
        
        public static String passStrength(String password) {
            String[] desc = new String[3];
            desc[0] = "Weak";
            desc[1] = "Medium";
            desc[2] = "Strong";
        
        
        
            int score   = -1;
        
        
                    String pattern = "(.*)(\\d+)(.*)";
                    Pattern r = Pattern.compile(pattern);
                    Matcher m = r.matcher(password);
                    if(m.find( )) score++;
        
                    pattern = "(.*)([a-z]+)(.*)";
                    r = Pattern.compile(pattern);
                    m = r.matcher(password);
                    if(m.find( )) score++;
        
                    pattern = "(.*)([A-Z]+)(.*)";
                    r = Pattern.compile(pattern);
                    m = r.matcher(password);
                    if(m.find( )) score++;
        
                    return desc[score];
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多