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

ぃ、小莉子 提交于 2019-12-24 14:13:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!