In Swing, the password field has a getPassword()
(returns char[]
) method instead of the usual getText()
(returns String
) method. Similarly, I have come across a suggestion not to use String
to handle passwords.
Why does String
pose a threat to security when it comes to passwords?
It feels inconvenient to use char[]
.
To quote an official document, the Java Cryptography Architecture guide says this about
char[]
vs.String
passwords (about password-based encryption, but this is more generally about passwords of course):Guideline 2-2 of the Secure Coding Guidelines for the Java Programming Language, Version 4.0 also says something similar (although it is originally in the context of logging):
String in java is immutable. So whenever a string is created, it will remain in the memory until it is garbage collected. So anyone who has access to the memory can read the value of the string.
If the value of the string is modified then it will end up creating a new string. So both the original value and the modified value stay in the memory until it is garbage collected.
With the character array, the contents of the array can be modified or erased once the purpose of the password is served. The original contents of the array will not be found in memory after it is modified and even before the garbage collection kicks in.
Because of the security concern it is better to store password as a character array.
1) Since Strings are immutable in Java if you store the password as plain text, it will be available in memory until Garbage collector clears it and since String is used in String pool for reusability, there is pretty high chance that it will remain in memory for the long duration, which poses a security threat. Since anyone who has access to memory dump can find the password in clear text and that's another reason you should always use an encrypted password than plain text. Since Strings are immutable, there are no way contents of Strings can be changed because any change will produce new String, while if you char[] you can still set all his element as blank or zero. So Storing password in character array mitigates security risk of stealing the password.
2) Java itself recommends using getPassword() method of JPasswordField which returns a char[] and deprecated getText() method which returns password in clear text stating security reason. It's good to follow advice from Java team and adhering to standard rather than going against it.
These are all the reasons, one should choose a char[] array instead of String for a password.
1. Since Strings are immutable in Java, if you store the password as plain text it will be available in memory until the Garbage collector clears it, and since String is used in the String pool for reusability there is a pretty high chance that it will remain in memory for a long duration, which poses a security threat.
Since anyone who has access to the memory dump can find the password in clear text, that's another reason you should always use an encrypted password rather than plain text. 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. Java itself recommends using the getPassword() method of JPasswordField which returns a char[], instead of the deprecated getText() method which returns passwords in clear text stating security reasons. It's good to follow advice from the Java team and adhere to standards rather than going against them.
3. With String there is always a risk of printing plain text in a log file or console but if you use an Array you won't print contents of an array, but instead its memory location gets printed. Though not a real reason, it still makes sense.
Referenced from this blog. I hope this helps.
getPassword()
method of JPasswordField which returns a char[] and deprecatedgetText()
method which returns password in clear text stating security reason.toString() there is always a risk of printing plain text in log file or console but if use Array you won't print contents of the array instead its memory location get printed.
Final thoughts: Though using char[] is not just enough you need to erase content to be more secure. I also suggest working with hashed or encrypted password instead of plain text and clearing it from memory as soon as authentication is completed.
The short and straightforward answer would be because
char[]
is mutable whileString
objects are not.Strings
in Java are immutable objects. That is why they can't be modified once created, and therefore the only way for their contents to be removed from memory is to have them garbage collected. It will be only then when the memory freed by the object can be overwritten, and the data will be gone.Now garbage collection in Java doesn't happen at any guaranteed interval. The
String
can thus persist in memory for a long time, and if a process crashes during this time, the contents of the string may end up in a memory dump or some log.With a character array, you can read the password, finish working with it as soon as you can, and then immediately change the contents.