Search code examples
c#securestring

How to convert a string to securestring explicitly


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


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