Example Of Encrypt Decrypt Password using asp.net with C#
Default.aspx
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td><asp:Label ID="Label4" runat="server" Text="registre"></asp:Label></td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="enter username"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" runat="server" Text="enter password"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4"
TextMode="Password"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="what is your rasi"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="what is your birth month "></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>
<%--<tr>
<td>
<asp:Label
ID="Label3" runat="server" Text="enter
quastion"></asp:Label>
</td>
<td>
<asp:TextBox
ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>--%>
<%--<tr>
<td>
<asp:Label
ID="Label7" runat="server" Text="enter your
answer"></asp:Label>
</td>
<td>
<asp:TextBox
ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>--%>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="registration" onclick="Button1_Click" />
</td>
<td>
</td>
</tr>
</table>
<%--<asp:GridView
ID="GridView1" runat="server"
AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label
ID="Label1" runat="server"
Text='<%#Eval("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label
ID="Label2" runat="server"
Text='<%#Eval("password") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>--%>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
}
protected void Button1_Click(object
sender, EventArgs e)
{
string
pass =encryp(TextBox4.Text);
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
conn.Open();
string
q ="insert into
pi_login1(username,password,ans1,ans2)values('"+TextBox3.Text+"','"+pass+"','"+TextBox5.Text+"','"+TextBox6.Text+"')";
SqlCommand
cmd = new SqlCommand(q,
conn);
int
i=cmd.ExecuteNonQuery();
if (i
> 0)
{
Response.Redirect("login.aspx");
}
}
private string encryp(string
password)
{
string
msg = string.Empty;
byte[]
encode = new byte[password.Length];
encode = Encoding.UTF8.GetBytes(password);
msg = Convert.ToBase64String(encode);
return
msg;
}
protected void Button3_Click(object
sender, EventArgs e)
{
}
}
0 comments :