Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

java - String to asterisk, masking password

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...