Generally We do keep application level variables and connection strings in Web.Config files for updating values across the project at one go. Being a very important file, It's always needed to secure the information of the file by some means, exactly for this reason .Net provides an API to protect the data by means of few providers(Cryptographic methods), to cite DpapiProtectedConfigurationProvider and
RSAProtectedConfigurationProvider. So in this Article we'll examine, how to encrypt a specific portion(
connection strings in this case) of Web.config file using RSAProtectedConfigurationProvider.
So here it goes,
1)Create a new website/application
2)Run the website, system'll prompt for adding Web.Config File, Click Yes
3)Stop debugging the application.
4)Open Web.Config File and add below code in between @configuration> and ,@/configuration>
@connectionstrings@
@add name="myCon" connectionstring="data source=localhost; Initial Catalog = master; user id = Encrypt; password= Decrypt"@
@/add@@/connectionstrings@@/blockquote@
5)Open Default.aspx(assuming you've not changed the name of the default page appears once you create a new website) and add two buttons and name them as Encrypt and Decrypt respectively
6)Double click "Encrypt" button and paste the below code
Configuration WebConfigFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection SectionConnectionStrings = WebConfigFile.GetSection("connectionStrings");
SectionConnectionStrings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
WebConfigFile.Save();
7)Double click "Encrypt" button and paste the below code
Configuration WebConfigFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection SectionConnectionStrings = WebConfigFile.GetSection("connectionStrings");
SectionConnectionStrings.SectionInformation.UnprotectSection();
WebConfigFile.Save();
8) Run the application, Default.aspx pops-up with two buttons "Encrypt" and "Decrypt". Click on button "Encrypt", It'll encrypt the "connectionStrings" section in Web.config file, and asks for overriding the file ie Web.config, click Yes
9)Stop debugging and open Web.Config file and now you can notice below code instead of original text form "connectionStrings" section.
@connectionstrings configprotectionprovider="RsaProtectedConfigurationProvider"@
@encrypteddata type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
@encryptionmethod algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc">
@keyinfo xmlns="http://www.w3.org/2000/09/xmldsig#">
@encryptedkey xmlns="http://www.w3.org/2001/04/xmlenc#">
@encryptionmethod algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5">
@keyinfo xmlns="http://www.w3.org/2000/09/xmldsig#">
@keyname>Rsa Key@/keyname>
@/keyinfo>
@cipherdata>
@ciphervalue>CGqBcokAoiZ+uB6Le7Aufbt8wQ018UrKWcfso73WtSSztJMj0O1BywJoE/3jdOREcojeHWiFY5+9GXX+7XBLxfFalOfrJgXQpXyBbwpQhvsgGbH+xiRMmVCPhpnwSYQK/3WjnulS8ywqQbGBNww86VohEjWm4bCXkmEDH0haoYI=@/ciphervalue>
@/cipherdata>
@/encryptionmethod>@/encryptedkey>
@/keyinfo>
@cipherdata>
@ciphervalue>IinIZskL3FGF8iDGAv+qEmkbGsGi4ByTohmEh7/wew86lvn2eqiOI9NVBm1usXzUz8QWFwHboSAam+TJmPvLe1LavrZ9ZHFmFs5exURegt4hxuZphAHFisWr7AB4HDrrrORsuD3IIMq4Fyq9EvsGpRRVcefl+gPejP04vv7bAc/qbFbJYtzH7OeglxI1Z0UBhvkYKGcFSGY+GgvyDsmo12+7KiVa7H7vAz6Se20BLAk=
@/cipherdata>
@/encryptionmethod>@/encrypteddata>
@/connectionstrings>
So normal text formed
sextion has been changed to cypher text, and if you want to get back the "connectionStrings" original text data, just click on "Decrypt" button and notice the magic, that's it, it's pretty simple, we should not bother about which Algorithm have to use, where to store the key value and etc, everything would be taken care by .Net and our life is made easy.....:)
Hope it helps, Thank you and comments are most welcome.