ASP.NET 2.0 引入 `
aspnet_regiis -pc "MyKeys" -exp
<configuration>
<configProtectedData>
<providers>
<add name="MyRsaProvider"
type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
keyContainerName="MyKeys"
useMachineContainer="true" />
</providers>
</configProtectedData>
<connectionStrings>...</connectionStrings>
</configuration>
aspnet_regiis -pef "connectionStrings" "C:\MyWebSite" -prov "MyRsaProvider"
aspnet_regiis -pef "system.web/identity" "C:\MyWebSite" -prov "MyRsaProvider"
<connectionStrings configProtectionProvider="MyRsaProvider">
<EncryptedData Type="https://www.w3.org/2001/04/xmlenc#Element" xmlns="https://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="https://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyInfo xmlns="https://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="https://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="https://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo><KeyName>MyKeys</KeyName></KeyInfo>
<CipherData><CipherValue>...</CipherValue></CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData><CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAA...</CipherValue></CipherData>
</EncryptedData>
</connectionStrings>
金鑰檔位於:`C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys\`
缺少權限會拋出:The RSA key container could not be opened.
aspnet_regiis -pdf "connectionStrings" "C:\MyWebSite"
aspnet_regiis -pdf "system.web/identity" "C:\MyWebSite"
aspnet_regiis -px "MyKeys" "C:\keys.xml" -priaspnet_regiis -pi "MyKeys" "C:\keys.xml"// 加密
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
ConfigurationSection section = config.GetSection("connectionStrings");
if (!section.SectionInformation.IsProtected) {
section.SectionInformation.ProtectSection("MyRsaProvider");
config.Save();
}
// 解密
if (section.SectionInformation.IsProtected) {
section.SectionInformation.UnprotectSection();
config.Save();
}