how to encrypt and decrypt web.config in asp.net

Advertisement
In this article I will explain how to encrypt and decrypt web.config in asp.net

Before Encrypting web.config

<connectionStrings>
<add name="ConnectionString" connectionString="Data Source = DotNetSharePoint;Initial Catalog = Test;Uid = sa;Password = Password123; Integrated Security=True;" />
</connectionStrings>


I Created two methods Encript and Decrypt. We can call these methods based on conditions.
I am creating two variables

  string provider = "RSAProtectedConfigurationProvider";
  string section = "connectionStrings";

public void decrypt()
{

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection confStrSection = config.GetSection(section);
if (confStrSection != null && confStrSection.SectionInformation.IsProtected)
{
confStrSection.SectionInformation.UnprotectSection();
config.Save();
}
}


public void encript()
{

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection confStrSection1 = config.GetSection(section);
if (confStrSection1 != null)
{
confStrSection1.SectionInformation.ProtectSection(provider);
config.Save();
}

}


Now I am calling the encript method in page load.

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
encript()
}
}


Press F5.

Now I am checking the webconfig file with encript informtion.

<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>iVkxbqB25SVzeMNZ13R2E0fHQtPSlXjBKtGwwFr2VVSwW2rkCtWDFapUYfXTA4VR7Px77VY+9H2vquc2nOHuB3fvEf8G+KL5i9TN6q1MnHQ8gTasYPCsS2n6q1iwh+B+e6Vk9EQ40HWFVuSuRRUPPNs/9YsmSQQkUH1yXFegh6M=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>K3Bp2BzIGww3Yv7Jb/tffENOo28r5AkBRZn264YubFlp9VmWD5vdU+FqLBf3WJEiE01jIvWV9+uMucjZxnoBklVPnxdfC8X9SZfkhsds9k0SkSGLiqdQeLzmNHxgnyuObxaR0Q4BWLN5RyXSNPgazqfRvVE2ugJMFXS3ZU5Lh/37IP5EsLwcf3VYr0MSDddB487HcIsg0VRTONz4SRsFdJPlixxk7t6cd44KcKkbr+2Y6jxPz8NhtUYSi7awJjgHTZzYEghsIpkgDr0qh09V6ZcvAN8k6uX6</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>



If you want to Decrypt call the decrypt method in Page load.

Advertisements
SHARE

Lakshmi Tulasi Jasti

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

2 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

    ReplyDelete