Encrypting and Decrypting Config Files
Thursday, November 19, 2009, 05:38 AM - Programming, .NET, C#, VB.NET, ASPX
I've talked for years about how you need to encrypt and decrypt Web.config and App.config files, but it took a spur for me to actually look it up. Here is the command for those of you as lazy as me:
c:windowsMicrosoft.NETFrameworkv2.0.50727aspnet_regiis -pef connectionStrings . -prov DataProtectionConfigurationProvider
Decrypting is just as easy:
c:windowsMicrosoft.NETFrameworkv2.0.50727aspnet_regiis -pdf connectionStrings .
Just a note, this only works on Web.config files. If you want to encrypt an App.config, just rename it Web.config, run the tool, then change the name back.
If you want to get hardcore and write your own tool, it's all available in the API:
Aaron Feng's Blog
Configuration configuration = ConfigurationManager.OpenExeConfiguration(appConfig);
ConfigurationSection section = this.configuration.GetSection("connectionStrings");
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
section.SectionInformation.ForceSave = true;
configuration.Save(ConfigurationSaveMode.Modified);
}
To decrypt just do the oposite:
if (section.SectionInformation.IsProtected)
{
// …
section.SectionInformation.UnprotectSection();
// …
}
[ add comment ] | [ 0 trackbacks ] | permalink |




( 2.9 / 657 )Back






