I would like to remove a String from the memory of .NET application. Lets say we have a Decryption method, which comes from a 3rd party lib and it returns a String. This is not smart, but there is nothing I can do about it.
String s = SomeComponent.Decrypt("cypherstring")
Now I would copy the content of s
into a SecureString
to work with. But... how do I get rid of s
. I know the GC would collect it here, but if I used that string for a while it would stay. Also I would like to not rely on the GC here, since it might be security related - which requries my code to be deterministic.
My Idea is something like that:
public static SecureString Convert(ref String s)
{
//copy content of s into SecureString
//shred s
}
There is no big deal to copy the data into the SecureString, but how do I "destroy" s?