C++中是否存在等价的C#SecureString

Is there a C# SecureString equivalent in C++?

本文关键字:C#SecureString 存在 是否 C++      更新时间:2023-10-16

我的C#代码中有一个SecureString,需要将其传递到DLL中。我宁愿不进行封送,因为当这种情况发生时,SecureString似乎是未加密的(因此不再安全)。所以问题是C++中是否有等价的C#SecureString,这样我就可以将C#代码中的SecureString传递到C++DLL中。。。或者如果有更好/不同的方式,我不需要对SecureString进行解密,就可以将其传递给DLL。

假设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