Ensure the removal of confidential data from memory
My application requires the user to enter a password like this:
using (var passwordForm = new PasswordForm())
{
var result = passwordForm.ShowDialog();
if (result == DialogResult.OK)
{
password = new SecureString();
foreach(var c in passwordForm.PasswordBox.Text)
{
password.AppendChar(c);
}
}
}
There password
will be a field SecureString
that is held on the password for some time.
Obviously, PasswordField.Text
this is just a plain plaintext string, which means that the user's password is open. This is not actually the case (as far as I know), so it is a necessary evil. Because of this, I want the time period during which the password is as short as possible.
What's the best way to achieve this? The password form will be removed as soon as I am done with this, but will this actually remove all "open text entries" of the password from memory? And if not, what's the best way to ensure that this happens as soon as possible?
source to share
I believe I have already answered this here , but let me summarize my understanding:
- A use case for SecureString is to prevent a developer from accidentally displaying a password for a user in a log or crash report.
- If you are worried about a virus stealing your password from memory than even encryption, this will not help, because at some point you will need to decite it (as explained here ), at which point the virus might just get the password.
- You can create a custom control like one that uses SecureString for the textbox, but I would question what the point is. Unless you generate log entries or crash reports, dumping everything in memory probably doesn't make sense.
source to share