In cryptography, encryption is the process of transforming information (referred
to as plaintext) using an algorithm (called cipher) to make it unreadable to
anyone except those possessing special knowledge, usually referred to as a key.
The result of the process is encrypted information (in cryptography, referred to
as cipher text). In many contexts, the word encryption also implicitly refers to
the reverse process, decryption (e.g. "software for encryption" can typically
also perform decryption), to make the encrypted information readable again (i.e.
to make it unencrypted).
User Define Key for Encryption & Decryption:
private
static string
EncryptionKey = "!#853g`de";
private static
byte[] key = { };
private
static byte[] IV
= { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Encryption Technique:
public
string Encrypt(string
Input)
{
try
{
key = System.Text.Encoding.UTF8.GetBytes(EncryptionKey.Substring(0,
8));
DESCryptoServiceProvider des
= new
DESCryptoServiceProvider();
Byte[] inputByteArray =
Encoding.UTF8.GetBytes(Input);
MemoryStream ms =
new MemoryStream();
CryptoStream cs =
new CryptoStream(ms,
des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return
Convert.ToBase64String(ms.ToArray());
}
catch (Exception
ex)
{
return "";
}
}
Decryption Technique :
public
string Decrypt(string
Input)
{
Byte[] inputByteArray =
new Byte[Input.Length];
try
{
key = System.Text.Encoding.UTF8.GetBytes(EncryptionKey.Substring(0,
8));
DESCryptoServiceProvider des
= new
DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(Input);
MemoryStream ms =
new MemoryStream();
CryptoStream cs =
new CryptoStream(ms,
des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding =
Encoding.UTF8;
return
encoding.GetString(ms.ToArray());
}
catch (Exception
ex)
{
return
"";
}
}

bharath kumareditedPosted Feb 14, 2013, 1:36 AMEdited Feb 14, 2013, 1:37 AM
i have created a simple login page and want to Encrypt and Decrypt the password and the coads is not working correctly so plz see the error int the coading. coading: using System; using System.Collections; 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.Security.Cryptography; using System.Data.SqlClient; namespace WebApplication5 { public partial class WebForm4 : System.Web.UI.Page { SqlConnection connection; protected void Page_Load(object sender, EventArgs e) { connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestQueryConnectionString"].ConnectionString); } protected void btnSubmit_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestQueryConnectionString"].ConnectionString); string strpassword = EncodePasswordToBase64(txtPassword.Text); con.Open(); string strQuery = ("insert into admin( USERNAME,PASSWORD) values('" + txtUserName.Text + "','" + txtPassword.Text + "')"); connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestQueryConnectionString"].ConnectionString); connection.Open(); SqlCommand cmd = new SqlCommand(strQuery, connection); cmd.ExecuteNonQuery(); connection.Close(); SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["TestQueryConnectionString"].ConnectionString); string strpwd = EncodePasswordToBase64(txtPassword.Text); con1.Open(); SqlCommand cmd1 = new SqlCommand("select * from admin where USERNAME=@USERNAME and PASSWORD=@PASSWORD ", con); cmd1.Parameters.AddWithValue("@username", txtUserName.Text); cmd1.Parameters.AddWithValue("@password", txtPassword.Text); SqlDataAdapter da = new SqlDataAdapter(cmd1); DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { Response.Redirect("emplist.aspx"); } else { ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>"); } con1.Close(); } public static string EncodePasswordToBase64(string password) { try { byte[] encData_byte = new byte[password.Length]; encData_byte = System.Text.Encoding.UTF8.GetBytes(password); string encodedData = Convert.ToBase64String(encData_byte); return encodedData; } catch (Exception ex) { throw new Exception("Error in base64Encode" + ex.Message); } } public string DecodeFrom64(string encodedData) { System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); System.Text.Decoder utf8Decode = encoder.GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(encodedData); int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); char[] decoded_char = new char[charCount]; utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); string result = new String(decoded_char); return result; } protected void btnClear_Click(object sender, EventArgs e) { txtUserName.Text = ""; txtPassword.Text = ""; } } }
manjusha nayakPosted Jun 11, 2012, 2:55 AM
Decryption not working for value 29.Is there any other solutions?