<configProtectedData defaultProvider="RsaProtectedConfigurationProvider">
<providers>
<add name="MyRsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" keyContainerName="NetFrameworkConfigurationKey" cspProviderName="" useMachineContainer="true" useOAEP="false"/>
<add name="DataProtectionConfigurationProvider" type="System.Configuration.DpapiProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses CryptProtectData and CryptUnProtectData Windows APIs to encrypt and decrypt" useMachineProtection="true" keyEntropy=""/>
</providers>
</configProtectedData>
<providers>
<add name="MyRsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" keyContainerName="NetFrameworkConfigurationKey" cspProviderName="" useMachineContainer="true" useOAEP="false"/>
<add name="DataProtectionConfigurationProvider" type="System.Configuration.DpapiProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses CryptProtectData and CryptUnProtectData Windows APIs to encrypt and decrypt" useMachineProtection="true" keyEntropy=""/>
</providers>
</configProtectedData>
有两种加密的方法
1. DataProtectionConfigurationProvider 使用windows内置的密码学技术加密的
2. RsaProtectedConfigurationProvider 使用RSA公钥加解密
以下为使用实例:
Configuration config;
config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection section;//要处理的对象
section = config.GetSection("connectionStrings") as ConnectionStringsSection;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
//注:解密会自动执行(两种加密同一方法解密)
config.Save();
}
else
{
section.SectionInformation.ProtectSection(
this.DropDownList1.SelectedItem.Text
);//使用Dropdownlist中用户指定的provider进行加密 "DataProtectionConfigurationProvider"&"RsaProtectedConfigurationProvider"
config.Save();
}
config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection section;//要处理的对象
section = config.GetSection("connectionStrings") as ConnectionStringsSection;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
//注:解密会自动执行(两种加密同一方法解密)
config.Save();
}
else
{
section.SectionInformation.ProtectSection(
this.DropDownList1.SelectedItem.Text
);//使用Dropdownlist中用户指定的provider进行加密 "DataProtectionConfigurationProvider"&"RsaProtectedConfigurationProvider"
config.Save();
}