1

我是 Java 新手,我想问一下如何屏蔽/隐藏用户的密码输入。我希望程序在每次用户键入任何内容时都显示一个星号 (*),以便隐藏密码。我的密码程序单独(没有屏蔽)有效。用户将输入密码,之后只有 3 次尝试。我想用星号屏蔽用户输入的任何内容。

我在互联网上研究了如何使用遮罩,但我不明白。我的程序多次未能屏蔽。我对Java真的很陌生,如果你们会使用我的一些其他代码。如果可能,请解释一下。谢谢。

这是我的第一个代码: import java.io.*; 导入 javax.swing.JPasswordField;

 public class Capture_Password {
 public static void main(String[] args) {

 BufferedReader loopin=new BufferedReader (new InputStreamReader (System.in));
 JPasswordField passwordFld = new JPasswordField();

 final double pinno=12345;
 double ca;
 String attempt;

 try
 {
 System.out.println("Please enter the 5-digit PIN code:");
 attempt=loopin.readLine();
 ca=Double.parseDouble(attempt);

  if(ca==pinno)
    {
        System.out.println("Congratulations! You have entered the correct PIN code!");
    }

  else
    { 
            for(int tryleft=3; tryleft>=0 && ca!=pinno; tryleft--)
            {
                System.out.println("Sorry, you have entered the wrong PIN code");
                System.out.println("You have " + tryleft + " try/tries left");

                System.out.println();

                if(tryleft==0)
                    {
                        System.out.println("That was your last attempt, goodbye!");
                    }

                System.out.println();

                if(tryleft>0)
                {
                    System.out.println("Please enter the 5-digit PIN code:");
                    attempt=loopin.readLine();
                    ca=Double.parseDouble(attempt);
                }    

                if(ca==pinno)
                    {
                        System.out.println("Congratulations! You have entered the correct PIN code!");
                    }

            }   

            }



  }

 catch(Exception e)
 {
    System.out.println("ERROR");
 }
 }}

这是另一个:

 public class JPasswordField{
  public String getEchoString(){

       return "*";

 }
 }

我真的很感谢你的帮助,伙计们!

4

2 回答 2

1

我不确定是否可以从 java 程序的系统控制台中显示星号,但绝对可以简单地禁用显示输出。

使用 Java 中的 Console 对象,您可以调用 readPassword() 方法,该方法与大多数 unix 系统一样,在用户输入密码时不显示任何内容。

考虑以下代码:

public class Capture_Password{

    public static void main(String[] args){
        final char[] pinno = "12345".toCharArray(); //For high-security applications, this should be replaced with a method that uses temporary mutable objects to store passwords.
        char[] attempt;

        Console c = System.console();

        if(c != null){
            attempt = c.readPassword();

            if(Arrays.equals(attempt, pinno)){
                System.out.println("Congratulations! You have entered the correct PIN code!");
            }else{
                for(int tryleft=3; tryleft>=0 && !Arrays.equals(attempt, pinno); tryleft--){
                    System.out.println("Sorry, you have entered the wrong PIN code");
                    System.out.println("You have " + tryleft + " try/tries left");

                    System.out.println();

                    if(tryleft==0){
                        System.out.println("That was your last attempt, goodbye!");
                    }

                    System.out.println();

                    if(tryleft>0){
                        System.out.println("Please enter the 5-digit PIN code:");
                        attempt = c.readPassword();
                    }

                    if(Arrays.equals(attempt, pinno)){
                        System.out.println("Congratulations! You have entered the correct PIN code!");
                    }
                }
            }

            Arrays.fill(attempt, '0'); //Security measure, blanking the attempt
        }else{
            System.err.println("Error: No console");
        }
    }
}

请注意,由于 IDE 编译和运行程序的方式,此代码可能无法在 Eclipse 等 IDE 中运行(打印出“错误:无控制台”)。要准确测试基于控制台的程序,请在实际的系统控制台(命令提示符、终端等)中编译并运行它。

于 2013-10-05T12:00:25.810 回答
0

嘿,为什么不使用摆动框架工作#### 看一个例子,它会有所帮助

  import java.io.*;
  import javax.swing.*;
   import java.awt.*;
  class Experiment 
 {
    /*declaration */

        JFrame frame; 
        JPanel panel;
        JLabel label1, label2, label3;
        JTextField tf1;
        JPasswordField tf2;
        JButton button;
        String S1,S2,S3,S4;


        /*declaration ended */


Experiment()
    {




        /*memory initialization */

        frame = new JFrame("Eraser");
        panel = new JPanel();
        label1 = new JLabel("Sentence 1");
        label2 = new JLabel("What to Erase");
        label3 = new JLabel("");
        tf1 = new JTextField(20);
        tf2 = new JPasswordField(20);
        button = new JButton("Erase");


       /*memory initialization ended */



        /*containment*/

                frame.add(panel, BorderLayout.CENTER);
                panel.add(label1);
                panel.add(tf1);
                panel.add(label2);
                panel.add(tf2);
                panel.add(label3);
                panel.add(button);


        /*containment ended*/

    Toolkit tk = Toolkit.getDefaultToolkit();  
    int frame_width = ((int) tk.getScreenSize().getWidth());  
    int frame_height= ((int) tk.getScreenSize().getHeight());  

/*button-configuration*/
     button.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            buttonActionPerformed(evt);
        }
    });


 /*close-button-configuration*/


/* open - frame configuration */
    frame.setLocation(frame_width/2 - 125, frame_height/2 - 65);

    frame.setSize(250,190);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    /* close - frame configuration */










     }
    private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
    /*LOGIC*/


            S1= tf1.getText();
            S2= tf2.getText();


            S3 = S1.replace(S2,"");

         JOptionPane.showMessageDialog(null,S3);


            /*LOGIC ENDED   */
  }


  public static void main (String[]args)throws Exception 
  {

     Experiment object = new Experiment();





  }
}

### 如果您坚持使用控制台,请阅读此http://docs.oracle.com/javase/7/docs/api/java/io/Console.html#readPassword%28%29您必须运行至少Java 6。.this 不适用于 Eclipse 控制台#####

于 2013-10-05T10:02:42.710 回答