C# – How to convert a string to securestring explicitly

c++securestring

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

Best Solution

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);
}