如何隐藏扫描仪输入文本? [英] How to obscure Scanner input text?

查看:115
本文介绍了如何隐藏扫描仪输入文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Possible Duplicate:
Hide input on command line

我正在制作一个密码安全检查程序,但我的问题很奇怪,因为我的程序运行得很好.我想知道的是,是否有任何办法可以使输入到控制台的文本看起来像在密码字段中那样.也就是说,在用户按回车键之前,输入的单词将显示为"****".

I'm making a password security checker program and my question is odd in that my program runs just fine. What I'm wondering is whether there is any way of making text entered to the console appear as it would in a password field. i.e the word entered will appear as "****" BEFORE the user presses the return key.

我知道JFrame有一个JPasswordField方法,但是我认为仅使用Scanner不会对我有帮助.

I am aware that JFrame has a JPasswordField method but I don't think that helps me when in just using 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;
    }
}

推荐答案

您可以使用

提供格式化的提示,然后从中读取密码或密码 禁用回显功能的控制台

Provides a formatted prompt, then reads a password or passphrase from the console with echoing disabled

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

    }

这篇关于如何隐藏扫描仪输入文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆