Ine*_*elp 1 c# wcf securestring
在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;
}
Run Code Online (Sandbox Code Playgroud)
然后在main.exe中我使用此函数解密它:
public string convertToUNSecureString(SecureString secstrPassword)
{
IntPtr unmanagedString = IntPtr.Zero;
try
{
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secstrPassword);
return Marshal.PtrToStringUni(unmanagedString);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是返回的字符串是空的,除非我加密main.exe中的初始字符串,然后返回的解密字符串确实是"8charPwd".为什么会这样?SecureString加密是否绑定到可执行文件?
SecureString的目的是将字符串安全保持在应用程序内存中(使字符串在RAM中保持安全)SecureString对象不是可序列化的.您无法在应用程序之间传输实例.
SecureString使用RtlEncryptMemory(WINAPI)加密字符串,标志为"0"(只有相同的进程可以解密内容).RtlEncryptMemory API
如果您不想(在任何时候)在RAM中公开密码,您可以创建一个简单的混淆(或加密)逻辑,然后传输内容.
编辑:
我找到了两个可能对您有帮助的旧问题: