zoukankan      html  css  js  c++  java
  • C# 对 App.config的appSettings节点数据进行加密

    .NET平台下的Winform和Asp.net的配置文件默认都是明文保存的,本文使用的是.Net自身如何加密配置文件,不包含自定义的加密规则
    但.Net是提供了直接对配置文件加密的功能的,使用.Net加密的配置节在读取时不需要手动解密.Net会自行解密并返回解密后的数据
    加密后的数据会保存到一个单独的配置节点里(不管加密的节点下有多少子项,加密后的数据都在CipherValue 里)
    .Net是按照节点来进行加密的,所以如果给像appSettings这样的节点加密
    那么该节点下面的所有数据都会加密(单独的Key进行加密可以自己Code实现,不太清楚.Net本身是否能只加密节点下某N个Key)
    加密后的数据及节点:
    <EncryptedData>
        <CipherData>
          <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAABbLHX[...]</CipherValue>
        </CipherData>
      </EncryptedData>
    using System;
    using System.Configuration;
    using System.Collections.Generic;
    namespace HTL{
        static class Program{
            /// <summary>
            /// 健值集合,用于保存数据到配置文件
            /// </summary>
            static Dictionary<string, string> key_value = new Dictionary<string, string>();
            [STAThread]
            public static void Main(string[] args)
            {
                key_value.Add("encrypt","加密");
                key_value.Add("test","测试");
                
                //加密AppSettings节点
                AddConfig(key_value,true);
                try    {
                    Console.WriteLine("加密'test'的数据:"+System.Configuration.ConfigurationManager.AppSettings["test"]);
                }
                catch(Exception e)
                {
                    Console.Write("读取AppSettings节点数据失败.错误信息:
    "+e.Message );
                }      
                //不加密AppSettings节点,如果不加密在前,加密在后则会出现错误“无法识别的属性“configProtectionProvider”。请注意属性名称区分大小写。”
                            AddConfig(key_value,false);
                try    {
                    Console.WriteLine("未加密'test'的数据:"+System.Configuration.ConfigurationManager.AppSettings["test"]);
                }
                catch(Exception e)
                {
                    Console.Write("读取AppSettings节点数据失败.错误信息:
    "+e.Message );
                }              
                Console.ReadKey();
            }
            /// <summary>
            /// 对appSettings节点添加健值
            /// 如果健已经存在则更改值
            /// 添加后重新保存并刷新该节点
            /// </summary>
            /// <param name="dict">添加的健值集合</param>
            /// <param name="isProtected">是否加密appSettings节点数据,如果为TrueappSettings节点下所有数据都会被加密</param>
            public static void AddConfig(System.Collections.Generic.Dictionary<string, string> dict, bool isProtected)
            {
                if (dict == null || dict.Count <= 0) return;
                System.Configuration.Configuration configuration =System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                #region  //循环添加或更改健值
                foreach (System.Collections.Generic.KeyValuePair<string, string> key_value in dict)
                {
                    if (string.IsNullOrEmpty(key_value.Key)) continue;
                    if ( configuration.AppSettings.Settings[key_value.Key]!=null)
                        configuration.AppSettings.Settings[key_value.Key].Value = key_value.Value;
                    else
                        configuration.AppSettings.Settings.Add(key_value.Key, key_value.Value);
                }//end foreach
                #endregion
                #region//保存配置文件
                try
                {
                    //加密配置信息
                    if (isProtected && !configuration.AppSettings.SectionInformation.IsProtected)
                    {
                        configuration.AppSettings.SectionInformation.ForceSave = true;
                        configuration.AppSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                    }
                    configuration.Save();
                }
                catch (Exception)
                {
                    throw;
                }
                ConfigurationManager.RefreshSection("appSettings");   
            ConfigurationManager.RefreshSection("configuration");
               }
           #endregion
            }
    }  

    复制代码
    using System;
    using System.Configuration;
    using System.Collections.Generic;
    namespace HTL{
        static class Program{
            /// <summary>
            /// 健值集合,用于保存数据到配置文件
            /// </summary>
            static Dictionary<string, string> key_value = new Dictionary<string, string>();
            [STAThread]
            public static void Main(string[] args)
            {
                key_value.Add("encrypt","加密");
                key_value.Add("test","测试");
                
                //加密AppSettings节点
                AddConfig(key_value,true);
                try    {
                    Console.WriteLine("加密'test'的数据:"+System.Configuration.ConfigurationManager.AppSettings["test"]);
                }
                catch(Exception e)
                {
                    Console.Write("读取AppSettings节点数据失败.错误信息:
    "+e.Message );
                }      
                //不加密AppSettings节点,如果不加密在前,加密在后则会出现错误“无法识别的属性“configProtectionProvider”。请注意属性名称区分大小写。”
                            AddConfig(key_value,false);
                try    {
                    Console.WriteLine("未加密'test'的数据:"+System.Configuration.ConfigurationManager.AppSettings["test"]);
                }
                catch(Exception e)
                {
                    Console.Write("读取AppSettings节点数据失败.错误信息:
    "+e.Message );
                }              
                Console.ReadKey();
            }
            /// <summary>
            /// 对appSettings节点添加健值
            /// 如果健已经存在则更改值
            /// 添加后重新保存并刷新该节点
            /// </summary>
            /// <param name="dict">添加的健值集合</param>
            /// <param name="isProtected">是否加密appSettings节点数据,如果为TrueappSettings节点下所有数据都会被加密</param>
            public static void AddConfig(System.Collections.Generic.Dictionary<string, string> dict, bool isProtected)
            {
                if (dict == null || dict.Count <= 0) return;
                System.Configuration.Configuration configuration =System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                #region  //循环添加或更改健值
                foreach (System.Collections.Generic.KeyValuePair<string, string> key_value in dict)
                {
                    if (string.IsNullOrEmpty(key_value.Key)) continue;
                    if ( configuration.AppSettings.Settings[key_value.Key]!=null)
                        configuration.AppSettings.Settings[key_value.Key].Value = key_value.Value;
                    else
                        configuration.AppSettings.Settings.Add(key_value.Key, key_value.Value);
                }//end foreach
                #endregion
                #region//保存配置文件
                try
                {
                    //加密配置信息
                    if (isProtected && !configuration.AppSettings.SectionInformation.IsProtected)
                    {
                        configuration.AppSettings.SectionInformation.ForceSave = true;
                        configuration.AppSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                    }
                    configuration.Save();
                }
                catch (Exception)
                {
                    throw;
                }
                ConfigurationManager.RefreshSection("appSettings");   
            ConfigurationManager.RefreshSection("configuration");
               }
           #endregion
            }
    }  
    复制代码

    AddConfig(key_value,true);//加密AppSettings节点
    加密在前,不加密在后
    不加密在前,加密在后出错的错误
    配置文件加密参考:
     
    无法识别的属性 configProtectionProvider参考(都未解决):
  • 相关阅读:
    linux拷贝软连接文件
    【知识点】Java常用类库
    Maven之pom.xml配置文件详解
    Java+Bigdata学习路线
    Hadoop界的Hello World!
    JavaEE三大框架的整合
    学完微型服务器(Tomcat)对其工作流程的理解,自己着手写个简单的tomcat
    【数据结构与算法】之链表
    JavaEE项目开发所需要的包(Struts2+Spring5+Hibernate5)
    在Linux(Centos7)系统上对进行Hadoop分布式配置以及运行Hadoop伪分布式实例
  • 原文地址:https://www.cnblogs.com/wanzhongjun/p/6534617.html
Copyright © 2011-2022 走看看