Welcome to Darren Sim's Playpans Sign in | Join | Help

Web.Config Configuration Encryption

Recently, I've been working on quite a number of projects to do with enterprise web application development, and many a times, I got request to have all encryption strings encrypted. Of course, even if they didn't tell me to do so, I'd also do so as it's just too dangerous to keep it in plaintext.

So do you have to specifically write a special dll to do this task or use the cryptographic services in .NET to do so? The answer is no! ASP.NET 2.0 provides you such capabilities. Infact, this has already been available in ASP.NET 1.1. Just that ASP.NET 2.0 includes the option to do so with DPAPI too!

ASP.NET 2.0 supports two forms of encryptions:

  • RSA (a form of asymmetric encryption)
  • DPAPI

RSA is recommended as DPAPI makes use of key that are machine-specific. So that doesn't sound too portable. Well, as RSA is a 1024 bit block encryption, if you do want to encrypt data-strings larger then that, you might want to consider making use of Envelop Encryption, where you'd

  1. Generate a random private key
  2. Encrypt the string with a symmetric encryption algorithm (e.g. 3DES, AES) using the randomly generated private key
  3. Encrypt the random private key using RSA

Why so? This would thus help you to solve any form of key distribution problems and also increase the encryption speed. Symmetric Encryption is typically 10,000 times faster then Asymmetric-Key. This makes senses when you look at the number of keys used.

In this example, DPAPI is used. However, if you do want to make use of RSA to make your code portable, you'd need to change "DataProtectionConfigurationProvider" to "RSAProtectedConfigurationProvider".

Implementation Code

Configuration config =

WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

 

ConfigurationSection appSettings =

config.GetSection("appSettings");

 

if (appSettings.SectionInformation.IsProtected)

{

appSettings.SectionInformation.UnprotectSection();               

Button1.Text = "Decrypt";

}

else

{

appSettings.SectionInformation.ProtectSection

            ("DataProtectionConfigurationProvider"); 

Button1.Text = "Encrypt";

} 

config.Save();

 

Web.Config Prior to Encryption

<?xml version="1.0"?>
<
configuration>
<
appSettings>
       <
add key="customerRecordDBConnectionString" value="Data Source=(local);Database=db_custRecs;Integrated Security=SSPI;"/>
</
appSettings>
<
connectionStrings/>
<
system.web>
      
<compilation debug="true" />
      
<authentication mode="Windows" />
</system.web>
</
configuration>

 

Web.Config After Encryption

<?xml version="1.0"?>
<
configuration>
<
appSettings>
        <EncryptedData>
                <
CipherData>
                        <
CipherValue>
                                             
AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAZ+eO5Glne0Cg5DOdDS6FlgQ
                                             AAAACAAAAAAADZgAAqAAAABAAAAASnhP72Mnx926RfOa32hOQAAAA
                                             AASAAACgAAAAEAAAAD+gxssSojnRnAqLtkqU2ThAAQAAiEMFWIBgi6zEb
                                             bcT78v65+Sm8gp2opspMWr2jTFxC5eJtVtecSUiDMGbEQOYJPStnrbrXL3W16
                                             bjF3xrBnEg4toTQnzvBMz+3Eaqy0/2Js/sksh/0OA2OIkwLU4BVEZhLN3TAiLDj
                                             HzrzHxUzmUBdkkPBxSfaSFrnSh2eVTZWf+YBDT7Z1q9WWSe8Q22BHI2TRa
                                             H/mjFXm/7rZQmdG3zhXX+EMQl2ow7/CGhYBZF1zOhMEdlE5ui/JOBd722CHv
                                             Gb8sWBK6wd92dRs1T99+LZucBWpW0S4gonObUVYmKHa+gnK26L5rskpm
                                             5XOBmDo8certosnEjHMqxH0JYY9/3xEevH5tG7HtOopc+TNo8/+C3jzWdX4uq
                                             +S1grYRQff5Kmvqx4vR73v2/99q5UHqYkCPe5XLkMAFT20Dxux8er/oUAAAAY
                                             auBiM2jrVRXhk3t6mN087j/HFI=
                        </CipherValue>
                </
CipherData>
        </
EncryptedData>

</
appSettings>
<
connectionStrings/>
<
system.web>
      
<compilation debug="true" />
      
<authentication mode="Windows" />
</system.web>
</
configuration>

Published Saturday, March 10, 2007 10:37 PM by darren.sim
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Web.Config Configuration Encryption

Sunday, March 11, 2007 5:56 PM by Alan

Good info

# re: Web.Config Configuration Encryption

Friday, March 23, 2007 6:31 AM by Amit

it gives the error...when config.Save();

is exceuted

# re: Web.Config Configuration Encryption

Friday, March 23, 2007 8:30 AM by darren.sim

Hi Amit,

Great to hear from you. What error are you getting? If the error contains "too confidential" information that you do not want to put on the public domain, you could drop me a mail at darren@darrensim.com. =)

Is the virtual directory on your Server set to run ASP.NET 2.0 runtime?

# re: Web.Config Configuration Encryption

Tuesday, April 24, 2007 3:49 AM by Md Farman Alam

Great ...

but.consider the following...

using the encryption method ..i encrypted a section of the web.config ...

than i copy that section and add the section (still in encrypted form) to another web.config

than i reun the code to decrypt the section ...i could still get to know the data that was initially encrypted ....

inviting hackers ..it seems ...

is there a way out...

# re: Web.Config Configuration Encryption

Thursday, October 04, 2007 9:10 AM by Tapas Pati

what is the solution for .Net1.1

Leave a Comment

(required) 
required 
(required)