SecureString to Byte[] C#

后端 未结 4 2149
忘了有多久
忘了有多久 2020-12-16 20:51

How would I get a byte[] equivalent of a SecureString (which I get from a PasswordBox)?

My objective is to write these bytes

4条回答
  •  独厮守ぢ
    2020-12-16 21:50

    Per this, http://www.microsoft.com/indonesia/msdn/credmgmt.aspx, you can marshal it into a stock C# string and then convert that into an array of bytes:

    static string SecureStringToString( SecureString value )
    {
      string s ;
      IntPtr p = Marshal.SecureStringToBSTR( value );
      try
      {
        s = Marshal.PtrToStringBSTR( p ) ;
      }
      finally
      {
        Marshal.FreeBSTR( p ) ;
      }
      return s ;
    }
    

    or per this answer, How to convert SecureString to System.String?, you can use Marshal.ReadByte and Marshal.ReadInt16 on the IntPtr to get what you need.

提交回复
热议问题