hide password on command line

Dear everybody,

I would like to find a way to hide password on command line when user type password. There is a way that use another thread to output '\b*'. However, it has some bugs. And I can't use Console class which has a method to hide password. Because my JDK is 1.5 and I can't change it because of some reasons.

Does anybody know a way to hide password on command line?

Thanks very much!

[424 byte] By [JavaFish2007a] at [2007-11-26 19:00:54]
# 1

> Dear everybody,

>

> I would like to find a way to hide password on

> command line when user type password. There is a way

> that use another thread to output '\b*'. However, it

> has some bugs. And I can't use Console class which

> has a method to hide password. Because my JDK is 1.5

> and I can't change it because of some reasons.

> Does anybody know a way to hide password on command

> line?

> Thanks very much!

In Java 1.5? You can't.

Like you already said: this can only be done with the new Java version (1.6) with the Console class.

prometheuzza at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 2
yes ... u need a higher version.
vaishali_mankara at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks for advices. I can't change JDK version because the change will cause conflict with some existing system. Is there another way?
JavaFish2007a at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 4
> ... Is there another way?No.
prometheuzza at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 5
Is there possible to get input without echoing on screen?
JavaFish2007a at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 6
> Is there possible to get input without echoing on> screen?No.
prometheuzza at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 7
> > Is there possible to get input without echoing on> > screen?> > No.No.
prometheuzza at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 8
> > > Is there possible to get input without echoing on> > > screen?> > > > No.> > No.No.
prometheuzza at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 9
You could give [url http://jline.sourceforge.net/]JLine[/url] a try.
paternostroa at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 10
use another way to hide your password... While writing it appears something like this ******
Contesta at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 11
> use another way to hide your password... While> writing it appears something like this ******Brilliant!
hunter9000a at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 12
> Is there possible to get input without echoing on screen?Possible, not easy...
Contesta at 2007-7-9 20:43:57 > top of Java-index,Java Essentials,Java Programming...
# 13
> > Is there possible to get input without echoing on screen?>> Possible, not easy...Not possible in Java.We're talking about the standard output console here.
prometheuzza at 2007-7-9 20:43:58 > top of Java-index,Java Essentials,Java Programming...
# 14

> Because my JDK is 1.5 and I can't change it because of some reasons.

Look how sweet Console is in 1.6:

import java.io.Console;

public class ConsoleExample {

public static void main(String[] args) {

Console console = System.console();

char[] password = console.readPassword("Please enter password:");

//don't do this: passwords shouldn't be stringified

String s = new String(password);

console.format("password =[%s]%n", s);

}

}

Find a way to install 1.6! We believe in you!

DrLaszloJamfa at 2007-7-9 20:43:58 > top of Java-index,Java Essentials,Java Programming...
# 15
//don't do this: passwords shouldn't be stringifiedHow do you pass it to the DB if it's not a String?
abillconsla at 2007-7-9 20:43:59 > top of Java-index,Java Essentials,Java Programming...
# 16
> //don't do this: passwords shouldn't be stringified> > How do you pass it to the DB if it's not a String?That is a dilli of a pickle.
DrLaszloJamfa at 2007-7-9 20:43:59 > top of Java-index,Java Essentials,Java Programming...
# 17

> > //don't do this: passwords shouldn't be

> stringified

> >

> > How do you pass it to the DB if it's not a String?

>

> That is a dilli of a pickle.

I vaguely remember reading something about this but cant remember the details - why should they not be stringy but it is OK to have them in a character array?

jbisha at 2007-7-9 20:43:59 > top of Java-index,Java Essentials,Java Programming...
# 18
why not just make the password literally "********" ? that should be plenty to fool any would be hackers.
den2681a at 2007-7-9 20:43:59 > top of Java-index,Java Essentials,Java Programming...
# 19

> I would like to find a way to hide password on command line when user type password.

Why are you reading the data from the console? Just display a simple GUI asking for the password. Then you don't have the problem. It will work in any version of the JDK.

Otherwise maybe this will be acceptable:

http://forum.java.sun.com/thread.jspa?forumID=256&threadID=264992

camickra at 2007-7-9 20:43:59 > top of Java-index,Java Essentials,Java Programming...
# 20
> I would like to find a way to hide password on command line when user type password. [url= http://java.sun.com/developer/technicalArticles/Security/pwordmask/]Password Masking in the Java Programming Language[/url]~
yawmarka at 2007-7-9 20:43:59 > top of Java-index,Java Essentials,Java Programming...
# 21

That's an interesting article that I'd seen before and yes, public char[] getPassword()

supercedes public String getText()

for the JPasswordField in swing. But not in AWT for TextField, and all the getConnection methods in java.sql.DriverManager class take Strings for URLs or PWDs. Would JPasswordField jpf = new JPasswordfield(" ... ");

// ...

String url,

user;

char[] pwd = jpf.getPassword();

DriverManager.getConnection(url, user, new String(pwd));

... do the trick ... I don't know.

abillconsla at 2007-7-9 20:44:00 > top of Java-index,Java Essentials,Java Programming...
# 22

> > > //don't do this: passwords shouldn't be

> > stringified

> > >

> > > How do you pass it to the DB if it's not a

> String?

> >

> > That is a dilli of a pickle.

>

> I vaguely remember reading something about this but

> cant remember the details - why should they not be

> stringy but it is OK to have them in a character

> array?

Because you can explicitly clear out the characters in the array as soon as you're done with it. You can't do this with a String.

As for sending it to the DB, you'd have to setBytes or setBlob or setObject. Never tried it, but one of those should work.

jverda at 2007-7-9 20:44:00 > top of Java-index,Java Essentials,Java Programming...
# 23

> new String(pwd)

*Ding*! I assume the point is to avoid creating a java.lang.String,

because it will hang around in memory for a spell and could be sniffed out

by another process examining the JVM process' memory.

I wonder if the DriverManager.getConnection method that takes a Properties

object would be willing to have it map "user" to a char[]?

DrLaszloJamfa at 2007-7-9 20:44:00 > top of Java-index,Java Essentials,Java Programming...
# 24
> As for sending it to the DB, you'd have to setBytes or setBlob or setObject. Never tried it, but one of those should work.I think the point here is that the password is not data but the user passwordto connect to the database.
DrLaszloJamfa at 2007-7-9 20:44:00 > top of Java-index,Java Essentials,Java Programming...
# 25

> > > > //don't do this: passwords shouldn't be

> > > stringified

> > > >

> > > > How do you pass it to the DB if it's not a

> > String?

> > >

> > > That is a dilli of a pickle.

> >

> > I vaguely remember reading something about this

> but

> > cant remember the details - why should they not

> be

> > stringy but it is OK to have them in a character

> > array?

>

>

> Because you can explicitly clear out the characters

> in the array as soon as you're done with it. You

> can't do this with a String.

>

> As for sending it to the DB, you'd have to setBytes

> or setBlob or setObject. Never tried it, but one of

> those should work.

HuH? ... no, I was talking about logging in / you know, creating a Connection - sorry that was not clearer - see my other post that clarifies a bit more.

abillconsla at 2007-7-9 20:44:00 > top of Java-index,Java Essentials,Java Programming...
# 26

> > new String(pwd)

>

> *Ding*! I assume the point is to avoid creating a

> java.lang.String,

> because it will hang around in memory for a spell and

> could be sniffed out

> by another process examining the JVM process'

> memory.

>

> I wonder if the DriverManager.getConnection method

> that takes a Properties

> object would be willing to have it map "user" to a

> char[]?

If that was preferred, then why don't they depcrecate the other methods for this reason?

abillconsla at 2007-7-9 20:44:00 > top of Java-index,Java Essentials,Java Programming...
# 27
>If that was preferred, then why don't they deprecate the other methods for this reason?I have my doubts that it even works!
DrLaszloJamfa at 2007-7-9 20:44:00 > top of Java-index,Java Essentials,Java Programming...
# 28

> > As for sending it to the DB, you'd have to setBytes

> or setBlob or setObject. Never tried it, but one of

> those should work.

>

> I think the point here is that the password is not

> data but the user password

> to connect to the database.

D'OH!

jverda at 2007-7-9 20:44:00 > top of Java-index,Java Essentials,Java Programming...
# 29

> > I vaguely remember reading something about this

> but

> > cant remember the details - why should they not

> be

> > stringy but it is OK to have them in a character

> > array?

>

>

> Because you can explicitly clear out the characters

> in the array as soon as you're done with it. You

> can't do this with a String.

Of course! Thanks.

jbisha at 2007-7-9 20:44:00 > top of Java-index,Java Essentials,Java Programming...
# 30

Although I guess this shouldn't be much of an issue in practice. The DB should only be accessed from a trusted app on a trusted host, where we have control over and knowledge of what apps are running. If the password is entered at that host's console, capturing it into a char[] vs. a String isn't really an issue.

But if the password is entered at a different host, then we'd want the char[], encrypt it, send it to the host that will connect to the db, decrypt, and turn it into a string where it's "safe."

jverda at 2007-7-9 20:44:01 > top of Java-index,Java Essentials,Java Programming...
# 31

> Although I guess this shouldn't be much of an issue

> in practice. The DB should only be accessed from a

> trusted app on a trusted host, where we have control

> over and knowledge of what apps are running. If the

> password is entered at that host's console, capturing

> it into a char[] vs. a String isn't really an issue.

>

> But if the password is entered at a different host,

> then we'd want the char[], encrypt it, send it to the

> host that will connect to the db, decrypt, and turn

> it into a string where it's "safe."

I think that is a very reasonable way to view it.

abillconsla at 2007-7-9 20:44:02 > top of Java-index,Java Essentials,Java Programming...
# 32

Actually there is. It is not very clean but it works. It involves using a thread that writes a back space and a * constantly while your waiting for input. When a user types a character it is pretty much immediately deleted and replaced with a '*'. See: http://java.sun.com/developer/technicalArticles/Security/pwordmask/ for details on how to implement.

baldmountaina at 2007-7-9 20:44:02 > top of Java-index,Java Essentials,Java Programming...
# 33

> Actually there is. It is not very clean but it works.

> It involves using a thread that writes a back space

> and a * constantly while your waiting for input. When

> a user types a character it is pretty much

> immediately deleted and replaced with a '*'. See:

> http://java.sun.com/developer/technicalArticles/Secur

> ty/pwordmask/ for details on how to implement.

Did you read the original question? The OP says that he tried that, but it's not a good solution.

Kaj

kajbja at 2007-7-9 20:44:02 > top of Java-index,Java Essentials,Java Programming...