Encrypt & Decrypt of Password in Asp.net
In .aspx page:-
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="" width="50%">
<tr>
<td colspan="2" style="font-family: 'Times New Roman', Times, serif; font-size: large;
font-weight: bold; text-decoration: underline; color: #800000">
Encryption and Decryption of Password.
</td>
</tr>
<tr>
<td align="right">
UserName :
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUsername" ErrorMessage="Enter User Name">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
Password :
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtPassword" ErrorMessage="Enter Password">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
Email Id :
</td>
<td>
<asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td />
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</br></br>
<table cellspacing="10" width="60%">
<tr>
<td align="center">
Encrypted Password.
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvUsers" runat="server" CellPadding="4" BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" Width="480px">
<RowStyle BackColor="White" ForeColor="#330099" />
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</td>
</tr>
<tr>
<td align="center">
Decrypted Password.
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvdecryption" runat="server" BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" CellPadding="4"OnRowDataBound="gvdecryption_RowDataBound"
Width="480px">
<RowStyle BackColor="White" ForeColor="#330099" />
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
In .aspx.Cs Page:-
SqlConnection ConnString = newSqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
DataSet Ds;
SqlDataAdapter Da;
String SqlString = "SELECT * FROM [UserTable]";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridBindEncryptedData();
GridBindDecryptedData();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string StrPwd = Encryptdata(txtPassword.Text);
ConnString.Open();
SqlCommand SqlCmd = new SqlCommand("INSERT INTO [UserTable]([Username],[Password],[EmailId])VALUES('" + txtUsername.Text + "','" + StrPwd + "','" + txtEmailId.Text + "')", ConnString );
SqlCmd.ExecuteNonQuery();
ConnString.Close();
GridBindEncryptedData();
GridBindDecryptedData();
}
#region " G R I D V i e w D I S P L A Y "
//Binding Encrypted data To GridView1
protected void GridBindEncryptedData()
{
ConnString.Open();
SqlCommand SqlCmd = new SqlCommand(SqlString, ConnString);
Da = new SqlDataAdapter(SqlCmd);
Ds = new DataSet();
Da.Fill(Ds);
gvUsers.DataSource = Ds;
gvUsers.DataBind();
ConnString.Close();
}
//Binding Decrypted data To GridView2
protected void GridBindDecryptedData()
{
ConnString .Open();
SqlCommand SqlCmd = new SqlCommand(SqlString, ConnString);
Da = new SqlDataAdapter(SqlCmd);
Ds = new DataSet();
Da.Fill(Ds);
gvdecryption.DataSource = Ds;
gvdecryption.DataBind();
ConnString .Close();
}
//RowDataBound event fires on binding the data to the row
protected void gvdecryption_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decryptpassword = e.Row.Cells[1].Text;
e.Row.Cells[1].Text = Decryptdata(decryptpassword);
}
}
#endregion
#region " F U N C T I O N S "
// Function is used to Encrypt the Password
private string Encryptdata(string Password)
{
string strPwd = string.Empty;
byte[] toEncode = new byte[Password.Length];
toEncode = System.Text.Encoding.UTF8.GetBytes(Password);
strPwd = Convert.ToBase64String(toEncode);
return strPwd;
}
// Function is used to Decrypt the Password
private string Decryptdata(string encryptpwd)
{
string strPwd = string.Empty;
UTF8Encoding EncodePwd = new UTF8Encoding();
System.Text.Decoder utf8Decode = EncodePwd.GetDecoder();
byte[] toDecode = Convert.FromBase64String(encryptpwd);
int StrCount = utf8Decode.GetCharCount(toDecode, 0, toDecode.Length);
char[] decodedStr = new char[StrCount];
utf8Decode.GetChars(toDecode, 0, toDecode.Length, decodedStr, 0);
strPwd = new String(decodedStr);
return strPwd;
}
#endregion
In Web.Config:-
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=Manjeeth;Initial Catalog= Manjeeth;User ID=sa;Password=****" providerName="System.Data.SqlClient"/>
</connectionStrings>
Database Script for a table UserTable:-
CREATE TABLE [dbo].[UserTable](
[Username] [varchar](50) NOT NULL,
[Password] [nvarchar](100) NULL,
[EmailId] [varchar](50) NULL,
CONSTRAINT [PK_UserTable] PRIMARY KEY CLUSTERED
(
[Username] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS =ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
No comments:
Post a Comment