說一說ASP.NET web.config 加密及解密方法 (代碼)

來源:互聯網
上載者:User

標籤:des   os   io   art   cti   代碼   ar   new   

  1. /// <summary>  
  2.    /// 保護web.config的加密和解密  
  3.    /// </summary>  
  4.    public class ProtectHelper  
  5.    {  
  6.        /// <summary>  
  7.        /// 解密  
  8.        /// </summary>  
  9.        /// <param name="pToDecrypt">加密連接字串</param>  
  10.        /// <param name="sKey">自訂密鑰</param>  
  11.        /// <returns>解密字串</returns>  
  12.        public static string UnProtectSection(string pToDecrypt, string sKey)  
  13.        {  
  14.            byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);  
  15.            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())  
  16.            {  
  17.                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
  18.                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  
  19.                System.IO.MemoryStream ms = new System.IO.MemoryStream();  
  20.                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))  
  21.                {  
  22.                    cs.Write(inputByteArray, 0, inputByteArray.Length);  
  23.                    cs.FlushFinalBlock();  
  24.                    cs.Close();  
  25.                }  
  26.                string str = Encoding.UTF8.GetString(ms.ToArray());  
  27.                ms.Close();  
  28.                return str;  
  29.            }  
  30.        }  
  31.   
  32.        /// <summary>  
  33.        /// 加密  
  34.        /// </summary>  
  35.        /// <param name="pToEncrypt">連接字串</param>  
  36.        /// <param name="sKey">自訂密鑰</param>  
  37.        /// <returns>加密字串</returns>  
  38.        public static string ProtectSection(string pToEncrypt, string sKey)  
  39.        {  
  40.            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())  
  41.            {  
  42.                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);  
  43.                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
  44.                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  
  45.                System.IO.MemoryStream ms = new System.IO.MemoryStream();  
  46.                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))  
  47.                {  
  48.                    cs.Write(inputByteArray, 0, inputByteArray.Length);  
  49.                    cs.FlushFinalBlock();  
  50.                    cs.Close();  
  51.                }  
  52.                string str = Convert.ToBase64String(ms.ToArray());  
  53.                ms.Close();  
  54.                return str;  
  55.            }  
  56.        }  
  57.    }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.