String to asterisk, masking password

前端 未结 4 1351
Happy的楠姐
Happy的楠姐 2020-12-21 15:53

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 a

相关标签:
4条回答
  • 2020-12-21 16:08

    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.

    0 讨论(0)
  • 2020-12-21 16:10

    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?

    0 讨论(0)
  • 2020-12-21 16:14

    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:

    • http://www.pitman.co.za/projects/charva/index.html
    • http://sourceforge.net/projects/javacurses/
    0 讨论(0)
  • 2020-12-21 16:28

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

    0 讨论(0)
提交回复
热议问题