.NET: what are my options for decrypting a password in my project .setting file

前端 未结 2 1097
无人共我
无人共我 2021-01-07 11:30

In the UI of my winForm I ask the user for a user name and password. I store this password as text in the Settings (the .settings file) of my project. The app is a visual St

2条回答
  •  星月不相逢
    2021-01-07 12:14

    I dd it with this:

        public static string Decrypt(string stringToDecrypt)
        {
            UnicodeEncoding byteConverter = new UnicodeEncoding();
            byte[] dataToEncrypt = byteConverter.GetBytes(stringToDecrypt);
            byte[] decryptedData = null;
            try
            {
                using (RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider())
                {
                    rsaCryptoServiceProvider.FromXmlString(_key);
                    byte[] decryptBytes = Encoding.Default.GetBytes(Properties.Settings.Default.SqlPassword);
    
                    decryptedData = rsaCryptoServiceProvider.Decrypt(decryptBytes, false);
                }
            }
            catch (Exception ex)
            {
                //TODO Do proper logging
                Console.WriteLine("Decrypt failed: " + ex.Message);
            }
    
            return byteConverter.GetString(decryptedData);
        }
    
        public static string Encrypt(string stringToEncrypt)
        {
            try
            {
                UnicodeEncoding byteConverter = new UnicodeEncoding();
                byte[] dataToEncrypt = byteConverter.GetBytes(stringToEncrypt);
    
                using (RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider())
                {
                    rsaCryptoServiceProvider.FromXmlString(_key);
                    byte[] encryptedData = rsaCryptoServiceProvider.Encrypt(dataToEncrypt, false);
    
                    return Encoding.Default.GetString(encryptedData);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Encrypt failed: " + ex.Message);
            }
        }
    

提交回复
热议问题