Re: Destroy a string

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"rossum" <rossum48@xxxxxxxxxxxx> wrote in message
news:c49op25un1a4lho1isikia8nvuueog06co@xxxxxxxxxx
On 3 Jan 2007 06:11:00 -0800, "Mark C" <MarkJohnCrosbie@xxxxxxxxx>
wrote:

I know a string is immutable, but is there any trick or any other way
to destroy a string

Thanks
www.quiznetonline.com
Not so much destroy the string as overwrite it:

/// <summary>
/// Overwrites a string in-situ. Useful for removing
/// evidence of keys, passwords etc.
/// </summary>
/// <param name="text">The string to overwrite.</param>
public static unsafe void OverwriteString(string text) {
const char overwriteChar = 'X';
fixed (char* cp = text) {
for (int i = 0; i < text.Length; ++i) {
cp[i] = overwriteChar;
} // end for
} // end fixed
} // end OverwriteString()

The string is still on the heap, but the sensitive information it
contained is no longer there.

The current location of the buffer is wiped, but if the string survived a
generation, the garbage collector has made copies :(


rossum



.



Relevant Pages

  • Re: Destroy a string
    ... public static unsafe void OverwriteString{ ... The string is still on the heap, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Destroy a string
    ... public static unsafe void OverwriteString{ ... The string is still on the heap, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Destroy a string
    ... public static unsafe void OverwriteString{ ... The string is still on the heap, ... That's why the SecureString was invented, it get's allocated in a non swappable fixed ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Modify a const string via reflection/unsafe code?
    ... compile time rather than a reference to the constant. ... static unsafe void StringManip ... string value; ...
    (microsoft.public.dotnet.languages.csharp)
  • Modify a const string via reflection/unsafe code?
    ... static unsafe void StringManip ... string value; ... That is - a const string can only be manipulated with unsafe code ... can be manipulated with unsafe code via reflection. ...
    (microsoft.public.dotnet.languages.csharp)