码迷,mamicode.com
首页 > 其他好文 > 详细

如何对ConnectionString进行加密解码?

时间:2014-07-18 16:35:37      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   文件   

这个就不说了就是一个类

    public static class EncryptionConfig
    {
        /*  加密配置节点
         *  Response.Write(EncryptionConfig.Encryption("connectionStrings"));
         *  解密配置节点
         *  Response.Write(EncryptionConfig.Deciphering("connectionStrings"));
         *  不影响读取
            Response.Write(WebConfigurationManager.AppSettings[0].ToString());
            Response.Write(WebConfigurationManager.AppSettings[1].ToString());
         */
        /// <summary>
        /// 加密配置节点
        /// </summary>
        /// <param name="strSection"></param>
        /// <returns></returns>
        public static string Encryption(string strSection)
        {
            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);

            //在打开的配置文件中找到指定的节
            System.Configuration.ConfigurationSection section = config.GetSection(strSection);

            if (section != null && !section.SectionInformation.IsProtected)
            {
                section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
                config.Save();

                return "加密成功";
            }
            else
            {
                return "加密失败";
            }
        }

        /// <summary>
        /// 解密配置节点
        /// </summary>
        /// <param name="strSection"></param>
        /// <returns></returns>
        public static string Deciphering(string strSection)
        {
            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
            System.Configuration.ConfigurationSection section = config.GetSection(strSection);
            if (section != null && section.SectionInformation.IsProtected)
            {
                section.SectionInformation.UnprotectSection();
                config.Save();
                return "解密成功";
            }
            else
            {
                return "解密失败";
            }
        }
    }

如何对ConnectionString进行加密解码?,布布扣,bubuko.com

如何对ConnectionString进行加密解码?

标签:style   blog   http   color   os   文件   

原文地址:http://www.cnblogs.com/Sky-cloudless/p/3851040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!