Convert String to SecureString

前端 未结 13 1920
粉色の甜心
粉色の甜心 2020-12-12 15:43

How to convert String to SecureString?

13条回答
  •  情深已故
    2020-12-12 16:29

    The following 2 extensions should do the trick:

    1. For a char array

      public static SecureString ToSecureString(this char[] _self)
      {
          SecureString knox = new SecureString();
          foreach (char c in _self)
          {
              knox.AppendChar(c);
          }
          return knox;
      }
      
    2. And for string

      public static SecureString ToSecureString(this string _self)
      {
          SecureString knox = new SecureString();
          char[] chars = _self.ToCharArray();
          foreach (char c in chars)
          {
              knox.AppendChar(c);
          }
          return knox;
      }
      

    Thanks to John Dagg for the AppendChar recommendation.

提交回复
热议问题