Search code examples
c#uwpsecurestring

SecureString in UWP usage


I'm trying to keep some strings safe in memory and Installed the SecureString Nuget package in a UWP targeting 10240. Using it is pretty easy:

SecureString secureStr = new SecureString();
for (int i = 0; i < someString.Length; i++)
{
    secureStr.AppendChar(someString[i]);
}
secureStr.MakeReadOnly();

But in order to access the content I have to use:

 IntPtr stringPointer = Marshal.SecureStringToBSTR(secureStringObj);
string normalString = Marshal.PtrToStringBSTR(stringPointer);
Marshal.ZeroFreeBSTR(stringPointer);

But in UWP there is not such method in the Marshal class SecureStringToBSTR any other ways to read the content of a SecureString?

Thanks!


Solution

  • You should be able to use the SecureStringMarshal class to get a pointer, and then the regular Marshal.PtrToStringXXXX methods to read it as a string etc.