There is a security note in javadoc for the java.io.Console class:
Safety Notice. If an application needs to read a password or other protected data, it should use readPassword () or readPassword (String, Object ...) and manually zero out the returned character array after processing to minimize the lifetime of sensitive data in memory.
Console cons;
char[] passwd;
if ((cons = System.console()) != null &&
(passwd = cons.readPassword("[%s]", "Password:")) != null) {
...
java.util.Arrays.fill(passwd, ' ');
}
I do not understand why you need such drastic measures? When a method that reads a password pops off the stack, the array object referenced by the local passwd variable will have the right to garbage collection. No one (even an attacker) can get a reference to this array, assuming that the array does not go beyond the scope of the method.
So, why do you need to change the array (erasing the password) when you know that it will be eligible for the GC as soon as the method pops out of the stack? They say:
to minimize the lifetime of sensitive data in memory
but for me, this programming style seems pretty ... desperate.
source
share