博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET 配置文件加密
阅读量:5340 次
发布时间:2019-06-15

本文共 5154 字,大约阅读时间需要 17 分钟。

众所周知,web.config可以存储包括数据库链接在内的众多信息,所以为配置文件加密有时候就显得必要了。

1.加密前的配置文件如下:

 

Web.config
1 
2 3
4
5
7
8
9
10
11
12 13
14
15
16
18
19 20
21
22
23

2.加密

(1)前端源代码

 

ConfigurationEncryption.aspx
1  
2
3
4
5
connectionStrings
6
appSettings
7
system.web/machineKey
8
system.web/sessionState
9
10
11
12
14
16
17

 

(2)后台代码

 

1  private const string provider = "RSAProtectedConfigurationProvider";  //Use RSA Provider to encrypt configuration sections  2  3         protected void Page_Load(object sender, EventArgs e)  4         {
5 6 } 7 8 protected void btnEncrypt_Click(object sender, EventArgs e) 9 {
10 if (string.IsNullOrEmpty(this.ddlSection.SelectedValue)) 11 {
12 Response.Write("please select a configuration section"); 13 //选择要加密的配置节点 14 return; 15 } 16 //获取加密的配置节点 17 string sectionString = this.ddlSection.SelectedValue; 18 //使用指定的虚拟路径将 Web 应用程序配置文件作为 System.Configuration.Configuration 对象打开以允许读或写操作。 19 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 20 ConfigurationSection section = config.GetSection(sectionString); 21 if (section != null) 22 { //标记配置节点,进行保护 23 section.SectionInformation.ProtectSection(provider); 24 config.Save(); 25 Response.Write("encrypt successed, please check the configuration file."); 26 } 27 } 28 29 protected void btnDecrypt_Click(object sender, EventArgs e) 30 {
31 string sectionString = this.ddlSection.SelectedValue; 32 33 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 34 ConfigurationSection section = config.GetSection(sectionString); 35 if (section != null && section.SectionInformation.IsProtected) 36 {
37 // 从关联的配置节中移除受保护的配置加密。 38 section.SectionInformation.UnprotectSection(); 39 config.Save(); 40 Response.Write("decrypt success, please check the configuration file."); 41 } 42 43 }

(3)加密后的效果

Rsa Key
im7ZLO/++t/Iu7r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ocEmI6GeoCG6SgIjPukMhY6jieo1LVbuEPb75g==
Rsa Key
iBNJ6+10cIZr9Cj9KJ8KEpRrQe2L/4KiIR9Vg51NyFURwhYZd4kjV0TeuALhUqtlpDImp4kCg+3X/FKp1dob8DiILTcLYY68JDIcUUgyVVwwijwqVTcDvEucmB+vRQAN7u3ivPrEdDpihjqfOh7GxODjB/PfLD5MwrAVxQAeqL0=
Zb9wsQ+U9yPLlduYe7cQtzsXGF9do97y4iGCULOffGU+WgnIUAqVgOjrhTuNSiDnb9MYHXircQIrtkV9mqrqKvR++169uOrqtakZ3oK8ht8G8Qe2hhXv1EmNttGZvVOfsZmShlPXslWfOPDeHDV1oqhe/JEANw41RQ3KXot8eXCZjkdf/xkA5rkdixvtlmh9TleWTbnTq6LzrWwXUZXAR9UXybNE2ijG6ar+5a+05R6sgXEdgqNuV7RoFcbYba/zLffLkTBZ1YNvAbEfekuJTn018zOOX4x6v2bB2X3i7MsW9moROs0ld3UL0YOuqylS

 

转载于:https://www.cnblogs.com/January/archive/2011/11/23/2260883.html

你可能感兴趣的文章
sonar结合jenkins
查看>>
解决VS+QT无法生成moc文件的问题
查看>>
AngularJs练习Demo14自定义服务
查看>>
stat filename
查看>>
关于空想X
查看>>
CF1067C Knights 构造
查看>>
【算法•日更•第三十二期】教你用出windows体验的Linux
查看>>
[BZOJ2938] 病毒
查看>>
webstorm修改文件,webpack-dev-server不会自动编译刷新
查看>>
[NOIP2009] 靶形数独
查看>>
【Python网络编程】利用Python进行TCP、UDP套接字编程
查看>>
在百度和博客园发表地文章被爬索的速度比较
查看>>
Scikit-learn 库的使用
查看>>
CSS: caption-side 属性
查看>>
python 用数组实现队列
查看>>
认证和授权(Authentication和Authorization)
查看>>
Mac上安装Tomcat
查看>>
CSS3中box-sizing的理解
查看>>
传统企业-全渠道营销解决方案-1
查看>>
Lucene全文检索
查看>>