title: Is SecureString ever practical in a C# application?
body: <p>Feel free to correct me if my assumptions are wrong here, but let me explain why I'm asking.</p>
  
  <p>Taken from MSDN, a <code>SecureString</code>:</p>
  
  <blockquote>
    <p>Represents text that should be kept confidential. The text is encrypted for privacy when being used, and deleted from computer memory when no longer needed.</p>
  </blockquote>
  
  <p>I get this, it makes complete sense to store a password or other private information in a <code>SecureString</code> over a <code>System.String</code>, because you can control how and when it is actually stored in memory, because a <code>System.String</code>:</p>
  
  <blockquote>
    <p>is both immutable and, when no longer needed, cannot be programmatically scheduled for garbage collection; that is, the instance is read-only after it is created and it is not possible to predict when the instance will be deleted from computer memory. Consequently, if a String object contains sensitive information such as a password, credit card number, or personal data, there is a risk the information could be revealed after it is used because your application cannot delete the data from computer memory.</p>
  </blockquote>
  
  <p>However, in the case of a GUI application (for example, an ssh client), the <code>SecureString</code> <em>has to be built from a</em> <code>System.String</code>. All of the text controls <em>use a string as its underlying data type</em>.</p>
  
  <p>So, this means that every time the user presses a key, the old string that was there is discarded, and a new string is built to represent what the value inside the text box is, even if using a password mask. <em>And we can't control when or if any of those values are discarded from memory</em>.</p>
  
  <p>Now it's time to log in to the server. Guess what? <em>You need to pass a string over the connection for authentication</em>. So let's convert our <code>SecureString</code> into a <code>System.String</code>.... and now we have a string on the heap with no way to force it to go through garbage collection (or write 0's to its buffer).</p>
  
  <p><strong>My point is</strong>: no matter what you do, somewhere along the line, that <code>SecureString</code> is <em>going</em> to be converted into a <code>System.String</code>, meaning it will at least exist on the heap at some point (without any guarantee of garbage collection).</p>
  
  <p><strong>My point is not</strong>: whether there are ways of circumventing sending a string to an ssh connection, or circumventing having a control store a string (make a custom control). For this question, you can replace "ssh connection" with "login form", "registration form", "payment form", "foods-you-would-feed-your-puppy-but-not-your-children form", etc.</p>
  
  <ul>
  <li>So, at what point does using a <code>SecureString</code> actually become
  practical?</li>
  <li>Is it ever worth the extra development time to completely eradicate
  the use of a <code>System.String</code> object?</li>
  <li>Is the whole point of <code>SecureString</code> to simply reduce the amount of time a <code>System.String</code> is on the heap (reducing its risk of moving to a physical swap file)?</li>
  <li>If an attacker already has the means for a heap inspection, then he most likely either (A) already has the means to read keystrokes, or (B) already <em>physically has the machine</em>... So would using a <code>SecureString</code> prevent him from getting to the data anyways?</li>
  <li>Is this just "security through obscurity"?</li>
  </ul>
  
  <p>Sorry if I'm laying the questions on too thick, curiosity just got the better of me. Feel free to answer any or all of my questions (or tell me that my assumptions are completely wrong). :)</p>