gpt4 book ai didi

java - 使用 getEchoString 来屏蔽/隐藏密码输入?

转载 作者:行者123 更新时间:2023-12-01 04:18:42 25 4
gpt4 key购买 nike

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

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

这是我的第一个代码: 导入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 "*";

}
}

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

最佳答案

我不确定是否可以从 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 中运行(打印出“错误:无控制台”)。为了准确测试基于控制台的程序,请在实际的系统控制台(命令提示符、终端等)中编译并运行它。

关于java - 使用 getEchoString 来屏蔽/隐藏密码输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19195658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com