Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

Manage the security of data by Encryption and Decryption
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">
<htmlxmlns="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:LabelID="lblTxt" Text="Encryption/Decryption" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td align="center">
<asp:Label ID="Label1" Text="Enter Your Text To Encrypted :" runat="server" Width="250px">
</asp:Label><asp:TextBox ID="txt1" runat="server" Width="250px"> </asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:LabelID="l1" runat="server" 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:" 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
/// </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 EncryptData(string Data2Encrypt)
   {
       AssignParameter();
      StreamReader reader =new StreamReader("Mypublickey.xml");          
     string l=reader.ReadToEnd();            
     rsa.FromXmlString(l);
     reader.Close();
    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 publickey = 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 = "";
}
}

 
You can download code for this tutorial.
 
 
 
 
 
 
 
 



Job or extra money for students

Search Engine Rank of your blog or websites