Search code examples
c#wpfencryptionsecurestring

SecureString to Byte[] C#


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

My objective is to write these bytes using a CryptoStream to a file, and the Write method of that class takes a byte[] input, so I want to convert the SecureString to the byte[] so I can use in with a CryptoStream.

EDIT: I don't want to use string as it defeats the point of having a SecureString


Solution

  • I modified from the original answer to handle unicode

    IntPtr unmanagedBytes = Marshal.SecureStringToGlobalAllocUnicode(password);
    byte[] bValue = null;
    try
    {
        byte* byteArray = (byte*)unmanagedBytes.GetPointer();
    
        // Find the end of the string
        byte* pEnd = byteArray;
        char c='\0';
        do
        {
            byte b1=*pEnd++;
            byte b2=*pEnd++;
            c = '\0';
            c= (char)(b1 << 8);                 
            c += (char)b2;
        }while (c != '\0');
    
        // Length is effectively the difference here (note we're 2 past end) 
        int length = (int)((pEnd - byteArray) - 2);
        bValue = new byte[length];
        for (int i=0;i<length;++i)
        {
            // Work with data in byte array as necessary, via pointers, here
            bValue[i] = *(byteArray + i);
        }
    }
    finally
    {
        // This will completely remove the data from memory
        Marshal.ZeroFreeGlobalAllocUnicode(unmanagedBytes);
    }