Masking password input from the console : Java

JavaConsolePasswordsConsole ApplicationMasking

Java Problem Overview


How to mask a password from console input? I'm using Java 6.

I've tried using console.readPassword(), but it wouldn't work. A full example might help me actually.

Here's my code:

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");
    }
}

I'm getting a NullPointerException...

Java Solutions


Solution 1 - Java

A full example ?. Run this code : (NB: This example is best run in the console and not from within an IDE, since the System.console() method might return null in that case.)

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();
    }
}

Solution 2 - Java

You would use the Console class

char[] password = console.readPassword("Enter password");  
Arrays.fill(password, ' ');

By executing readPassword echoing is disabled. Also after the password is validated it is best to overwrite any values in the array.

If you run this from an ide it will fail, please see this explanation for a thorough answer: Explained

Solution 3 - Java

Console console = System.console();
String username = console.readLine("Username: ");
char[] password = console.readPassword("Password: ");

Solution 4 - Java

The given code given will work absolutely fine if we run from console. and there is no package name in the class

You have to make sure where you have your ".class" file. because, if package name is given for the class, you have to make sure to keep the ".class" file inside the specified folder. For example, my package name is "src.main.code" , I have to create a code folder,inside main folder, inside src folder and put Test.class in code folder. then it will work perfectly.

Solution 5 - Java

If you're dealing with a Java character array (such as password characters that you read from the console), you can convert it to a JRuby string with the following Ruby code:

# 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}"

See also "https://stackoverflow.com/a/27628738/4390019" and "https://stackoverflow.com/a/27628756/4390019"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNew StartView Question on Stackoverflow
Solution 1 - Javabilash.sahaView Answer on Stackoverflow
Solution 2 - JavaWoot4MooView Answer on Stackoverflow
Solution 3 - Javauser1525941View Answer on Stackoverflow
Solution 4 - JavaAmulya KoppulaView Answer on Stackoverflow
Solution 5 - JavaDaniel HuffmanView Answer on Stackoverflow