【问题标题】:.NET: what are my options for decrypting a password in my project .setting file.NET:在我的项目 .setting 文件中解密密码的选项是什么
【发布时间】:2010-11-15 10:56:14
【问题描述】:

在我的 winForm 的 UI 中,我要求用户输入用户名和密码。我将此密码作为文本存储在我的项目的设置(.settings 文件)中。该应用是一个 Visual Studio 插件

在我将密码输入到设置文件之前和之后如何加密和解密?

我真的不需要任何花哨的东西。这只是一个小小的内部应用程序。但我想知道我所有的选择

谢谢 鲍勃

【问题讨论】:

    标签: c# .net vb.net encryption


    【解决方案1】:

    我有自己的服务(写在我的一个项目中),如果需要,您可以使用我的代码 :) 您可以加密/解密字节 [] 和字符串

    //***********************************************************************
    // Assembly         : Common
    // Author           : SERKANH
    // Created          : 09-27-2009
    //
    // Last Modified By : SERKANH
    // Last Modified On : 09-27-2009
    // Description      : 
    //
    // Copyright        : (c) Microsoft. All rights reserved.
    //***********************************************************************
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace Common
    {
        /// <summary>
        /// This class provides 3DES encryption and decryption routines.
        /// 
        /// Serkan Hekimoglu
        /// </summary>
        public class TripleDes
        {
            readonly byte[] _key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
            readonly byte[] _iv = { 8, 7, 6, 5, 4, 3, 2, 1 };
    
            // define the triple des provider
            private readonly TripleDESCryptoServiceProvider _mDes = new TripleDESCryptoServiceProvider();
    
            // define the string handler
            private readonly UTF8Encoding _mUtf8 = new UTF8Encoding();
    
            // define the local property arrays
            private readonly byte[] _mKey;
            private readonly byte[] _mIv;
    
            /// <summary>
            /// Default constructor
            /// </summary>
            public TripleDes()
            {
                _mKey = _key;
                _mIv = _iv;
            }
    
            /// <summary>
            /// Parameterized constructor
            /// </summary>
            /// <param name="key"></param>
            /// <param name="iv"></param>
            public TripleDes(byte[] key, byte[] iv)
            {
                _mKey = key;
                _mIv = iv;
            }
    
            /// <summary>
            /// Encrypts the given byte array input
            /// </summary>
            /// <param name="input">Input value</param>
            /// <returns>Encrypted result</returns>
            public byte[] Encrypt(byte[] input)
            {
                return Transform(input, _mDes.CreateEncryptor(_mKey, _mIv));
            }
    
            /// <summary>
            /// Decrypts the given encrypted byte array input
            /// </summary>
            /// <param name="input">Encrypted byte array input</param>
            /// <returns>Decrypted result</returns>
            public byte[] Decrypt(byte[] input)
            {
                return Transform(input, _mDes.CreateDecryptor(_mKey, _mIv));
            }
    
            /// <summary>
            /// Encrypts the given string input
            /// </summary>
            /// <param name="text">Input value</param>
            /// <returns>Encrypted result</returns>
            public string Encrypt(string text)
            {
                byte[] input = _mUtf8.GetBytes(text);
                byte[] output = Transform(input, _mDes.CreateEncryptor(_mKey, _mIv));
                return Convert.ToBase64String(output);
            }
    
            /// <summary>
            /// Decrypts the given encrypted string input
            /// </summary>
            /// <param name="text">Encrypted string input</param>
            /// <returns>Decrypted result</returns>
            public string Decrypt(string text)
            {
                byte[] input = Convert.FromBase64String(text);
                byte[] output = Transform(input, _mDes.CreateDecryptor(_mKey, _mIv));
                return _mUtf8.GetString(output);
            }
    
            private static byte[] Transform(byte[] input, ICryptoTransform cryptoTransform)
            {
                // create the necessary streams
                using (MemoryStream memStream = new MemoryStream())
                {
                    using (CryptoStream cryptStream = new CryptoStream(memStream, cryptoTransform, CryptoStreamMode.Write))
                    {
                        // transform the bytes as requested
                        cryptStream.Write(input, 0, input.Length);
                        cryptStream.FlushFinalBlock();
                        // Read the memory stream andconvert it back into byte array
                        memStream.Position = 0;
                        byte[] result = memStream.ToArray();
                        // close and release the streams
                        memStream.Close();
                        cryptStream.Close();
                        // hand back the encrypted buffer
                        return result;
                    }
                }
            }
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我是这样写的:

          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);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-05
        • 2010-12-13
        • 2014-03-16
        • 2020-12-18
        • 2010-09-17
        相关资源
        最近更新 更多