如何对ConnectionString进行加密解码?

这个就不说了就是一个类

    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 "解密失败";

            }

        }

    }

你可能感兴趣的:(Connection)