Encrypting and Decrypting Configuration File
page 4 of 5
by Uday Denduluri
Feedback
Average Rating: 
Views (Total / Last 10 Days): 26565/ 38

Decrypting the Encrypted Configuration file

Decrypting a configuration file can be done with the help of the method shown below, Decrypt. The Decrypt method simply checks if the configuration section is encrypted. If it is encrypted then it decrypts the same. The same is shown in listing 8. The code below assumes that the reader is well versed with programming in C#. Some of the classes are new in .NET Framework 2.0

Listing 8

private void Decrypt()
{
// Open the Configuration file
Configuration ObjConfiguration =
 WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
 
// Open a section in the configuration file
AppSettingsSection ObjAppSettingsSection = ObjConfiguration.AppSettings;
 
// Checking if the section information is protected
if (ObjAppSettingsSection.SectionInformation.IsProtected)
{
// Decrypt the section
               ObjAppSettingsSection.SectionInformation.UnprotectSection();
 
//Save the changes after decryption
ObjConfiguration.Save();
}
// Reading the values after De-crypting configuration file
            Response.Write(ConfigurationManager.AppSettings["Key"].ToString());
}

 

Decrypting the configuration file involves loading the configuration file in the Configuration object. Reading the section for which the encryption is done, check that the section is protected. If it is protected then unprotect the section. The changes done to the configuration need to be persisted back to the configuration file. Therefore, save the changes.

Listing 9

// Checking if the section information is protected
if (ObjAppSettingsSection.SectionInformation.IsProtected)
{
// Decrypt the section
               ObjAppSettingsSection.SectionInformation.UnprotectSection();
 
//Save the changes after decryption
ObjConfiguration.Save();
}

Listing 9 has the code in C# where the section information is checked for protection. This is done by the IsProtected method of SectionInformation class. If it returns true then the SectionInformation’s method UnprotectSection is called. This method simply decrypts the configuration and changes the configuration to its previous state. Once the changes are done then the Configuration is saved back to the configuration.


View Entire Article

User Comments

Title: Good one..   
Name: Gourik Kumar Bora
Date: 2009-03-04 1:03:41 AM
Comment:
Hi,
Its really good .can you please tell me how can i ensure that asp.net worker process will modify the web.config.
thanks in advance
Gourik
Title: Encrypting and decrypting a configuration file   
Name: Nitin Dixit
Date: 2007-07-26 3:46:41 AM
Comment:
Dear Uday,
How can i use my configuration after encryption?
Means lets suppose we have a connectionstring of my application and i encrypt that particular config section.
Now in my code behind how can i use it??????

thanks & Regards
Nitin Dixit






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 3:32:36 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search