。NET库中属性的存储。应用程序范围的设置

问题描述 投票:0回答:1

我有一个用于访问第三方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# .net visual-studio
1个回答
1
投票

与其他项目一样,dll也可以使用Properties.Settings存储用户数据。

enter image description here

关于存储敏感数据,您可以尝试加密/解密数据。然后将加密的字符串存储到“设置”中。

以下是您可以参考的加密演示。

// 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());
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.