Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we read password from console in char array instead of String [duplicate]

Tags:

Possible Duplicate:
Why is char[] preferred over string for passwords?

When I was preparing for OCPJP I came accross the topic - "Reading User input from console".

There was an example where it read username in String reference, whereas password in a char[] array, but I couldn't understand why it used char array.. Here is the code : -

Console console = System.console();

String username = console.readLine("User Name? ");
char[] password = console.readPassword("Password? "); 

This raised a doubt in my mind.. Why didn't we used String reference to store password. Since Strings are immutable, so it must be more secure to read password in a String, as its content could not be changed for that matter.

So, what's the whole point in reading password in char[] array..

Can anyone shed some light in this matter?

like image 458
Rohit Jain Avatar asked Oct 02 '12 13:10

Rohit Jain


People also ask

Why do we prefer char arrays instead of Strings to store passwords?

Since Strings are immutable there is no way the contents of Strings can be changed because any change will produce a new String, while if you use a char[] you can still set all the elements as blank or zero. So storing a password in a character array clearly mitigates the security risk of stealing a password. 2.

Will you store password in string or char array?

Since Strings are immutable there is no way the contents of Strings can be changed because any change will produce new String, while if you char[] you can still set all his elements as blank or zero. So Storing the password in a character array clearly mitigates security risk of stealing passwords.

Why we use char array for passwords in Java?

Security: Any one who has access to memory dump can find the password in clear text and that's another reason to use encrypted password than plain text. So Storing password in character array clearly mitigates security risk of stealing password.

Why we should not use string for manipulating passwords in Java and which we prefer?

But since string is immutable in Java, we can't change its contents after usage. That means if we use a String object for storing passwords, we can't get rid of the password until the Garbage collector clears it, which poses a big security threat.


2 Answers

As you said, strings are immutable, meaning that once you've created the string, if another process can dump memory, there's no way (ok, may with reflection) you can get rid of the data before GC kicks in.

With an array, you can explicitly wipe the data after you're done with it: you can overwrite the array with anything you like, and the password won't be present anywhere in the system, even before garbage collection.

like image 148
caarlos0 Avatar answered Sep 27 '22 19:09

caarlos0


From the Javadoc of java.io.Console:

Security note: If an application needs to read a password or other secure data, it should use readPassword() or readPassword(String, Object...) and manually zero the returned character array after processing to minimize the lifetime of sensitive data in memory.

This is just to prevent other applications (like keyloggers etc., from accessing the password.

And moreover if you use String, since they are immutable, modifying them would create copies in the memory. Using char[] would save you in this case. As they are mutable, they won't create an copies and you can make them null after processing.

like image 40
Sri Harsha Chilakapati Avatar answered Sep 27 '22 20:09

Sri Harsha Chilakapati