Encryption and Decryption using Cryptography Application block in Enterprise Library 5.0(part 1)

Nithadeepak
Posted by in .NET Framework category on for Beginner level | Points: 250 | Views : 14935 red flag
Rating: 4.33 out of 5  
 9 vote(s)

In this article we are going to learn how to use Cryptography Application Block. Cryptography application block helps to lessen the codes for encrytion and decryption.

Introduction

In the last article (How to use logging application block in enterprise library 5.0) I have shown you how to configure the web.config for using enterprise library 5.0.


Let us learn how to use Cryptography Application Block

For using the Cryptography Application Block you have to select "Add Cryptography settings" by clicking the Block menu of the web.config (in edit mode). This adds two empty columns to the tool: one for Hash providers and other for Symmetric Cryptography Providers.


Configuring the application for using symmetric key cryptography provider

1. Click the plus-sign icon in the Symmetric Cryptography Providers column, point to Add Symmetric Cryptography Providers, and click Add Symmetric Algorithm Provider.


2. A dialog is displayed, as shown in the following screen shot. Expand the mscorlib item until you see a list of algorithm providers. Select the RijndaelManaged type and then click OK. (You can select any algorithm.Here I'm showing an example based on RijndaelManaged algorithm.).

 

3. The Cryptographic Key Wizard is displayed (see below). In the first screen of the wizard, select the Create a new key option, and then click the Next button.

The wizard will lead you through the process of creating and protecting a cryptographic key.

 

4. Click the Generate button to generate a new key, and then click the Next button, as you see here.

5. Click the ellipsis (…) button and choose a key file location (here I choose Desktop) and then click the Next button.

 

6. Select User mode and then click Finish, as you see here.

After clicking finish button the tool will look like the below screen shot. You can change the name

RijndaelManaged to any other name. I have changed the name to crp(not in the picture).

The xml  of web.cofig is shown below

Now the code:


First you have to add the below references to the project

  • Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll
  • Microsoft.Practices.EnterpriseLibrary.Common.dll

aspx page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:TextBox runat="server" ID="txt1" /> //no validations are done here so you have to enter text before clicking button.

<asp:Button runat="server" ID="btn1" onclick="btn1_Click" text="submit"/><br />

Encrypted Text : <asp:Label runat="server" ID="lbl1" /><br />

Decrypted Text : <asp:Label runat="server" ID="lbl2"></asp:Label>

</div>

</form>

</body>

</html>

aspx.cs

using System;

using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;


public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{


}

protected void btn1_Click(object sender, EventArgs e)

{

string encrypted;

string message = txt1.Text;

encrypted = Cryptographer.EncryptSymmetric("crp", message);

lbl1.Text = encrypted;

string plainText;

plainText = Cryptographer.DecryptSymmetric("crp", encrypted);

lbl2.Text = plainText;


}

}


Conclusion

In this part-1 I had shown you how to use Symmetric cryptography providers. In the next article(part-2), I'll show you how to use Hash providers.... 

Page copy protected against web site content infringement by Copyscape

About the Author

Nithadeepak
Full Name: Nitha Deepak
Member Level: Bronze
Member Status: Member
Member Since: 4/21/2011 4:47:37 AM
Country: India
Nitha Deepak
http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Susanthampy on: 5/18/2011 | Points: 25
Gud................
Posted by: Nithadeepak on: 5/18/2011 | Points: 25
Thanks.....
Posted by: Ninilcr on: 6/2/2011 | Points: 25
helloo...

It is really a good article ...the way you explain things and provide information is awesome .
Posted by: thedavegray-10495 on: 6/3/2011 | Points: 25
Great stuff, a fully worked example without anything missing!

Thanks after trolling around for ages on other sites, your great example worked and has helped me greatly.

Cheers :-)
Posted by: Nithadeepak on: 6/4/2011 | Points: 25
Hi Ninil,Thedavegray,

Thank you very much for your encouragement.

Nitha.
Posted by: Ramyasmiley on: 6/12/2011 | Points: 25
hiiii
your article is very nice

Posted by: Nithadeepak on: 6/12/2011 | Points: 25
Thanks Ramya
Posted by: Chvrsri on: 6/13/2011 | Points: 25
Hi Nithadeepak,

You have choosed a very good topic . ! I liked it ! Thanks for sharing !!!
Posted by: Nithadeepak on: 6/14/2011 | Points: 25
Thanks Chvrsri
Posted by: Jayeshl on: 6/17/2011 | Points: 25
thanks friend to write this ..


from
Jayesh L
http://www.sqlassistant.blogspot.com
Posted by: patnaikpattu-10774 on: 6/24/2011 | Points: 25
hai.....
very good artical........
Posted by: Cryptographyexamples on: 9/6/2012 | Points: 25
Hi,
Thanks for the nice article. I am trying to user Cryptography block in my application. I have downloaded Enterprise library 5.0, however when I start new ASP.Net application, I am not able to get to the edit mode of web.config. So I am stuck at the very first step here. Can you please let me know how to get to edit mode of web.config?

Thanks
Posted by: Nithadeepak on: 9/6/2012 | Points: 25
Hi,

If you are not able to get the edit mode of web.config, take enterprise libary from start menu and open your application's web.config there. Then you can edit and save web.config.


Posted by: Cryptographyexamples on: 9/6/2012 | Points: 25
Thanks for your prompt reply. Do you have any example of encrypting/decrypting datasets using EL Cryptography block?
Also I would like to know when this application with Cryptography block is deployed to QA/Prod, how can I make sure the keys are generated correctly? What are the steps/tips to follow to deploy this application to Production?

Thanks
Posted by: Nithadeepak on: 9/7/2012 | Points: 25
Hi,

If you want to use same key for development and production, first you have to generate the key . Then you can export and send to another machine and import the key in that machine using configuration tool or code.

Please check the below link to know more about export and import.....
http://entlib.codeplex.com/discussions/251896

Thanks,
Nitha Deepak

Login to post response

Comment using Facebook(Author doesn't get notification)