I need to pass a SecureString
from my client process to my service. Both are written using .NET and C#. I'm using named pipes to pass the data between processes. My question is how to get access to the SecureString
as byte array to pass it to another process? And then re-assemble it back to SecureString
in the receiving process?
Asked
Active
Viewed 1,734 times
1

c00000fd
- 20,994
- 29
- 177
- 400
-
Do you ***really*** need to use SecureString? By needing to pass the string in to the byte array for serialization you are going to loose 99% of your protection anyway. *Could* you use just a `String` instead? – Scott Chamberlain Oct 12 '13 at 04:28
-
@ScottChamberlain: Well, SecureString is represented as a memory byte array, right? I was thinking to pass that byte array instead of decrypting it into a string. – c00000fd Oct 12 '13 at 04:29
-
It is stored as a encrypted byte array, however you can't get access to the encrypted byte array directly, they only way to access the string is to decrypt it to unmanaged memory. Depending exactly which IPC you are using you may be able to just pass the pointer returned by [`SecureStringToGlobalAllocUnicode`](http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.securestringtoglobalallocunicode.aspx) – Scott Chamberlain Oct 12 '13 at 04:33
-
`SecureStringToGlobalAllocUnicode` decrypts it and returns a pointer to its location in memory. I'm not sure why I can't get access to encrypted memory array? – c00000fd Oct 12 '13 at 04:47
-
Because they don't expose it anywhere, if you can find a way to access it without decrypting it feel free to post it as an answer yourself. – Scott Chamberlain Oct 12 '13 at 04:52
1 Answers
0
Since we also had the same problem and since we were unable to access the encrypted bytes what we did was, access the decrypted bytes on fly and encrypt them using our own algorithm or encryption technique. And on other side decrypted the bytes and assigned byte by byte to SecureString calling AppendChar function.
Code to access the byte array of SecureString
IntPtr passwordBytes = Marshal.SecureStringToCoTaskMemUnicode(password);
try
{
unsafe
{
byte* byteArrayStart = (byte*)passwordBytes.ToPointer();
int length = password.Length;
byte[] encrypted = new byte[length];
for (int i = 0; i < length; ++i)
{
encrypted[i] = EncryptByte(*(byteArrayStart + i));
}
}
}
finally
{
// This removed the decrypted data bytes from memory as soon as we finished encrypting bytes. Thus reducing the window that any one can access the secure password
Marshal.ZeroFreeGlobalAllocAnsi(passwordBytes);
}
Now, on other process side I believe code will be simple to decrypt and assign to SecureString. Remember, there we used AppendChar function so that all the decrypted bytes are not visible at once or in continuation in memory (reducing chance of password being seen).
Example,
SecureString mypassword = new SecureString();
for (int i = 0; i < length; ++i) //length of bytes
{
mypassword.AppendChar ((char) DecryptByte(encryptedByteBuffer[i] ));
}

Deepak Bhatia
- 6,230
- 2
- 24
- 58
-
Hmm. What is `EncryptByte` and `DecryptByte`? I guess I can use AES: http://stackoverflow.com/questions/202011/encrypt-decrypt-string-in-net – c00000fd Oct 12 '13 at 20:33
-
These are your custom functions where you can alter the byte value as you need may be AES or just adding-subtracting some byte value which can be exchanged in handshake – Deepak Bhatia Oct 14 '13 at 05:58