Pages

Tuesday, March 20, 2012

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

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;
}
}

No comments: