导航:首页 > 文档加密 > gs5加密封条

gs5加密封条

发布时间:2022-07-11 22:12:59

1. 如何为配置文件加密

在web.config或app.config文件里我们经常会存储一些敏感信息,比如connectionStrings或者appSettings,比如像下面的文件。
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="MyNwConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;"/>
</connectionStrings>
<appSettings>
<add key="User" value="myUsername"/>
<add key="Password" value="myPassword"/>
</appSettings>
</configuration>
using System;
using System.Configuration;

namespace WebConfigEncryptTest
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string user = ConfigurationManager.AppSettings.Get("User");
string password = ConfigurationManager.AppSettings.Get("Password");
string connectionString = ConfigurationManager.ConnectionStrings["MyNwConnectionString"].ConnectionString;
}
}
}

(一)加密文件可以使用的Provider
.NET为我们提供了一个工具aspnet_regiis.exe来对web.config文件中的敏感信息进行加密(app.config文件可以先改名为web.config,加密后再改回app.config)。你可以使用两个provider中的一个来进行加密:
System.Configuration.:在System.Configuration.dll中,使用Windows DPAPI(Data Protection API)来进行加密,密钥存在Windows Local Security Authority(LSA)中。注意:当使用时,加密文件所使用的帐号需要与运行web application的帐号相同,否则web application无法解密加密的内容。
System.Configuration.:在System.Configuration.dll中,使用RSA算法来进行加密(RSA算法是非对称加密,参见《对称加密与非对称加密 》),公钥存放在config文件当中,只有加密的计算机有密钥。通常是默认的缺省provider。
(二)加密文件的命令
加密web.config文件可以使用:
aspnet_regiis -pef section web-app-physical-dir
Encrypt the configuration section. Optional arguments:
[-prov provider] Use this provider to encrypt.

比如运行下面的命令就会分别对connectionStrings和appSettings中的信息进行加密:

aspnet_regiis.exe -pef "connectionStrings" "C:\myweb\HelloService"
aspnet_regiis.exe -pef "appSettings" "C:\myweb\HelloService"

加密后的web.config文件变成:

<?xml version="1.0"?>
<configuration>
<system.web>
<compilation targetFramework="4.0" />
</system.web>
<connectionStrings configProtectionProvider="">
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>Rsa Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>E2fO9C0TJVxImLYQZza+fCQdDbTpNh/kOKLRsK6zcFjkgtUCl6SnMViuu/2G1NVTxqXyEWYwyK6AiCZA+feeG/+f2EIimP7LJI+JRZVerI4MU6Ke3wxm2S/ATc73/W6eg9808f4//JB0kso0kGJ9i+==</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>I1DWG11Iz/rq+NC9C/21B3Q22J9+//JW1oCvAGs5tHrZU5+vgvm0yCmSuCWZbXva+iv9J35EQqs58pq+hwVo1hg1dffpGCBykaXGl5VX3TIGc=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
<appSettings configProtectionProvider="">
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>Rsa Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>//SuBvV3D2kxhHaYGFaPuvYgsyOLf3+aYR3O/uh/+/iSANzAWoC+==</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>5W2KhG/oETLUDptobcOM52x1qD/g9A0By/wcGXI+///w=</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>
</configuration>

是默认的缺省provider,如果想使用,可以用-prov参数指明:

aspnet_regiis.exe -pef "connectionStrings" "C:\myweb\HelloService" -prov ""
aspnet_regiis.exe -pef "appSettings" "C:\myweb\HelloService" -prov ""

加密配置文件后,源程序不需要做任何改动。如果要修改或添加新的配置信息,需要先解密配置文件。不论使用哪种Provider,都只能在进行加密的计算机上对配置文件进行解密。

阅读全文

与gs5加密封条相关的资料

热点内容
交易平台小程序源码下载 浏览:148
程序员记笔记用什么app免费的 浏览:646
java与单片机 浏览:897
服务器内网如何通过公网映射 浏览:478
程序员穿越到宋代 浏览:624
怎么使用云服务器挂游戏 浏览:618
真实的幸福pdf 浏览:344
d盘php调用c盘的mysql 浏览:266
怎么样搭建源码网站 浏览:429
新概念四册pdf 浏览:363
怎么下载悦虎检测app 浏览:530
cad表达式命令 浏览:200
程序员去一个小公司值不值得 浏览:848
程序员做个程序多少钱 浏览:497
win10原始解压软件 浏览:321
阿里程序员的老家 浏览:260
量子加密银行 浏览:195
命令方块获得指令手机 浏览:501
学习结束感言简短程序员 浏览:400
android关机闹钟实现 浏览:970