首页 > 解决方案 > .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 窗体应用程序存储敏感数据的最佳实践?

标签: c#.netvisual-studio

解决方案


与其他项目一样,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());
        }
    }
}

推荐阅读