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 runat="server">
<div>
<asp:Button ID="btnDPAIP" runat="server" Text="DPAIP加密" />
<br />
<br />
<asp:Button ID="btnRSA" runat="server" Text="RSA加密" />
<br />
<br />
<asp:Button ID="btnResolve" runat="server" Text="完全解密"
/>
</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>
加密一般用在神馬時候呢?
當你把這個程序發布給用戶,發布之前,你需要把你不希望暴露給用戶的信息的節點加密。
最後更新:2017-04-02 22:16:01