如何在Java中读取数字密码?

时间:2014-08-17 06:51:40

标签: java java.util.scanner password-protection

我试图让用户输入一个四位数的引脚,但我不希望它在键入时在屏幕上打印出来。我尝试过像这样使用System.console().readPassword();

import java.util.Scanner;
import java.io.*;
import java.util.InputMismatchException;
public class VirtualATM{
    private static String Details;  
    public static void main(String [] args){
        try{
            //Create variables & scanner to be used throughout the program.
            Scanner sc = new Scanner(System.in);
            boolean RegisterOrExist = false;
            int cardNo = 0;
            String DirToWriteFile = System.getProperty("user.dir") + "/VirtualATM.txt"; //Get path to write text file to.
            DirToWriteFile.trim();
            File file = new File(DirToWriteFile);
            // if file doesn't exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
            //Create writer to write to files.
             FileWriter fw = new FileWriter(file.getAbsoluteFile());
             BufferedWriter bw = new BufferedWriter(fw);
            System.out.println("Enter card number, or type register to register a new card.");
            String register = sc.nextLine();
            if(register.equalsIgnoreCase("register")){
                RegisterOrExist = false;
                RegisterNewCard();
            } else {
            RegisterOrExist = true;
            cardNo = Integer.valueOf(register);
            }
            bw.write(Details);
            //Close the writer.
            bw.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
    /*** Method for registering a new card **/
    public static void RegisterNewCard(){
        Scanner sc = new Scanner(System.in);
        System.out.print("Name: ");
        String name = sc.nextLine();
        int MaxVal = Integer.MAX_VALUE;
        int CardNo =  new java.util.Random().nextInt(MaxVal) + 1;
        int balance = new java.util.Random().nextInt(10000) + 1;
        boolean OverDraft = false;
        int OverDraftLimit = 0;
        if(OverDraft = true){
            OverDraftLimit = 250;
        }
        char [] PIN = {};
        System.out.println("Create a FOUR digit pin: ");
        try{
            PIN = System.console().readPassword();
        }catch(InputMismatchException e){
            e.printStackTrace();
        }
        int P1 = (int) PIN[0];
        int P2 = (int) PIN[1];
        int P3 = (int) PIN[2];
        int P4 = (int) PIN[3];
        int [] PinNo = {P1, P2, P3, P4};
        Details = "Name:   " + name + "   CardNo:   " + CardNo + "   Current Balance:   " + balance + "   overdraft?   " + OverDraft + "   OverDraftLimit:   " + OverDraftLimit + "   pin:   " + PinNo;
    }
}

然后我尝试使用int [] pinNo = {P1, P2, P3, P4}将图钉BufferedWriter写入文本文件。当我将引脚输入2566时,我在文本文件中得到以下文本: [I@42a57993 是否有其他方法可以在用户输入时不在屏幕上打印时读取密码?

3 个答案:

答案 0 :(得分:2)

<强>问题:

pin:   " + PinNo;

实际上是打印数组对象的memory location而不是数组元素本身

<强>溶液

您可以获取PinNo数组的每个索引,并将每个索引附加到字符串

 "pin:   " + PinNo[0] + PinNo[1] + PinNo[2] + PinNo[3]

答案 1 :(得分:2)

这条线

bw.write(Details);

details写入文件

Details = "..otherdetails..."+"pin:   " + PinNo;

表明您正在编写数组的哈希值。因此,只需将"pin: " + PinNo替换为 数组的实际元素

你可以简单地按照罗德所说或试试这个

Details = "..otherdetails..."+"pin:   " + java.util.Arrays.toString(PinNo)

提示 - 使用此功能可使您的密码更安全 试试这个

char[] PIN = System.console.readPassword();  
Arrays.fill(password, ' ');

答案 2 :(得分:1)

如果您尝试在Eclipse IDE控制台上运行代码,那么它将无法运行。这是bug System.console() (Java 6.0) returns null when running inside Eclipse

您需要从命令提示符或终端等运行

有关详细信息,请参阅以下文章: -

  1. Hide input on command line - stackoverflow
  2. Masking password input from the console - stackoverflow