我有一个.Net库,用于访问第三方REST服务。我将安全信息存储在“属性/设置”中。效果很好。问题是,我想确保我的安全信息没有被放置在一个可能被泄露的文本文件中。我查看了application.exe.config文件,但没有看到应用程序设置的部分。没有用于.config的.dll文件。我查看了https://learn.microsoft.com/en-us/visualstudio/ide/managing-application-settings-dotnet?view=vs-2019,它说应用程序设置在库/dll中不起作用。这显然不是事实,因为它正在起作用。有人知道它将库/dll的设置存储在哪里吗?是否有使用.Net框架4.7.2为windows窗体应用程序存储敏感数据的最佳实践?
发布于 2020-03-04 02:25:39
与其他项目一样,dll也可以使用Properties.Settings存储用户数据。

至于存储敏感数据,您可以尝试加密/解密这些数据。然后将加密的字符串存储到Settings中。
以下是您可以参考的加密演示。
// dll
namespace dlltest
{
public class Class1
{
public void Show()
{
Console.WriteLine(Properties.Settings.Default.EncryptedString);
string key = "A123456."; // 8 or 16 characters
DES des = new DES();
Console.WriteLine("1.Encrypt\n2.Decrypt");
string option = Console.ReadLine();
switch (option)
{
// Encrypt
case "1":
Console.WriteLine("Input a string");
string str = Console.ReadLine();
Properties.Settings.Default.EncryptedString = des.DesEncrypt(str, key);
Properties.Settings.Default.Save();
break;
// Decrypt
case "2":
Console.WriteLine(des.DesDecrypt(Properties.Settings.Default.EncryptedString, key));
break;
}
Console.ReadLine();
}
}
public class DES
{
// DES Encrypt
public string DesEncrypt(string pToEncrypt, string sKey)
{
StringBuilder ret = new StringBuilder();
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
}
catch { }
return ret.ToString();
}
// DES Decrypt
public string DesDecrypt(string pToDecrypt, string sKey)
{
MemoryStream ms = new MemoryStream();
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
}
catch { }
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
}
}https://stackoverflow.com/questions/60514298
复制相似问题