我想创建一个变量,一个安全的变量,它或多或少是一个在我的代码中使用的 CONST。我已经研究过使用 System.Security.SecureString,看起来它可能是票,因为我不希望用户找到这个密码。唯一的问题是初始化它。在大多数情况下,看起来 SecureString 最好由用户按键“设置”。我不想要这个。我遇到的一个选项如下所示:
unsafe public static void Main()
{
SecureString testString;
// Define the string value to assign to a new secure string.
char[] chars = { 't', 'e', 's', 't' };
// Instantiate a new secure string.
fixed(char* pChars = chars)
{
testString = new SecureString(pChars, chars.Length);
}
// Display secure string length.
Console.WriteLine("The length of the string is {0} characters.",
testString.Length);
}
唯一的问题是,char 数组 't','e','s','t' 可能在编译后仍然打包在内存中。有没有什么好的方法可以在编译时间之前将 SecureString 的值设置为常数值并对该值进行加扰?