密码屏蔽不会在需要时终止程序

发布于 2024-09-26 18:57:38 字数 3398 浏览 4 评论 0原文

我开发了以下应用程序,其中我需要屏蔽 PIN 并在用户输入错误 PIN 三次后终止程序。但是,仅当我在开始时关闭 stopThread (我在下面的代码中对其进行了注释)时,程序才会终止,但是当我这样做时,所有三种机会都不会发生密码屏蔽。但是,当我在显示登录成功屏幕之前关闭 stopThread 时,程序不会终止。我需要使用ctrl+c来结束程序。

非常感谢任何帮助。

boolean stopThread = false;
boolean hideInput = false;
boolean shortMomentGone = false;
public static double userBal=0.0D;

public void run(){
    try{
        sleep(500);
    } catch(InterruptedException e){
    }
    shortMomentGone = true;
    while(!stopThread){
        if(hideInput){
            System.out.print("\b*");
        }
        try{
            sleep(1);
        } catch(InterruptedException e){
        }
    }
}

public static final int NB_OF_TRIES = 3;        

public void validatePin(){
    BankAccount getAll=new BankAccount();
String pin="";
    getAll.Login();
    Login hideThread =new Login();
    hideThread.start();
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try{    
    do{

        } while(hideThread.shortMomentGone == false  );          
    // Now the hide thread should begin to overwrite any input with "*"
        hideThread.hideInput = true;            // Read the PIN
        System.out.println("\nPIN:");

    boolean pinMatch = false;
        int i = 0;

    while(!pinMatch && i < NB_OF_TRIES) {
        hideThread.hideInput = true;
        pin = in.readLine();
    i++;
        //hideThread.stopThread = true;       //Program terminates after third attempt 
                                              //PIN masking is stopped, if uncommented
        System.out.print("\b \b");        
        if(pin.equals(" ")){
    System.out.println("Please do not leave unnecessary spaces!");
    getAll.Login();
    }else if(pin.equals("")){
    System.out.println("Please do not press the enter key without entering the PIN!");
        getAll.Login();
    }

    FileInputStream fileinputstream = new FileInputStream(".\\AccountInfo.txt");
        DataInputStream datainputstream = new DataInputStream(fileinputstream);
        BufferedReader bufferedreader1 = new BufferedReader(new InputStreamReader(datainputstream));

    do
        {
            String s1;
            if((s1 = bufferedreader1.readLine()) == null)
            {
                break;
            }
            if(s1.trim().charAt(0) != '#')
            {
                String as[] = s1.split(" ");
                if(pin.equals(as[0]))
                {          
                    System.out.println("You have login!");
                    String s2 = as[2];
                    userBal = Double.parseDouble(s2);                       
                    getAll.balance = userBal;
                hideThread.stopThread = true;
                    getAll.MainMenu();
        System.exit(0);
                }else if(pin != as[0]){
        System.out.println("Invalid PIN!");
        getAll.Login();           
        System.out.println("\n NOTE :- You are only allowed to enter the PIN THREE times. The number of tries remaining before your card is blacklisted are "+i + "\n Please re-enter your PIN");
                }
            }
        } while(true);
        datainputstream.close();    
    }//End of While Loop

    }catch(Exception exception)
    {
        System.err.println((new StringBuilder()).append("Error: ").append(exception.getMessage()).toString());
    }//End of try-catch block    
}

I developed the following application in which I needed to masking the PIN and terminate the program after the user has entered the wrong PIN thrice. However, the program terminates only if i close the stopThread at the beginning (I commented it in the code below), however the password masking does not occur for all the three channces when I do so. But, when I close the stopThread just before displaying the login successful screen, the program does not terminate. I need to use ctrl+c to end the program.

Any help is greatly appreciated.

boolean stopThread = false;
boolean hideInput = false;
boolean shortMomentGone = false;
public static double userBal=0.0D;

public void run(){
    try{
        sleep(500);
    } catch(InterruptedException e){
    }
    shortMomentGone = true;
    while(!stopThread){
        if(hideInput){
            System.out.print("\b*");
        }
        try{
            sleep(1);
        } catch(InterruptedException e){
        }
    }
}

public static final int NB_OF_TRIES = 3;        

public void validatePin(){
    BankAccount getAll=new BankAccount();
String pin="";
    getAll.Login();
    Login hideThread =new Login();
    hideThread.start();
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try{    
    do{

        } while(hideThread.shortMomentGone == false  );          
    // Now the hide thread should begin to overwrite any input with "*"
        hideThread.hideInput = true;            // Read the PIN
        System.out.println("\nPIN:");

    boolean pinMatch = false;
        int i = 0;

    while(!pinMatch && i < NB_OF_TRIES) {
        hideThread.hideInput = true;
        pin = in.readLine();
    i++;
        //hideThread.stopThread = true;       //Program terminates after third attempt 
                                              //PIN masking is stopped, if uncommented
        System.out.print("\b \b");        
        if(pin.equals(" ")){
    System.out.println("Please do not leave unnecessary spaces!");
    getAll.Login();
    }else if(pin.equals("")){
    System.out.println("Please do not press the enter key without entering the PIN!");
        getAll.Login();
    }

    FileInputStream fileinputstream = new FileInputStream(".\\AccountInfo.txt");
        DataInputStream datainputstream = new DataInputStream(fileinputstream);
        BufferedReader bufferedreader1 = new BufferedReader(new InputStreamReader(datainputstream));

    do
        {
            String s1;
            if((s1 = bufferedreader1.readLine()) == null)
            {
                break;
            }
            if(s1.trim().charAt(0) != '#')
            {
                String as[] = s1.split(" ");
                if(pin.equals(as[0]))
                {          
                    System.out.println("You have login!");
                    String s2 = as[2];
                    userBal = Double.parseDouble(s2);                       
                    getAll.balance = userBal;
                hideThread.stopThread = true;
                    getAll.MainMenu();
        System.exit(0);
                }else if(pin != as[0]){
        System.out.println("Invalid PIN!");
        getAll.Login();           
        System.out.println("\n NOTE :- You are only allowed to enter the PIN THREE times. The number of tries remaining before your card is blacklisted are "+i + "\n Please re-enter your PIN");
                }
            }
        } while(true);
        datainputstream.close();    
    }//End of While Loop

    }catch(Exception exception)
    {
        System.err.println((new StringBuilder()).append("Error: ").append(exception.getMessage()).toString());
    }//End of try-catch block    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作死小能手 2024-10-03 18:57:38

java.io.Console中有一个readPassword()方法,使用它。为什么你需要一个单独的线程?这让一切变得太复杂了。

关于你的问题为什么这不关闭:如果你不这样做,Java可能会将 while(isTrue){} 优化为 if(isTrue) { while(true) { } }不设置 isTrue volatile 或同步对 isTrue (getter/setter) 的访问。这种优化称为提升,并在《Effective Java SE》第 66 项中进行了解释。

这里有一篇文章准确地解释了您的问题:回显 * 而不是空白。
http://java.sun.com/developer/technicalArticles/Security/pwordmask/
他们也在走复杂的路,但它有效。我更喜欢空白而不是星号,因为这是更简单的方法。不回显 * 是 *nix 标准据我所知。

There's a readPassword() method in java.io.Console, use that. Why do you need a separate thread at all? That makes everything way too complicated.

Regarding your question why this does not close: Java may optimize while(isTrue){} to something like if(isTrue) { while(true) { } } if you don't set isTrue volatile or synchronize the access to isTrue (getter/setter). This optimizations is called hoisting and explained in Effective Java SE, item 66.

Here is an article which explains exactly your problem: echoing * instead of blanks.
http://java.sun.com/developer/technicalArticles/Security/pwordmask/
They are going the complicated way, too but it works. I would prefer blanks over asterisks since that is the easier way to go. Not echoing * is *nix standard afaik.

老子叫无熙 2024-10-03 18:57:38

实际上,在我分析了它之后,我意识到系统不会终止的原因是因为它没有保存在正确的位置。因此,解决方案是在 while 循环关闭后立即结束程序,然后一切都会正常工作。

        } while(true);
        datainputstream.close(); 
 }//End of While Loop
     System.exit(0);  // After the system is closed the program would terminate after the third attempt
    }catch(Exception exception)
    {
        System.err.println((new StringBuilder()).append("Error: ").append(exception.getMessage()).toString());
    }//End of try-catch block

Actually after I analysed it a but more i realized that the reason the system wont terminate is because it is not kept in the proper place. Therefore, the solution would be to end the program as soon as the while loop is closed and then everything would work fine.

        } while(true);
        datainputstream.close(); 
 }//End of While Loop
     System.exit(0);  // After the system is closed the program would terminate after the third attempt
    }catch(Exception exception)
    {
        System.err.println((new StringBuilder()).append("Error: ").append(exception.getMessage()).toString());
    }//End of try-catch block
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文