First add a reference: using System.Web.Configuration;
The
encryption operation is as follows:
PRivate void ProtectSection (String sectionname, string provider)
{
Configuration config =
WebConfigurationManager.
openwebconfiguration (Request.applicationpath);
configurationsection section = config. GetSection (sectionname);
if (section!= null &&!section. sectioninformation.isprotected)
{
section. Sectioninformation.protectsection (provider);
CONFIG. Save ();
}
}
The
decryption operation is as follows:
private void Unprotectsection (string sectionname)
{
Configuration config =
WebConfigurationManager.
openwebconfiguration (Request.applicationpath);
ConfigurationSection section = Config. GetSection (sectionname);
if (section!= null && section. sectioninformation.isprotected)
{
section. Sectioninformation.unprotectsection ();
CONFIG. Save ();
}
}
Practice:
configuration file before encryption:
<?xml version= "1.0"?>
<configuration>
<appSettings>
<add key= "name" value= "shy520"/>
<add key= "Address" value= "Cnblogs"/>
</appSettings>
<system.web>
<compilation debug= "true"/>
</system.web>
</configuration>
encrypted configuration file (web.config) :
<?xml version= "1.0"?>
<configuration>
<appsettings configprotectionprovider= "DataProtectionConfigurationProvider" >
<EncryptedData>
<CipherData>
<CipherValue>
Aqaaancmnd8bfderjhoawe/cl+s
Baaaabi1atlnkeugef0xywgl2xg
qaaaacaaaaaaadzgaaqaaaabaaa
abihxmwlazantwiipst1cdxaaaa
aasaaacgaaaaeaaaapz/ykyx07c
b+h4fqdr4fklgaaaax1ieyc+wsx
Afsdw1vn2c/fxsg2tanyeugacov
8e3ngfthhsh91gliqkregfpyzgr
Vw1xrez/3vwomji9es7efkrcxej
Nnhl66kg2inrk3ntlnzlztts3cz
9W63U47VKAJS6MIWSGBZ2GNTL/9
UGHLELIGRJCR3YJ+LSJOSCEXQNV
HGVA48EFXPD+TEIFBTGXEHSFKQX
CQGYSSHX16VCT2GUNUC3ZMEAHBH
USAFKPIYQELYHD4+M9A/XPE2TQW
GIBLA1WBW2NDEFRZJPWNKFMPNQR
hxijkimipwxbdvyy6o0uaaaas8d
suynohj7qajja2c/4euc7sks=
</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>
<system.web>
<compilation debug= "true"/>
</system.web>
</configuration>