Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » ASP.NET 2.0/3.5 » Manage the security of data by Encryption and Decryption

Manage the security of data by Encryption and Decryption

How we can make our data secure? By Encryption and Decryption we can achieve security for our data.

Author Rank :
Page Views : 5164
Downloads : 224
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
EncryptionAndDecryption.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Security is most important thing while working on internet. How we will get secure our data on internet?  When we send data from one place to another place then if we send it in original form then this is not a secure way to send the data. Here we send our data in encrypted form and on receiver hand we decrypt it. Here in this article, I am trying to show how we encrypt and decrypt the data.

 

For this I made a form, where we have to enter our data and here two buttons is showing one for assign key and another for encrypt the data. If we click on assign key button then a key value will be assigned to this data and if I click on encrypt button then my data will come in another TextBox in encrypted form. Here one more button is embedded to decrypt the data. If I click this button then my original data come in another TextBox.

The aspx code is:

<%@ 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 id="Head1" runat="server">

    <title>Encrypted and Decrypted </title>

</head>

<body>

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

       <div>

          <table cellpadding="0" cellspacing="0" width="80%"

                               align="center" border="4">

          <tr><td> <table width="80%">

                    <tr><td height="40px">  </td></tr>

                    <tr><td align="center">

                          <asp:Label ID="lblTxt" Text="Encryption /Decryption" runat="server"

                                   Font-Bold="true" ForeColor="Red" Width="400px"> </asp:Label>

                    </td></tr>

                    <tr> <td height="10px"> </td> </tr>

                    <tr><td align="center">

                        <asp:Label ID="Label1" Text="Enter Your Text To Encrypted :" runat="server"

                          Font-Bold="true" ForeColor="#00ccff" Width="250px"> </asp:Label>

                        <asp:TextBox ID="txt1" runat="server" Width="250px">

                        </asp:TextBox>

                    </td></tr>

                    <tr><td> <asp:Label ID="l1" runat="server" Width="176px" Visible="false"> 

                             </asp:Label>

                    </td></tr>

                    <tr><td height="30px"> </td></tr>

           

                    <tr><td align="center">

                        <asp:Button ID="AssignKey" runat="server" OnClick="AssignKey_Click"

                          Text="AssignKey To This Text" />

                        <asp:Button ID="Encrypt" runat="server" OnClick="Encrypt_Click"

                                Text="Encrypt" Width="200px" />&nbsp; </td> </tr>

                    <tr><td height="40px"> </td> </tr>

           

                   <tr> <td align="center">

                          <asp:Label ID="lbltextShow" runat="server" Text="Your Encrypted Text:"

                             ForeColor="#00ccff" Font-Bold="true" Width="250px">

                          </asp:Label>

                          <asp:TextBox ID="txt2" runat="server" Width="250px"> </asp:TextBox>

                   </td></tr>

                    <tr> <td height="10px"> </td> </tr>

                   <tr><td align="center">

                        <asp:Label ID="Label2" runat="server" Text="Want To Decrypte It:"

                           Font-Bold="true" ForeColor="Blue"> </asp:Label> <br /> <br />

                        <asp:Button ID="Decrypt" runat="server" OnClick="Decrypt_Click" Text="Decrypt"

                           Width="250px" />

                        <asp:TextBox ID="txt3" runat="server" Width="250px"> </asp:TextBox>

                    </td></tr> 

                    <tr><td height="40px"> </td></tr>

                 </table>

             </td></tr>

           </table>

        </div>

    </form>

  </body>

</html>

Here for doing encrypting and decrypting I used a class name as Cryptograpghy1 in App_code:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Security.Cryptography;

using System.Xml;

using System.IO; 

 

/// <summary>

/// Summary description for Cryptography

///// Developed by abhishek gaurav..

/// </summary>

public class Cryptography1

{

    public static RSACryptoServiceProvider rsa;

    public Cryptography1(string name)

    {

    }

 

    public static void AssignParameter()

    {

        const int PROVIDER_RSA_FULL = 1;

        const string CONTAINER_NAME = "mycontent"; CspParameters cspParams;

        cspParams = new CspParameters(PROVIDER_RSA_FULL);

        cspParams.KeyContainerName = CONTAINER_NAME;

        cspParams.Flags = CspProviderFlags.UseMachineKeyStore;

        cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";

        rsa = new RSACryptoServiceProvider(cspParams);

    }

    public static string EncryptData(string data2Encrypt)

    {

        AssignParameter();

        StreamReader reader = new StreamReader("Mypublickey.xml");    

        string publicOnlyKeyXML = reader.ReadToEnd();    

        rsa.FromXmlString(publicOnlyKeyXML);

        reader.Close();

        //read plaintext, encrypt it to ciphertext    

        byte[] plainbytes =     System.Text.Encoding.UTF8.GetBytes(data2Encrypt);    

        byte[] cipherbytes = rsa.Encrypt(plainbytes,false);

        return Convert.ToBase64String(cipherbytes);

    }

 

    public static void AssignNewKey()

    {

        AssignParameter();              

        //provide public and private RSA params

        StreamWriter writer = new StreamWriter("Myprivatekey.xml");     

        string publicPrivateKeyXML = rsa.ToXmlString(true);        

        writer.Write(publicPrivateKeyXML);

        writer.Close();

        //provide public only RSA params

        writer = new StreamWriter("Mypublickey.xml");

        string publicOnlyKeyXML = rsa.ToXmlString(false);

        writer.Write(publicOnlyKeyXML);      

        writer.Close();

    }

    public static string DecryptData(string data2Decrypt)

    {   

        AssignParameter();

        byte[] getpassword = Convert.FromBase64String(data2Decrypt);

        StreamReader reader = new StreamReader("Myprivatekey.xml");

        string publicPrivateKeyXML = reader.ReadToEnd(); 

        rsa.FromXmlString(publicPrivateKeyXML);     reader.Close();

                            

        //read ciphertext, decrypt it to plaintext

 

        byte[] plain =   rsa.Decrypt(getpassword,false);

        return System.Text.Encoding.UTF8.GetString(plain);

    }

}

The aspx.cs code is:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Security.Cryptography;

 

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

{  

    protected void Page_Load(object sender, EventArgs e)

    {

  

    }

 

    protected void AssignKey_Click(object sender, EventArgs e)

    {

        Cryptography1.AssignNewKey();

    }

 

    protected void Encrypt_Click(object sender, EventArgs e)

    {      

        txt2.Text = Cryptography1.EncryptData(txt1.Text);       

    }

 

    protected void Decrypt_Click(object sender, EventArgs e)

    {

       txt3.Text = Cryptography1.DecryptData(txt2.Text);

       txt2.Text = "";

    }

}

When user run the application then the  window will become look like as:



Figure 1.

 

If user wants to to do decrypt the data, then:



Figure 2.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Rahul Kumar Saxena
Rahul shows great interests in working with Microsoft technologies. He specializes in the implementation of DataBase & Graphics. His area of expertise includes: C#, ASP.NET,ADO.NET,Windows Forms & Web Services. He hails from background , Master's in Computer Application. With programming he loves photography, traveling and reading books.
(Talabpur*)
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Thanks by kyaw On August 19, 2009

Hi,
Thank you for your EncryptionAndDecryption.zip.
Kyaw

Reply | Email | Modify 
Got Following Error in an above application by Vijay On November 11, 2010

Error 8 'ASP.default2_aspx.GetTypeHashCode()': no suitable method found to override c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\encryptdecrpty\6d61d36b\287ccfae\App_Web_fv5ockje.0.cs 

Error 9 'ASP.default2_aspx.ProcessRequest(System.Web.HttpContext)': no suitable method found to override c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\encryptdecrpty\6d61d36b\287ccfae\App_Web_fv5ockje.0.cs 

Error 10 'ASP.default2_aspx' does not implement interface member 'System.Web.IHttpHandler.IsReusable' c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\encryptdecrpty\6d61d36b\287ccfae\App_Web_fv5ockje.0.cs 

Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.