关于配置文件的节点内容加密(备忘)
在这里呢,将要介绍一种加密 web.config 文件中节的方法,
就是 DPAPI 也就是使用 DataProtectionConfigurationProvider 来实现,
其实呢,还有一种加密的算法,叫做 RSA 加密算法,
不过在实现上这个 RSA 和 DPAPI 差不多,
所以只要注意看一下代码就 OK 了,
DPAPI 是使用的 Windows Data Provider API 来实现加密和解密的,
其中的 Provider 字符串为 DataProtectionConfigurationProvider,
而 RSA 的 Provider 字符串为 RSAProtectedConfigurationProvider,
对于 RSA ,其在 MSDN Library 中有一个非常详细的例子,
不懂得可以去看一下,
这一次呢,
主要是讲一下如何对 web.config 中的 appSettings 和
connectionStrings 实现加密和解密,
其实呢,这两个在加密和解密的实现上根本没有区别,
您只需要在 web.config 中获取这两个节就可以来加解密了,
还是直接看代码和效果比较实在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | using System; using System.Web.Configuration; using System.Configuration; namespace WebForm { public partial class Demo__1 : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { } //使用 DPAPI 加密 appSettings protected void btnAddApp_Click( object sender, EventArgs e) { //Request.ApplicationPath //获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。 //当前应用程序的虚拟路径。 //开启 Request.ApplicationPath 应用程序所在的 web.config 文件 Configuration config = WebConfigurationManager. OpenWebConfiguration(Request.ApplicationPath); //获取 web.config 中的 appSettings 区块 ConfigurationSection configSection = config.GetSection( "appSettings" ); //如果这个区块还没有被加密 if (!configSection.SectionInformation.IsProtected) { //进行加密操作 configSection.SectionInformation. ProtectSection( "DataProtectionConfigurationProvider" ); //将加密的结果保存回 web.config 文件中 config.Save(); lblMsg.Text = "AppSettings 使用 DPAPI 加密成功!!!" ; } else { lblMsg.Text = "AppSettings " + "已经被 DPAPI 加密了,此次加密操作被取消!!!" ; } } //使用 DPAPI 加密 connectionStrings protected void btnAddCon_Click( object sender, EventArgs e) { //Request.ApplicationPath //获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。 //当前应用程序的虚拟路径。 //开启 Request.ApplicationPath 应用程序所在的 web.config 文件 Configuration config = WebConfigurationManager. OpenWebConfiguration(Request.ApplicationPath); //获取 web.config 中的 appSettings 区块 ConfigurationSection configSection = config.GetSection( "connectionStrings" ); //如果这个区块还没有被加密 if (!configSection.SectionInformation.IsProtected) { //进行加密操作 configSection.SectionInformation. ProtectSection( "DataProtectionConfigurationProvider" ); //将加密的结果保存回 web.config 文件中 config.Save(); lblMsg.Text = "ConnectionStrings 使用 DPAPI 加密成功!!!" ; } else { lblMsg.Text = "ConnectionStrings " + "已经被 DPAPI 加密了,此次加密操作被取消!!!" ; } } //进行解密 protected void btnSub_Click( object sender, EventArgs e) { Configuration config = WebConfigurationManager. OpenWebConfiguration(Request.ApplicationPath); ConfigurationSection configAppSection = config.GetSection( "appSettings" ); if (configAppSection.SectionInformation.IsProtected) { //在解密时,并不需要区分是 DPAPI 加密的还是 RSA 加密的 //其均会自行解密 configAppSection.SectionInformation.UnprotectSection(); config.Save(); lblMsg.Text = "解密成功!!!" ; } else { lblMsg.Text = "该区块暂时还没有被加密,所以无需解密!!!" ; } ConfigurationSection configConSection = config.GetSection( "connectionStrings" ); if (configConSection.SectionInformation.IsProtected) { configConSection.SectionInformation.UnprotectSection(); config.Save(); lblMsg.Text = "解密成功!!!" ; } else { lblMsg.Text = "该区块暂时还没有被加密,所以无需解密!!!" ; } } } } |
以上就是所有的 Code-Behind 了
看截图吧
加密前的 appSettings 和 connectionStrings
对 appSettings 加密后
再在对 appSettings 加密的基础上对 connectionStrings 加密
以上就是对 appSettings 和 connectionStrings
使用 DPAPI 加密后的结果
然后再对 appSettings 和 connectionStrings 解密
以上就是使用 DPAPI 加密的过程了,
实质上还可以使用一种方法,也就是 RSA 加密,
使用这种方式加密其实和 DPAPI 加密方式差不多,
您只需要在加密时,把上面的 Provider 参数字符串由
DataProtectionConfigurationProvider
改为 RSAProtectedConfigurationProvider 就 OK 了,
感兴趣的可以去试试,还有就是推荐一下 MSDN Library 中的那个 Demo,
也蛮好的,自己去找找看吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端