I am trying to remove any traces of a normal string from memory, to do so I am creating an instance of SecureString
from a reference of the normal string. Like so:
public static unsafe void Burn(this string input)
{
fixed (char* c = input)
{
var secure = new SecureString(c, input.Length);
secure.Dispose();
}
}
The problem is that even after calling the dispose method the contents of input
are non-changed. From my understanding the SecureString
instance should reference the input
address and therefore clean if from memory upon Dispose()
call. What am I missing?
It appears the two parameter constructor is not meant to be used by your code. The documentation isn't clear but its use of the phrase Initializes a new instance of the SecureString class from a subarray of System.Char objects
tells me it's probably copying the data, not encrypting the existing string in place. This would make sense since the documentation for SecureString
specifically mentions a String
cannot be destroyed in a deterministic way.
A good way to test this theory would be to compare the addresses of input
and secure
to see if they still point to the same location in memory after the initialization.