Click here to Skip to main content
11,932,446 members (53,481 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked

Encryption Decryption Connection String for the App.Config File

, 14 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to develop connection string encryption on the application config file.


This tip is about encryption of the connection string from app.config file. I searched a lot on the net but was not able to find proper step by step information, which gives us proper instruction on how we can develop this functionality. Then I found a good link on Microsoft forums where I found how we can encrypt the connection string. Then I found it is very easy and can be implemented within no time. This application will generate the Encrypted connection string from the config file. We just need to copy that section and put in our app.config.


If we want to encrypt the Connection with a Windows Application Service while deploying the application to the customer we can use this code.  

Using the Code

This project is very simple. I have used Microsoft’s ConfigSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider") using this command it will encrypt the config file. What my program does is that it will look for .NET executable file. When you provide the executable file, you will have two options:

  1. Encrypt File
  2. Decrypt File

The below code is very easy to understand. The EncryptConnectionString takes two parameters. We are passing the .NET executable file name which we need to execute. The code will convert the ConnectionString Section in the encrypted format and open the file in Notepad.

public static void EncryptConnectionString(bool encrypt,string fileName)
    Configuration configuration = null;
        // Open the configuration file and retrieve the connectionStrings section.
        configuration = ConfigurationManager.OpenExeConfiguration(fileName);
        ConnectionStringsSection configSection = 
        configuration.GetSection("connectionStrings") as ConnectionStringsSection;
        if ((!(configSection.ElementInformation.IsLocked)) && 
            if (encrypt && !configSection.SectionInformation.IsProtected)
                //this line will encrypt the file

            if (!encrypt && 
            configSection.SectionInformation.IsProtected)//encrypt is true so encrypt
                //this line will decrypt the file. 
            //re-save the configuration file section
            configSection.SectionInformation.ForceSave = true;
            // Save the current configuration
            Process.Start("notepad.exe", configuration.FilePath);
    catch (Exception ex)

If you set the config file entry like below:

Section 1

    <add name="SecurePassDataBase" connectionString="Data Source=D-6058;
    Initial Catalog=DEMO_Test;User ID=sysdba;Password=xxxxxx" />

You will get the encrypted connection string like below:

Section 2

<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">

Just replace Section 1 with Section 2 in your application config file.

You can easily access the connection in your program like below:

string connectionString = ConfigurationManager.ConnectionStrings["SecurePassDataBase"].ToString();

No need to change or do anything else to be able to access the connection string. Hope this code will help you to generate the encryption in your WinForm application.


I would like to thank Shreyas Ramdahve who reviews my articles and makes them perfect.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Preetam U Ramdhave
Technical Lead
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionGreat Work, thank you Pin
Member 1048687114-Aug-15 5:03
memberMember 1048687114-Aug-15 5:03 
Questionok good Pin
mojtaba poormohamad15-May-15 2:28
membermojtaba poormohamad15-May-15 2:28 
QuestionNice work Pin
Thiago Daher13-Mar-15 20:53
memberThiago Daher13-Mar-15 20:53 
QuestionGetting NullReferenceException on Decryption Pin
MicrosoftDotNetNewbie17-Jul-14 8:46
memberMicrosoftDotNetNewbie17-Jul-14 8:46 
GeneralMy vote of 5 Pin
Sapan Ghafuri28-Sep-13 14:25
memberSapan Ghafuri28-Sep-13 14:25 
GeneralRe: My vote of 5 Pin
Thiago Daher13-Mar-15 21:13
memberThiago Daher13-Mar-15 21:13 
GeneralRe: My vote of 5 Pin
Sapan Ghafuri18-May-15 9:57
memberSapan Ghafuri18-May-15 9:57 
Questionweb config Pin
Hanieef14-Jun-13 16:45
memberHanieef14-Jun-13 16:45 
QuestionDecryption of encrypted connection string Pin
sefjnksef28-May-13 0:29
membersefjnksef28-May-13 0:29 
AnswerRe: Decryption of encrypted connection string Pin
abhi_here4-Sep-13 11:49
memberabhi_here4-Sep-13 11:49 
GeneralRe: Decryption of encrypted connection string Pin
eng_mohamedhamed13-Jan-14 0:56
membereng_mohamedhamed13-Jan-14 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 14 Jun 2013
Article Copyright 2013 by Preetam U Ramdhave
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid