屏蔽来自控制台的密码输入:Java

屏蔽来自控制台的密码输入:Java,java,console,passwords,console-application,masking,Java,Console,Passwords,Console Application,Masking,如何从控制台输入屏蔽密码?我正在使用Java6 我尝试过使用console.readPassword(),但不起作用。一个完整的例子可能会对我有所帮助 这是我的密码: import java.io.BufferedReader; import java.io.Console; import java.io.IOException; import java.io.InputStreamReader; public class Test { public static void m

如何从控制台输入屏蔽密码?我正在使用Java6

我尝试过使用
console.readPassword()
,但不起作用。一个完整的例子可能会对我有所帮助

这是我的密码:

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test 
{   
    public static void main(String[] args) 
    {   
        Console console = System.console();

        console.printf("Please enter your username: ");
        String username = console.readLine();
        console.printf(username + "\n");

        console.printf("Please enter your password: ");
        char[] passwordChars = console.readPassword();
        String passwordString = new String(passwordChars);

        console.printf(passwordString + "\n");
    }
}
我得到一个NullPointerException…

您将使用该类

通过执行readPassword,将禁用回显。此外,在验证密码后,最好覆盖数组中的任何值


如果您从ide运行此操作,它将失败,请参阅以下解释以获得完整的答案:

完整示例?。运行此代码:(注意:此示例最好在控制台中运行,而不是从IDE中运行,因为在这种情况下System.console()方法可能返回null。)


如果您正在处理Java字符数组(例如从控制台读取的密码字符),可以使用以下Ruby代码将其转换为JRuby字符串:

# GIST: "pw_from_console.rb" under "https://gist.github.com/drhuffman12"

jconsole = Java::java.lang.System.console()
password = jconsole.readPassword()
ruby_string = ''
password.to_a.each {|c| ruby_string << c.chr}

# .. do something with 'password' variable ..    
puts "password_chars: #{password_chars.inspect}"
puts "password_string: #{password_string}"
#要点:“pw_from_console.rb”下https://gist.github.com/drhuffman12"
jconsole=Java::Java.lang.System.console()
password=jconsole.readPassword()
ruby_字符串=“”

password.to_a.each{| c | ruby_string如果我们从控制台运行,给定的代码将完全正常工作。并且类中没有包名


您必须确保在哪里有“.class”文件。因为,如果为类指定了包名,则必须确保将“.class”文件保留在指定的文件夹中。例如,我的包名为“src.main.code”,我必须在主文件夹、src文件夹中创建一个代码文件夹,并将Test.class放在代码文件夹中。这样它就可以完美地工作。

console.readPassword()怎么办
work?在控制台中运行,而不是从ide中运行此代码在ide中不工作,请查看我的更新答案。因此,我必须将项目导出为jar文件并从命令行运行?如果是这样,我这样做了,并出现此错误..“未能从C:\…加载主类清单属性”看看我刚刚得到的另一个NullPointerException…我不明白!你是在IDE中运行的吗?代码不是在运行吗?为什么要向下投票?对于获得null指针的人来说,从控制台运行它,从eclipse运行它不会work@Woot4Moo你能澄清为什么这是“高度不安全的”吗?在没有任何背景说明的情况下,仅此陈述对于促进理解并不十分有用(也不容易验证或反驳)。如果一行正在将密码打印到控制台,那么担心内存安全风险就没有什么意义了。需要填写密码数组。@Woot4Moo:请详细说明“接受的答案不安全!”。我看到您在读入密码后正在重写密码,但这似乎很愚蠢。无论您做什么,密码都将在内存中保留一段时间。即使您没有手动重写保存密码的内存,它也会被垃圾收集,内存将被其他内容重新填充。老实说,如果如果你有能力读取内存中的任意地址,我想你应该有更大的顾虑。@ArtOfWarfare如果“答案”发生变化:正如我所说的,它不安全的原因是:新字符串(passwordArray)一个新字符串被分配给字符串池,而“从不”在JVM的生命周期内消失。为了反驳您关于垃圾收集的陈述,您可能没有意识到String类是“特殊的”而且没有GCd。是的,我同意密码将在内存中保留一段时间,我只是碰巧尽可能地减少了这段时间。@ArtOfWarfare另外,任何像JVM这样复杂的系统都会有问题。自从我做了这个之后的近4年里,发现了无数的漏洞因此,如果你在java 6中运行JVM,我敢打赌,有一个“任意内存”,开发写数组中的字符值充其量是一个创可贴,不能保证“密码”的内容。从Console对象读取密码到
Arrays.fill()
方法完成之间,字符数组没有在堆中复制过几次。事实上,在Java中安全处理内存中的密码是不可能的。
import java.io.Console;
public class Main {

    public void passwordExample() {        
        Console console = System.console();
        if (console == null) {
            System.out.println("Couldn't get Console instance");
            System.exit(0);
        }

        console.printf("Testing password%n");
        char[] passwordArray = console.readPassword("Enter your secret password: ");
        console.printf("Password entered was: %s%n", new String(passwordArray));

    }

    public static void main(String[] args) {
        new Main().passwordExample();
    }
}
Console console = System.console();
String username = console.readLine("Username: ");
char[] password = console.readPassword("Password: ");
# GIST: "pw_from_console.rb" under "https://gist.github.com/drhuffman12"

jconsole = Java::java.lang.System.console()
password = jconsole.readPassword()
ruby_string = ''
password.to_a.each {|c| ruby_string << c.chr}

# .. do something with 'password' variable ..    
puts "password_chars: #{password_chars.inspect}"
puts "password_string: #{password_string}"