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.
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
Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<form id="form1" runat="server">
<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>
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 message = txt1.Text;
encrypted = Cryptographer.EncryptSymmetric("crp", message);
plainText = Cryptographer.DecryptSymmetric("crp", encrypted);
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....