How to read password from command-line without echoing it back to the user?

Your command-line application needs to authenticate the user and for security reasons, you don?t want to display the password on the console. Java API did not have support for this until java.io.Console class was introduced in JDK 1.6. Specifically, Console class has readPassword() method which can serve the purpose of reading the password from command-line securely. Here is the code snippet to prompt and read the password in one go:char[] password = console.readPassword(?Enter Password: ?);Here is a short and simple Java program that you can execute to get a feel for the Console class and especially its readPassword() method:import java.io.*;import java.util.Arrays;public class ReadPassword { public static void main(String[] args) { // Console class doesn?t provide any public constructors Console console = System.console(); String uname = console.readLine(“Enter Username: ” ); char[] password = console.readPassword(“Enter Password: “); // the below system.out?s purpose is to prove to us that the password has been correctly read System.out.println(“User: ” + uname + ” with Password: ” + new String(password) + ” is authenticated.”); // Manually erase the password character array by filling it with spaces Arrays.fill(password, ? ?); }}Notes:1. You can?t use new operator to create Console object as it has no public constructors. JVM maintains a single instance of console class which can be accessed using System.console() method.2. To prompt and read username we used Console?s readLine() method which returns String object unlike readPassword() method which returns character array3. It is a good idea to manually erase the password immediately after authenticating/processing the password so as to minimize the lifetime of sensitive data

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

How to Manage Your Finances after Buying a Home

How to Manage Your Finances after Buying a Home

Buying a home is a milestone in the journey of life – it’s one way to invest your money and create lasting memories. Now you know everything about a home purchase, home mortgage, and what is a conventional 97 loan but do you know what’s next after you sign the

Windows Logging is one of the vital aspects of any Windows system administration. However, it is mostly overlooked until the system develops a problem. This is because logs contain important information needed to troubleshoot and resolve system issues.

The Fundamentals of Windows Logging

Windows Logging is one of the vital aspects of any Windows system administration. However, it is mostly overlooked until the system develops a problem. This is because logs contain important information needed to troubleshoot and resolve system issues. Without it, tech experts might find it difficult to track a computer’s

Interstitial Ads: Best Practices for Successful Campaigns

Interstitial Ads: Best Practices for Successful Campaigns

Interstitial Ads: Best Practices for Successful Campaigns Interstitial ads are full-screen advertisements that appear to grasp the attention of on-site prospects, creating opportunities for brands seeking effective ways to communicate their proposition of value. With such an attention-grabbing format and high-impact visuals, it’s no wonder why interstitial advertising is proving

positive contribution tech

Technology’s Positive Contributions to Society

Technology has and continues to improve our lives. From the business world to the world of medicine, and our day-to-day lives, you can’t go a day without interacting with at least one form of technology. While some fear technology may be going too far, there are many ways in which

How to Choose From The Best Big Data Platforms in 2023

How to Choose From The Best Big Data Platforms in 2023

As big data continues to become increasingly popular in the business world, companies are always looking for better ways to process and analyze complex data. The process critically depends on the platform that manages and analyzes the data. In this article, we will provide a guide to help you choose