Password Encryption & Decryption using C#
Password Encryption & Decryption using C#:
This resource demonstrates to encrypt and decrypt your password as you want.
1. Add reference to the Microsoft.VisualBasic class.
2. Place a Label Control to display the Encrypted Password.
3.Below is the code that is used to convert a string to hexadecimal format. We cannot directly convert all characters into hexadecimal format.So first we take the ASCII value of the character and then convert the ASCII value into hexadecimal format.
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 Microsoft.VisualBasic;
public partial class PasswordEncyrption : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string sData = TextBox1.Text;
Label3.Text = PasswordEncryption(ref sData);
Label3.Visible = true;
Label2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
string sData = Label3.Text;
TextBox1.Text = PasswordDecription(ref sData);
}
public string PasswordDecryption(ref string Data)
{
string Data1 = "";
string sData = "";
while (Data.Length > 0)
{
Data1 = System.Convert.ToChar(System.Convert.ToUInt32(Data.Substring(0, 2), 16)).ToString();
sData = sData + Data1;
Data = Data.Substring(2, Data.Length - 2);
}
return sData;
}
public string PasswordEncryption(ref string Data)
{
string sValue;
string sHex = "";
while (Data.Length > 0)
{
sValue = Conversion.Hex(Strings.Asc(Data.Substring(0, 1).ToString()));
Data = Data.Substring(1, Data.Length - 1);
sHex = sHex + sValue;
}
return sHex;
}
}