How to convert a string to securestring explicitly

Indish Cholleti picture Indish Cholleti · Mar 27, 2012 · Viewed 77.1k times · Source

I want the text entered in the textbox to be converted to securestring in c#.

Answer

Balazs Tihanyi picture Balazs Tihanyi · Mar 27, 2012

The simplest approach is to iterate over the source string and append one character at a time to the secure string, like so:

var secure = new SecureString();
foreach (char c in textbox1.Text)
{
    secure.AppendChar(c);
}