2

I'm creating this simple login code for an ATM machine. You enter username and password and you logs in, that works just great. Since I'm not connecting to a database or an external text file and I've just got 1 user, it's just written plainly in the Java code. But when you enter the password "p4ss" I want it to be masked, so instead of seing it on the screen while typing you should see "* * * *" or " " just blank (Like when you enter pass on Linux).

Currently my code looks like this:

    String user;
    String pass;            
    System.out.print("User: ");
    user = Keyboard.readString();
    System.out.print("Pass: ");
    pass = Keyboard.readString();

    if ((user.equals("Admin")) && (pass.equals("p4ss")))            
    {       
        menu();      
    }    
    else
    {       
        out.println("Wrong username or password.");
    }

Would appreciate any help I could get.

jww
  • 97,681
  • 90
  • 411
  • 885
Michael
  • 644
  • 5
  • 14
  • 33
  • 2
    What is the Keyboard class here? If you're writing code for an ATM, why on earth are you using System.out? – Jon Skeet Oct 07 '11 at 13:01
  • 1
    Stackoverflow has built-in security, when you type p4ss all I see is **** – JRL Oct 07 '11 at 13:01
  • Well it's just a login page, the keyboard class is so I can input the text. The system out is so I can see the output on the screen. – Michael Oct 07 '11 at 13:05
  • Possible duplicate of [How to mask a password in Java 5?](http://stackoverflow.com/questions/1108937/how-to-mask-a-password-in-java-5) – jww Oct 05 '14 at 03:09

4 Answers4

2

Michael, have a look at the description on Sun's website: https://web.archive.org/web/20120214061606/http://java.sun.com/developer/technicalArticles/Security/pwordmask

Or, if you're using Java 5 or newer, you can use this: How to mask a password in Java 5?

Community
  • 1
  • 1
Pieter
  • 3,339
  • 5
  • 30
  • 63
1

I assume this is a simulated ATM...

Dev has pointed out that password masking on the console is supported out of the box, so you can use that. However for anything but the most trivial of IO you'd be better off using Swing or a "curses-like" library:

Paul Cager
  • 1,910
  • 14
  • 21
  • Password masking is supported out of the box in JavaSE 6 or newer as indicated in my answer. – Dev Oct 07 '11 at 13:22
0

There's a special method Console.readPassword() for doing this, introduced in Java 6. You obviously couldn't run code like this on a real ATM, though! Swing has JPasswordField which lets you do this kind of masking in a GUI, and one could imagine a Swing-based ATM window.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • The first part is incorrect, you can mask a password on the console in Java6 or newer. – Dev Oct 07 '11 at 13:20
0

If you have JavaSE 6 or newer you can use Console.readPassword()

Dev
  • 11,919
  • 3
  • 40
  • 53