Search code examples
c#.netsecuritysecurestring

Convert String to SecureString


How to convert String to SecureString?


Solution

  • You don't. The whole reason for using the SecureString object is to avoid creating a string object (which is loaded into memory and kept there in plaintext until garbage collection). However, you can add characters to a SecureString by appending them.

    var s = new SecureString();
    s.AppendChar('d');
    s.AppendChar('u');
    s.AppendChar('m');
    s.AppendChar('b');
    s.AppendChar('p');
    s.AppendChar('a');
    s.AppendChar('s');
    s.AppendChar('s');
    s.AppendChar('w');
    s.AppendChar('d');