webconfig加密

简介:
 

.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Configuration;

namespace WebConfig加密
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        Configuration myConfiguration = null;
        ConfigurationSection myAppSettings = null;

        // DPAIP加密(用的最多)
        protected void btnDPAIP_Click(object sender, EventArgs e)
        {
            try
            {
                getAppSettings(out myConfiguration, out myAppSettings);

                if (!myAppSettings.SectionInformation.IsProtected)
                {
                    //DPAPI加密
                    myAppSettings.SectionInformation.ProtectSection

                                ("DataProtectionConfigurationProvider");
                   
                    //储存设定写入web.config文件
                    myConfiguration.Save();
                    Response.Write("用DPAIP加密成功");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }

        // RSA加密(需要设置权限,比较麻烦,用的不多)
        protected void btnRSA_Click(object sender, EventArgs e)
        {
            try
            {
                getAppSettings(out myConfiguration, out myAppSettings);

                if (!myAppSettings.SectionInformation.IsProtected)
                {
                    //RSA加密
                    myAppSettings.SectionInformation.ProtectSection

                                  ("RsaProtectedConfigurationProvider");

                    //储存设定写入web.config文件
                    myConfiguration.Save();
                    Response.Write("以RSA加密成功!");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }

        //取得取得Web.config中appSettings设定区段(还可以根据需要,设置需要加密的节点)
        protected void getAppSettings(out Configuration myConfig, out ConfigurationSection

                                     appSettings)
        {
            //开启Request所在路径网站的Web.config文件
            myConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
           
            //取得Web.config中appSettings设定区段
            appSettings = myConfig.GetSection("appSettings");
        }

        // 完全解密
        protected void btnResolve_Click(object sender, EventArgs e)
        {
            try
            {
                getAppSettings(out myConfiguration, out myAppSettings);
                if (myAppSettings.SectionInformation.IsProtected)
                {
                    myAppSettings.SectionInformation.UnprotectSection(); //解密
                    myConfiguration.Save();
                }
                Response.Write("appSettings解密成功!");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }
    }
}

 

.aspx

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnDPAIP" runat="server" Text="DPAIP加密" onclick="btnDPAIP_Click" />
        <br />
        <br />
        <asp:Button ID="btnRSA" runat="server" Text="RSA加密" onclick="btnRSA_Click" />
        <br />
        <br />
        <asp:Button ID="btnResolve" runat="server" Text="完全解密"
            onclick="btnResolve_Click" />
    </div>
    </form>
</body>





加密前

  <appSettings>
    <add key="con" value="data source=.\SQLEXPRESS;Integrated

    Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" />
  </appSettings>

 

加密结果

  <appSettings configProtectionProvider="DataProtectionConfigurationProvider">
    <EncryptedData>
      <CipherData>
       <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAzCJXt/1660evq+/58WwHfAQAAAACAAAAAAADZgAAqAAAABAAAABa4SlZnvwdFhVlRr9PuT3hAAAAAASAAACgAAAAEAAAAEDgN4H/IpjLojCaYhMXkudgAQAA1NHa7mkrBWMGqXH9nmGi8Ie1Mnuh1iD8hXaAzZ8/4UnzAwIyJBvLHln/Kv+LatS/w8hLkTR/GbnIkYhzeuk/ER1m76VUzuhRY7KcwdXkZkOxelEjWZU/jA7wcvgyEN7OkRyhV0nz98zHI+XdxQkFsLEltNacCqBx3PgCkX+sKz1hyzp06D0QQOIbqoaNSWl/QuBAIxlZohAKaTxAQcnKjrOuBofp49N4OCbDFdoIFMfWaoCSfQV0xQUEVRCkBzzd/FGVjrSYeLgk9CM9vdSnioLUDIMv62dxqEYM/0dRd4qhYTghIzWwe/POKR8IxUC++zRT/kEKh5cTw8OppW+mU1+6oqul98jxxk//UJ/HyVEBZ8XAVhetcSjUH1eXyzBcup03L8V+WnEmqAwzoibMpDmTkXIEitSZRJ/8Fy26hByUJkslQFYdtLgZ92OCLfSVCW3etWXDtMpD7cfJuMP6XBQAAAAYjlMjewxEJfZKD64skP+Lnh5x6w==</CipherValue>
      </CipherData>
    </EncryptedData>
  </appSettings>

 

加密一般用在神马时候呢?

 

当你把这个程序发布给用户,发布之前,你需要把你不希望暴露给用户的信息的节点加密。

目录
相关文章
|
9月前
|
数据可视化 搜索推荐 数据挖掘
评价:这 6 款软件在电商团队物流协作中表现如何?
在电商行业,高效的团队协作至关重要。本文推荐六款提升团队协作效率的软件:板栗看板、Trello、Asana、Wrike、Monday.com 和 Basecamp。这些软件各具特色,如板栗看板的直观可视化界面和灵活任务管理,Trello 的卡片式任务管理和丰富插件,Asana 的任务层级清晰和自定义工作流程,Wrike 的动态时间跟踪和智能任务分配,Monday.com 的多样化视图模式和自动化工作流程,以及 Basecamp 的集中式信息管理和群组讨论功能。选择合适的软件,能显著提升电商团队的协作效率。
129 6
|
10月前
|
API
如果API调用失败,我应该如何排查问题?
当小红书API调用失败时,可按以下步骤排查:1. 检查请求参数;2. 确认身份验证凭据;3. 控制调用频率;4. 检查网络连接;5. 查看错误码和日志;6. 核实授权范围;7. 联系技术支持;8. 定期更新与测试。这些方法有助于系统地解决问题,确保API调用稳定。
|
消息中间件 存储 Kafka
RabbitMQ、Kafka和RocketMQ比较
RabbitMQ、Kafka和RocketMQ比较
1372 0
|
云安全 监控 安全
干货帖 | 阿里云云安全ACP认证考试攻略来袭…
号外!号外!云安全专业认证考试的经验分享来啦,除了烧高香,拜大佛,尊考神,我们还要做些什么实实在在的准备呢?我们来看一看~
7908 0
|
关系型数据库 分布式数据库 数据库
|
数据可视化 druid 数据挖掘