问题
I have a SecureString in my C# code that I need to pass into a DLL. I would prefer to not do a Marshalling as it seems that when this occurs the SecureString is unencrypted (and hence not secure anymore). So the question is whether or not there is a C# SecureString equivalent in C++ so that I can pass the SecureString from my C# code into the C++ DLL ... or if there is a better/different way such that I do not need to unencrypt the SecureString to pass it to the DLL.
回答1:
Assuming your C++ compiler target the Common Language Runtime (CLR), you can use the same SecureString
implementation.
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
来源:https://stackoverflow.com/questions/34067372/is-there-a-c-sharp-securestring-equivalent-in-c