为什么 SecureString 解密在可执行文件之间给出不同的结果?
Why is SecureString decryption giving different results between executables?
在 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 加密是否绑定到可执行文件?
SecureString 的目的是在你的应用程序内存中保持字符串安全(keep the string secure in RAM)
SecureString 对象不可序列化。
您不能在应用程序之间传输实例。
SecureString 使用标志为“0”的 RtlEncryptMemory (WINAPI) 加密字符串(只有相同的进程才能解密内容)。 RtlEncryptMemory API
如果您不想在 RAM 中暴露密码(任何时候),您可以创建一个简单的混淆(或加密)逻辑,然后传输内容。
编辑:
我发现了 2 个可能对您有帮助的旧问题:
When would I need a SecureString in .NET?
Wcf-Authentication and Logging
在 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 加密是否绑定到可执行文件?
SecureString 的目的是在你的应用程序内存中保持字符串安全(keep the string secure in RAM) SecureString 对象不可序列化。 您不能在应用程序之间传输实例。
SecureString 使用标志为“0”的 RtlEncryptMemory (WINAPI) 加密字符串(只有相同的进程才能解密内容)。 RtlEncryptMemory API
如果您不想在 RAM 中暴露密码(任何时候),您可以创建一个简单的混淆(或加密)逻辑,然后传输内容。
编辑:
我发现了 2 个可能对您有帮助的旧问题:
When would I need a SecureString in .NET?
Wcf-Authentication and Logging