2013-10-12 56 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

相關問題