• 说一说ASP.NET web.config 加密及解密方法 (代码)


    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.    }  
  • 相关阅读:
    linux之卸载软件
    linux之挂载硬盘
    windows MySQL 5+ 服务手动安装
    深刻理解Python中的元类(metaclass)
    Python_cmd的各种实现方法及优劣(subprocess.Popen, os.system和commands.getstatusoutput)
    WSGI、flup、fastcgi、web.py的关系
    Windows下python环境变量配置
    External file changes sync may be slow: Project files cannot be watched (are they under network mount?)
    as。 对象和数组
    为什么for不能有序遍历数组的所有元素?(Array的设计原理)
  • 原文地址:https://www.cnblogs.com/ranran/p/3875278.html
Copyright © 2020-2023  润新知