SOLVED: Mask password with asterisk from console in Java

Introduction

In this article, we will study how to mask password with an asterisk Java console.  masking is an important technique that is used vastly in programming for security purposes like logging in and signing up for entering any pin information.

To mask password with asterisk java console, the console library provides a method that is readPassword method, it reads the password and hides it from the user, the password is stored in a character array which is returned by the method.

Advertisement

 

Method-1: Use System Console Read Password Method

The first method to mask password with asterisk java console is the console read password function, which is a java built in function used to mask the password, this can further be masked by adding a asterisk string in the code print the asterisk string  the code is  is written below :

import java.io.Console;

// mask password with asterisk java console
class Main {

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

        char[] passwordArray = console.readPassword("Enter your  password: ");
        for (int i = 0; i < passwordArray.length; i++) {
            System.out.print("*");
        }
        System.out.println();
        console.printf("Password entered was: \n", new String(passwordArray));

    }

    public static void main(String[] args) {
        new Main().consoleFunc();
    }
}

The output is :

Testing password
Enter your secret password: 
****
Password entered was: azka

 

Method-2: Use threads to erase characters from password

We can use a thread that can erase the echoed characters, the ones that are used

Here we are using the threading technique to mask password with asterisk java console by hiding the character while typing, the code is

import java.io.*;

// mask password with asterisk java console
class ThreadDisappear implements Runnable {
    private boolean end;
    public ThreadDisappear(String prompt) {
        System.out.print(prompt);
    }
    public void run() {
        end = true;
        while (end) {
            System.out.print("\010*");
            try {
                Thread.currentThread().sleep(1);
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    }

    public void maskEnd() {
        this.end = false;

    }

}


class maskwithsterik {
    public static void main(String[] args) {
        ThreadDisappear td = new ThreadDisappear("Enter your password: ");
        Thread t = new Thread(td);
        t.start();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String password = br.readLine();
            td.maskEnd();
            System.out.println("\nYour password is: " + password);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

 

The output of this code is :

Enter your password:***************
Your password is: thisispassword

 

Compare the Above Two Methods

Since Java has no specific API or library to mask password with asterisk Java console so we use different logics to do so we have seen 2 different methods above and the most efficient and less time-consuming is the first one since we are just hiding the password on console, we can also get it whenever we want, but it is a less secure method. Threading may take up more processing time and thus be less efficient.

Advertisement

Although the graphical user interfaces like Java swing and Java effects provide API to hide password fields.  but none of them are applicable to the Java console so we must stick with the traditional logic building here.

 

Conclusion

We have seen three Different techniques to mask password with asterisk Java console.  the first way is to use the console based masking by using readPassword function and the other is through thread, the thread reads and stops printing asterisks when input thread stops.

 

Further Reading

Masking Format
Data Masking and Security

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X