PC Review


Reply
Thread Tools Rate Thread

Destroy a string

 
 
Mark C
Guest
Posts: n/a
 
      3rd Jan 2007
I know a string is immutable, but is there any trick or any other way
to destroy a string

Thanks
www.quiznetonline.com

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      3rd Jan 2007
Well, as long as it isn't interned, it should (AFAIK) simply get
collected when available... do you have a specific situation in mind?

Marc


 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      3rd Jan 2007
Strings are objects and so, the GC controls when objects are removed from
the heap.


"Mark C" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I know a string is immutable, but is there any trick or any other way
> to destroy a string
>
> Thanks
> www.quiznetonline.com
>



 
Reply With Quote
 
DeveloperX
Guest
Posts: n/a
 
      3rd Jan 2007
Not really, I assume you're worried about things like passwords ending
up in the page file and the like. MS introduced the SecureString in V2,
then revoked it in V3 (at least stopped using it) as it's just not
secure. You can also try playing with volatile char arrays, I've heard
that's an option in that you can be (fairly) sure it's overwritten when
you overwrite each element. I recall issues with volatile, but I don't
use it so don't know what they are. There are also some Win32 API calls
which can allocate a bit of the page file (working from memory here,
basically just google password dotnet secure, that sort of thing) but
again that's problematic.
Sorry for being nebulous, I did look at this a while ago and came to
the conclusion that no there's no bulletproof way of protecting
strings, just try and hold them in memory for the shortest period
possible.


Mark C wrote:
> I know a string is immutable, but is there any trick or any other way
> to destroy a string
>
> Thanks
> www.quiznetonline.com


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      3rd Jan 2007
hi

What u mean with destroy it?



--
Ignacio Machin
machin AT laceupsolutions com



"Mark C" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I know a string is immutable, but is there any trick or any other way
> to destroy a string
>
> Thanks
> www.quiznetonline.com
>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      3rd Jan 2007
I imagine he means remove it from memory (the heap).


"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.com> wrote in
message news:%(E-Mail Removed)...
> hi
>
> What u mean with destroy it?
>
>
>
> --
> Ignacio Machin
> machin AT laceupsolutions com
>
>
>
> "Mark C" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I know a string is immutable, but is there any trick or any other way
>> to destroy a string
>>
>> Thanks
>> www.quiznetonline.com
>>

>
>



 
Reply With Quote
 
rossum
Guest
Posts: n/a
 
      3rd Jan 2007
On 3 Jan 2007 06:11:00 -0800, "Mark C" <(E-Mail Removed)>
wrote:

>I know a string is immutable, but is there any trick or any other way
>to destroy a string
>
>Thanks
>www.quiznetonline.com


 
Reply With Quote
 
rossum
Guest
Posts: n/a
 
      3rd Jan 2007
On 3 Jan 2007 06:11:00 -0800, "Mark C" <(E-Mail Removed)>
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.

rossum

 
Reply With Quote
 
Ben Voigt
Guest
Posts: n/a
 
      3rd Jan 2007

"rossum" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 3 Jan 2007 06:11:00 -0800, "Mark C" <(E-Mail Removed)>
> 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
>



 
Reply With Quote
 
rossum
Guest
Posts: n/a
 
      4th Jan 2007
On Wed, 3 Jan 2007 16:13:58 -0600, "Ben Voigt" <(E-Mail Removed)>
wrote:

>
>"rossum" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> On 3 Jan 2007 06:11:00 -0800, "Mark C" <(E-Mail Removed)>
>> 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
>

There may also be a copy on disk in swapspace, on automatic backups
and written on a post-it under the keyboard. It is not possible to be
completely secure, all that is possible is to make the attacker's job
more difficult.

rossum

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string? Daniel Microsoft Dot NET 7 12th Nov 2004 09:08 AM
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string? Daniel Microsoft C# .NET 10 3rd Nov 2004 03:26 PM
Inserting a destroy function which destroy a model after few runs Maite Microsoft Excel Misc 1 22nd Apr 2004 02:55 PM
Cannot create an object of type 'System.String[]' from its string representation 'String[] Array' for the 'Options' property. Hessam Microsoft C# .NET 0 8th Aug 2003 09:45 AM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Frank Oquendo Microsoft C# .NET 0 31st Jul 2003 08:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:34 AM.