首先添加引用: using System
加密操作如下:
PRivate void ProtectSection(string sectionName
{
Configuration config =
WebConfigurationManager
OpenWebConfiguration(Request
ConfigurationSection section = config
if (section != null && !section
{
section
config
}
}
解密操作如下:
private void UnProtectSection(string sectionName)
{
Configuration config =
WebConfigurationManager
OpenWebConfiguration(Request
ConfigurationSection section = config
if (section != null && section
{
section
config
}
}
實踐:
加密前的配置文件:
<?xml version="
<configuration>
<appSettings>
<add key="name" value="shy
<add key="address" value="cnblogs" />
</appSettings>
<system
<compilation debug="true"/>
</system
</configuration>
加密後的配置文件(Web
<?xml version="
<configuration>
<appSettings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>
AQAAANCMnd
BAAAABi
QAAAACAAAAAAADZgAAqAAAABAAA
ABIhxMWlazAntwIIpST
AASAAACgAAAAEAAAAPz/YKYx
b+h
AfsDW
vW
NnHL
UGHLELigrJcr
HGvA
cqGySshx
UsAFkPIYqelYHd
GIbla
hXijKImipwXbDVYy
suYNOhJ
</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>
<system
<compilation debug="true"/>
</system
</configuration>
From:http://tw.wingwit.com/Article/program/net/201311/14027.html