Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday, 4 February 2015

Introduction: 

 We will learn How to Encrypt and  Decrypt connection string in web.config.As we now connectionstring in the web.config file contains the most sensitive information such as Database name ,Username & Password. So it’s better to encrypt the connectionstring for security.


Implementation:

  • Suppose your connection string  in web.config file look like:
<connectionStrings>

  <add name="con" connectionString="Data Source=localhost;Initial Catalog=Test;Integrated Security=True" />
 </connectionStrings>

and In order to encrypt the connection string section in the web.config file follow the steps, 

1. Go to Start -> All programs -> Microsoft Visual Studio 2010-> Visual studio Tools 
-> Microsoft Visual Studio Command Prompt(2010) 

2. Type following command in command prompt, 

aspnet_regiis.exe -pef “connectionStrings” D:\Projects\MyProject 

Here “–pef” indicates that the application is built as File System website. The second argument is the name of configuration section that you want to encrypt. Third argument is the physical path where the web.config file is located. E.g. here in our case in D drive and the project name is MyProject. 

In case if you are using IIS base web site then the command will be, 

aspnet_regiis.exe -pe “connectionStrings” -app “/MyProject” 

Here ” –pe” indicates that the application is built as IIS based site. The second argument is the name of configuration section that you want to encrypt. Third argument “-app” indicates virtual directoryand last argument is the name of virtual directory where application is deployed. 

If everything goes fine then you will receive a message “Encrypting configuration section…Succeeded!” 

Now to check your web.config file and you can see that connection string is in encrypted form like:

<connectionStringsconfigProtectionProvider="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>BtkULhGibSbuUXf+Sj7Ji4K7KTCvZkCHD4V/2cF1uZBqRxaZJDEfNyZ6VfCyZSzzhALRyAwXe6BSF5If4X755sZzwPeIB5/T0Xbf4A3k/U5zVh8GEeEej3Su6N+qY4RBJWg6YxXRTY40fsBqb8jgwBEC3QpoR1T4OZBvhJtqZaI=</CipherValue>
     </CipherData>
    </EncryptedKey>
   </KeyInfo>
   <CipherData>
    <CipherValue>UUwuB9KVFnFwFHH85nBDe5hWoF0d1cDjA6ObR8U62zXh7NiRPLKNzCJe6LZN5+dhN986Vw9YPKldEJJK4MaNXkvI9pavHb/nY9Oeuhr/GjFAaThx9SEzgIO53TdYMqH0Fpg4ESfK0gCMvniX5cdvukCMETRnQxqXP3IoHaonADnsbzS9nE0drVHfF1E+v4LXtfiYjMLFH5XR88Vki+6R8gY8m3pks/RN</CipherValue>
   </CipherData>
  </EncryptedData>

 </connectionStrings>

0 comments:

Post a Comment