如何掩盖扫描仪输入的文本? [重复](How to obscure Scanner input

2019-07-31 03:12发布

可能重复:
隐藏在命令行上输入

我正在做一个密码的安全检查程序,以及我的问题是在我的程序运行得很好奇怪。 我想知道是,是否有使进入到控制台的任何文本的方式出现,因为它会在密码字段。 即输入的词将显示为“****”之前,用户按下回车键。

我知道JFrameJPasswordField的方法,但我不认为仅仅使用时,可以帮助我Scanner

这里是我的代码:

import java.util.Scanner;

public class SecurityCheckerMain {

    static String enteredPassword;

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter your password: ");
        enteredPassword = input.nextLine();
        Checker tc = new Checker(enteredPassword);
        tc.checkSecurity();

    }

}

public class Checker {

    //number of chars : the Higher the Better("password must be greater than 8 chars"){done}
    //combination of uppercase and lowercase{done}
    //contains numbers{done}
    //non repeated characters (every char is different ascii char){done}
    //is not a consecutive password ie 123456789 or 987654321{done}
    //is not blank ("[space]"){done} 


    int pLength;
    final int MAX_STRENGTH = 10;
    int pStrength = 0;
    String pass;

    public Checker(String pwd){
        pass = pwd;
        pLength = pwd.length();

    }

    public void checkSecurity(){
        if(pass.isEmpty()){
            System.out.println("Password Field is Empty! Password is Very Insecure.");
        }
        if(pLength >= 8){
            pStrength++;
            if(pLength >= 12){
                pStrength++;
                if(pLength >= 16){
                    pStrength++;
                }
            }
        }
        if(hasUpperCase(pass) && hasLowerCase(pass)){
            pStrength+=2;
        }
        if(containsNumbers(pass)){
            pStrength+=2;
        }
        if(hasNoRepeats(pass)){
            pStrength+=2;
        }
        if(!containsConsecutiveNums(pass)){
            pStrength++;
        }

        System.out.println("Your password strength is rated at " + pStrength +"/" + MAX_STRENGTH);

    }


    //Component Methods

    public boolean hasUpperCase(String str){
        for(int i = 0; i<pLength; i++){
            if(Character.isUpperCase(str.charAt(i))){
                return true;
            }
        }
        return false;

    }

    public boolean hasLowerCase(String str){
        for(int i  = 0; i<pLength; i++){
            if(Character.isUpperCase(str.charAt(i))){
                return true;
            }
        }
        return false;
    }

    public boolean containsNumbers(String str){
        for(int i = 0; i<pLength; i++){
            if(Character.isDigit(str.charAt(i))){
                return true;
            }
        }
        return false;
    }

    public boolean hasNoRepeats(String str){
        for(int i = 0; i<pLength; i++)
            if(containsChar(str, str.charAt(i))){
                return false;
            }
        return true;
    }


    public boolean containsChar(String s, char search) {
        if (s.length() == 0)
            return false;
        else
            return s.charAt(0) == search || containsChar(s.substring(1), search);
    }

    public boolean containsConsecutiveNums(String str){
        for(int i = 0; i<pLength; i++){
            if(Character.isDigit(str.charAt(i))){
                if(str.charAt(i)-1 == str.charAt(i-1) || str.charAt(i)+1 == str.charAt(i+1)){
                    return true;
                }
            }
        }
        return false;
    }
}

Answer 1:

您可以使用Console.readPassword代替。

readPassword(String fmt, Object... args) 

提供一个格式化提示,然后从控制台读取密码或密码,禁用回显

public class SecurityCheckerMain {

    static String enteredPassword;

    public static void main(String[] args) {

        Console console = System.console();

        enteredPassword =
            new String(console.readPassword("Please enter your password: "));
        Checker tc = new Checker(enteredPassword);
        tc.checkSecurity();

    }


文章来源: How to obscure Scanner input text? [duplicate]