aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Encrypt and Decrypt Connection String in App.Config File in Windows / Console Applications

Feb 25, 2015

Here I will explain how to encrypt and decrypt connection string in app.config file in windows or console application using,
we can do encryption and decryption for connection string using aspnet_regiis.exe command line tool in file system website.


In Previous articles I explained add textbox items to listbox on button click in, difference between dispose and finalize methods in c#, vb.netdifference between page_load and page_init methods in, convert numbers or rupees to words in and many articles related to, Gridview, SQL Server, Ajax, JavaScript. Now i will explain how to encrypt and decrypt connection string in app.config file in windows or console applications. 

Generally in all applications we will use connectionStrings section in web.config or app.config file to store database connection. The connectionStrings section contains sensitive information of database connections including username and password of database. Is it secured to store the sensitive information of database connections in plain text files called web.config and machine.config files? 

If we are using applications in our internal servers with security then it’s OK if we deploy our applications in shared host environment then we have chance to arise security problems to avoid these problems 2.0 provided built in protected configuration model functionality to encrypt or decrypt few sections of web.config file those are

RSAProtectedConfigurationProvider: This is default provider and uses the RSA public key encryption algorithm to encrypt and decrypt data.

DataProtectionConfgurationProvider: This provider uses windows data protection application programming interface to encrypt and decrypt the data.

The encrypting and decrypting of connection strings in app.config file will do by using aspnet_regiis.exe command line tool. Before we implement encryption and decryption in app.config file first we need to rename our app.config file to web.config because aspnet_regiis.exe command line tool will not recognize app.config for that reason temporarily we need to rename app.config file to web.config  

Once we rename app.config file to web.config open web.config file in application and add sample db connection in connectionStrings section like this 

<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings >

After add dbconnection in connectionString check the below steps to encrypt or decrypt the connection string in web.config.

1.     1) Go to Start >> All Programs >> Microsoft visual studio 2008 >> Visual Studio Tools >> Visual Studio 2008 Command Prompt (Note: if you’re using windows 7 right click on command prompt and select Run as administrator)

2.  After open command prompt type the following command aspnet_regiis.exe -pef "connectionStrings" "C:\VisualStudio2008\Authorization"

Here –pef indicates that the application is built as File System website. Second argument connectionStrings indicates that name of the configuration section needs to be encrypted. The Third argument is the physical path of the folder where the web.config file is located.

3. After enter the command click enter if everything goes well we will get success message like “Encrypting configuration sectionSucceeded!

Now open your file and check connectionStrings in web.config file that would be like this

<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
<EncryptedData Type=""
<EncryptionMethod Algorithm="" />
<KeyInfo xmlns="">
<EncryptedKey xmlns="">
<EncryptionMethod Algorithm="" />
<KeyInfo xmlns="">
<KeyName>Rsa Key</KeyName>

Here we don’t want to write any code to decrypt the encrypted connectionString in our application because .NET automatically decrypts it. If we want to use the connection string just call it like normal way

string strconnection = ConfigurationManager.AppSettings["dbconnection"].ToString();

Now if we want to decrypt connectionStrings section in web.config use the following command aspnet_regiis.exe -pdf "connectionStrings" "C:\VisualStudio2008\Authorization"

After command execute we will get message like “Decrypting configuration sectionSucceeded!

Now check your connctionStrings section in your web.config file you will see decrypted connection string. 

I hope it helps you to encrypt or decrypt connection string in app.config file.... Happy coding....

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Unknown said...

Can i dencrypt/decrypt my smtp settings too using this???

Dileepcp said...

hi sir. that is ok for windows or console app but how to make it for web app.
please help me in this regard. its little urgent please

Unknown said...

Hello ,

How to prevent someone who knows that the file was encrypted can not copy the project and go decrypt it to have access to the parameters in the clear.

Anonymous said...

how to encrypt password of an entities in connection strings

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.