2013-10-12 54 views
1

我需要从我的客户端进程传递一个SecureString我的服务。两者都是使用.NET和C#编写的。我使用命名管道在进程之间传递数据。我的问题是如何获得SecureString作为字节数组传递给另一个进程?然后在接收过程中重新组装回SecureString如何将SecureString从一个进程传递到另一个进程?

+0

你真的*** ***需要使用SecureString的?由于需要将字符串传递到字节数组进行序列化,无论如何,您将释放99%的保护。 *可以*只用'String'来代替? –

+0

@ScottChamberlain:好吧,SecureString被表示为一个内存字节数组,对吧?我正在考虑传递该字节数组而不是将其解密为字符串。 – c00000fd

+0

它被存储为一个加密的字节数组,但你不能直接获得访问加密的字节数组,他们只访问字符串的方法是将其解密非托管内存。根据到底是哪IPC你使用你可以只是路过['SecureStringToGlobalAllocUnicode']返回的指针(http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.securestringtoglobalallocunicode。 aspx) –

回答

0

因为我们也有同样的问题,因为我们无法访问加密的字节我们所做的是,访问飞解密的字节,并使用我们自己的算法或加密技术进行加密。另一端解密字节并逐字节地分配给SecureString,调用AppendChar函数。
代码访问字节数组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); 
     } 

的现在,对其他流程方面,我相信代码将是简单的解密,并分配给SecureString的。请记住,我们使用了AppendChar函数,以便所有解密的字节不会立即显示或连续显示(减少看到密码的机会)。
例,

SecureString mypassword = new SecureString(); 
for (int i = 0; i < length; ++i) //length of bytes 
{ 
    mypassword.AppendChar ((char) DecryptByte(encryptedByteBuffer[i]));      
} 
+0

嗯。什么是“EncryptByte”和“DecryptByte”?我想我可以使用AES:http://stackoverflow.com/questions/202011/encrypt-decrypt-string-in-net – c00000fd

+0

这些是根据您的需要可能是AES或只是增加你可以改变字节值自定义功能 - 减去可以在握手中交换的一些字节值 – dbw

相关问题