Monday, December 5, 2011
Encrypt & Decrypt Data in asp.net with c#
Copy Belove code IN Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">
Id
Copy Belove code in default.aspx.cs page
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;using System.Text;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=DataDirectory\Database.mdf;Integrated Security=True;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
}
//Encrypt Text public string EncryptText(string str)
{ return Encrypt(str, "&%#@?,:*");
}
// Decrypt the text
public string DecryptText(string strText)
{ return Decrypt(strText, "&%#@?,:*");
}
//The function used to encrypt the text
private string Encrypt(string strText, string strEncrKey)
{
byte[] byKey = { }; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD,0xEF };
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs=new CryptoStream(ms, des.CreateEncryptor(byKey, IV),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
//The function used to decrypt the text private string Decrypt(string strText, string sDecrKey)
{
byte[] byKey = { };
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = new byte[strText.Length];
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
protected void btnAdd_Click(object sender, EventArgs e)
{
string name = EncryptText(txtName.Text);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into emp values('"+name+"')",con); cmd.ExecuteNonQuery();
con.Close();
}
protected void btnSelect_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from emp where id='"+txtId.Text+"'",con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string name = DecryptText(dr[1].ToString());
txtName.Text = name;
txtWde.Text = dr[1].ToString();
}
con.Close();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">
Id
Copy Belove code in default.aspx.cs page
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;using System.Text;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=DataDirectory\Database.mdf;Integrated Security=True;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
}
//Encrypt Text public string EncryptText(string str)
{ return Encrypt(str, "&%#@?,:*");
}
// Decrypt the text
public string DecryptText(string strText)
{ return Decrypt(strText, "&%#@?,:*");
}
//The function used to encrypt the text
private string Encrypt(string strText, string strEncrKey)
{
byte[] byKey = { }; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD,0xEF };
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs=new CryptoStream(ms, des.CreateEncryptor(byKey, IV),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
//The function used to decrypt the text private string Decrypt(string strText, string sDecrKey)
{
byte[] byKey = { };
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = new byte[strText.Length];
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
protected void btnAdd_Click(object sender, EventArgs e)
{
string name = EncryptText(txtName.Text);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into emp values('"+name+"')",con); cmd.ExecuteNonQuery();
con.Close();
}
protected void btnSelect_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from emp where id='"+txtId.Text+"'",con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string name = DecryptText(dr[1].ToString());
txtName.Text = name;
txtWde.Text = dr[1].ToString();
}
con.Close();
}
}