c# - .Net 库中应用程序范围的属性的存储。
问题描述
我有一个用于访问第三方 REST 服务的 .Net 库。我将安全信息存储在属性/设置中。它工作得很好。问题是,我想确保我的安全信息没有放在可能被泄露的文本文件中。我查看了 application.exe.config 文件,但没有看到应用程序设置部分。.dll 没有 .config 文件。我查看了https://docs.microsoft.com/en-us/visualstudio/ide/managing-application-settings-dotnet?view=vs-2019它说应用程序设置在库/dll中不起作用。这显然不是这种情况,因为它正在工作。有谁知道它在哪里存储库/dll 的设置?是否有使用 .Net Framework 4.7.2 为 Windows 窗体应用程序存储敏感数据的最佳实践?
解决方案
与其他项目一样,dll 也可以使用 Properties.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());
}
}
}
推荐阅读
- php - 创建 Veloconnect 服务器(使用 PHP),尝试从 xsd-schemas 生成 xml 文件
- java - 如何检查字符串是否包含没有正则表达式的拉丁字母
- javascript - 我如何解决 Javascript Fetch 的问题
- javascript - 如何根据点条件动态启用/禁用系列工具提示
- reactjs - 来自 DRF 的图像未在反应 js 中加载
- git - 为什么window git Bash中只有两个PS命令?如图所示
- python - 使用 Python SDK 和 Azure Functions 将元数据添加到 Azure Blob
- mongodb - Mongoose - 子文档数组问题
- java - IntelliJ 中的 mvn spring-boot:run 和“run”有什么区别
- javascript - 如何访问对象内的图像链接并使用反应组件显示它