Encrypting the configuration file has been illustrated in
form of a simple method Encrypt. The code below assumes that user is well
versed with programming of C# in ASP.NET. The code below explains how
AppSettings section in Web.Config can be encrypted.
Listing 2
private void Encrypt()
{
//Name of the provider declared as string variable
const string PROVIDER = "DataProtectionConfigurationProvider";
// Open the Configuration file
Configuration ObjConfiguration =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
// Open a section in the configuration file
AppSettingsSection ObjAppSettingsSection = ObjConfiguration.AppSettings;
// Encryption is done here
ObjAppSettingsSection.SectionInformation.ProtectSection(PROVIDER);
// Save the changes back to the configuration file
ObjConfiguration.Save();
// Reading the values after encryption and displaying the same
Response.Write(ConfigurationManager.AppSettings["Key"].ToString());
}
Let us understand the code in Listing 1 step by step. We
have a simple Encrypt method that does not have any parameters. It encrypts the
appsettings section of web.config.
Listing 3
//Name of the provider declared as string variable
const string PROVIDER = "DataProtectionConfigurationProvider";
As shown in Listing 3 we have defined a constant provider.
This is a custom protection provider. In some cases, we might need to encrypt
information using an algorithm other than those available with the RSA or DPAPI
providers. In that case, we can build a custom protected configuration provider
to be used by ASP.NET. Here, the Provider defined is a custom protection
provider. Developing the custom provider and extending the same is out of the scope
for this section. We would be limited to only using the custom provider.
Listing 4
// Open the Configuration file
Configuration ObjConfiguration =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
// Open a section in the configuration file
AppSettingsSection ObjAppSettingsSection = ObjConfiguration.AppSettings;
As shown in Listing 4, the configuration file is opened and
read into the Configuration class and the appropriate section is read into
AppSettingsSection class. [In this case it is AppSettings section.]
Listing 5
// Encryption is done here
ObjAppSettingsSection.SectionInformation.ProtectSection(PROVIDER);
As shown in listing 5 the AppSettings section gets
encrypted.
Listing 6
<appSettings>
<add key="Key" value="Value"/>
</appSettings>
Listing 6 shows the AppSettings section before encryption.
Listing 7
<appSettings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAvylHl+xPbE6
+vxGaIAr8OAQAAAACAAAAAAADZgAAqAAAABAAAAAUFpW88UBS/
QkWUOOFRdfRAAAAAASAAACgAAAAEAAAAAZ
NAa75GGiUzSMVh52mIgGIAAAAE7uQVThUA5sI4fA1C+FBWwS2H8N/c6K4KHAqz5Dqw7at2kx+
t0EXzDRAx1s1Bwmll4M7z0pwuTFGH5bZPJHGXTDqEaiHPRpaFSoyCAQ
3RCyXj6LnbxgZagBQNMU0elohTjLksNY8JdGpnrmL12Ncw
s9P5PKpA6MnPSv3H3yzG3xua17Fjn/nQRQAAACjJt5sQ5kreKEPBBPyLLnfR1RaVw==</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>
Listing 7 shows the AppSettings section after encryption.