2015-12-03 39 views
0

我在我的C#代碼中有一個SecureString,我需要傳入一個DLL。我寧願不做編組,因爲看起來發生這種情況時,SecureString是未加密的(因此不再安全)。所以問題是在C++中是否存在C#SecureString等價物,以便我可以將C#代碼中的SecureString傳遞到C++ DLL中,或者如果有更好的/不同的方式,以至於我不需要解密SecureString將它傳遞給DLL。在C++中是否存在C#SecureString等價物?

+1

http://stackoverflow.com/questions/1508082/how-is-a-securestring-marshalled-to-unmanaged-code – Ralf

+0

你只打算能夠派遣,作爲純文本,也許看在使用C#中的ProtectedData.Protect和C++中的CryptUnprotectData時,都使用DPAPI –

+0

@Ralf我特別說我試圖避免編組。 – njbuwm

回答

1

假設您的C++編譯器以公共語言運行時(CLR)爲目標,則可以使用相同的SecureString實現。

using namespace System; 
using namespace System::Security; 

int main(array<System::String ^> ^args) 
{ 
    // Define the string value to assign to a new secure string. 
    Char chars[4] = { 't', 'e', 's', 't' }; 
    // Instantiate the secure string. 
    SecureString^ testString = gcnew SecureString(); 
    // Assign the character array to the secure string. 
    for each (Char ch in chars) 
    { 
     testString->AppendChar(ch); 
    } 
    // Display secure string length. 
    Console::WriteLine("The length of the string is {0} characters.", 
         testString->Length); 

    delete testString; 
    return 0; 
} 
// The example displays the following output: 
//  The length of the string is 4 characters 4 characters 
相關問題