如何创建一个 Java 程序来检查您的密码是否强?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37454110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:31:09  来源:igfitidea点击:

How to create a Java program to check if your password is strong or not?

javapasswords

提问by shreydan

I want the program to do 2 things:

我希望程序做两件事:

  1. Check if the string(password input) contains both letters and numbers.
  2. Check if the password has atleast 8 characters.
  1. 检查字符串(密码输入)是否同时包含字母和数字。
  2. 检查密码是否至少有 8 个字符。

Here is my code:

这是我的代码:

import java.util.*;
class howstrong
{
  public static void main(String ar[])
  {
    String password;
    int i;
    int c=0;
    Scanner in = new Scanner(System.in);
    System.out.println("ENTER PASSWORD");
    password = in.next();
    if (password.length() <= 8)
    {
      for (i = 0; i <= password.length(); i++)
      {
        char x = password.charAt(i);
        if (Character.isLetter(x))
        {
          if (Character.isDigit(x))
          c = 1;
        }
      }
      if (c == 1)
        System.out.println("STRONG");
      else 
        System.out.println("NOT STRONG");
    }
    else
      System.out.println("HAVE AT LEAST 8 CHARACTERS");
  }
}

回答by Arnaud

You have several issues :

你有几个问题:

i <= password.length()should be i < password.length()

i <= password.length()应该 i < password.length()

if (password.length() <= 8)should be if (password.length() >= 8)

if (password.length() <= 8)应该 if (password.length() >= 8)

You are checking if the character is a letter and a digit at the same time.

您正在检查字符是否同时是字母和数字。

Finally, I'd suggest to use two flags, one to detect if there is a letter, another one to detect if there is a digit .

最后,我建议使用两个标志,一个用于检测是否有字母,另一个用于检测是否有 digit 。

Putting it all together :

把它们放在一起:

import java.util.Scanner;

class howstrong {
    public static void main(final String ar[]) {
        String password;
        Scanner in = new Scanner(System.in);
        System.out.println("ENTER PASSWORD");
        password = in.next();

        boolean hasLetter = false;
        boolean hasDigit = false;

        if (password.length() >= 8) {
            for (int i = 0; i < password.length(); i++) {
                char x = password.charAt(i);
                if (Character.isLetter(x)) {

                    hasLetter = true;
                }

                else if (Character.isDigit(x)) {

                    hasDigit = true;
                }

                // no need to check further, break the loop
                if(hasLetter && hasDigit){

                    break;
                }

            }
            if (hasLetter && hasDigit) {
                System.out.println("STRONG");
            } else {
                System.out.println("NOT STRONG");
            }
        } else {
            System.out.println("HAVE AT LEAST 8 CHARACTERS");
        }
    }
}

回答by ishmaelMakitla

there is a Regex expression that you could use to check password strength. You could add a validation method that checks if the password matches the expression: So you call the method in your code like this:

您可以使用正则表达式来检查密码强度。您可以添加一个验证方法来检查密码是否与表达式匹配:因此您可以像这样在代码中调用该方法:

   password = in.next();

   if(isStrong(password){
      //good password, do stuff
    }
   else{
        //not so good, prompt again
   }

And the method looks like this:

该方法如下所示:

private boolean isStrong(String password){
    return password.matches("^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z])");

  }

Below is a regex which you can use to check password strength.

下面是一个正则表达式,您可以使用它来检查密码强度。

^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$

You can look at the original post and selected answer here.

您可以在此处查看原始帖子和选定的答案。

回答by Julien Lopez

In your loop, you check if your character is a letter anda digit. So cwill never be set to 1. I suggest you learn to use a debugger, it will help you solve this kind of bugs easily.

在你的循环中,你检查你的字符是否是一个字母一个数字。所以c永远不会设置为1。我建议你学会使用调试器,它会帮助你轻松解决此类错误。

回答by xcesco

I don't know, because it not specified, if your need it just for exercise or you need to implements in a real world application.

我不知道,因为它没有指定,如果你只是为了练习而需要它,或者你需要在现实世界的应用程序中实现。

In the second case, I suggest you to use an existing library to check if a password is strong enough.

在第二种情况下,我建议您使用现有的库来检查密码是否足够强。

Check zxcvbn Java version on github, that is a porting of a library provided by Dropbox.

检查github 上的 zxcvbn Java 版本,这是 Dropbox 提供的库的移植。