在proxy.exe我通过以下方式创建一个安全字符串:
public SecureString GetSecureEncryptionKey()
{
string strPassword = "8charPwd";
SecureString secureStr = new SecureString();
if (strPassword.Length > 0)
{
foreach (var c in strPassword.ToCharArray()) secureStr.AppendChar(c);
}
return secureStr;
}
然后在main.exe我使用这个函数解密它:
public string convertToUNSecureString(SecureString secstrPassword)
{
IntPtr unmanagedString = IntPtr.Zero;
try
{
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secstrPassword);
return Marshal.PtrToStringUni(unmanagedString);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
问题是返回的字符串是空的,除非我在main.exe中加密了初始字符串,否则返回的解密字符串确实是“8charPwd”。为什么会这样?SecureString 加密是否绑定到可执行文件?