Save Encrypted Password In Database In ASP.NET

We make a small registration form and save all the details along with encrypted data into the database.

Initial chamber

Step 1. Open Visual Studio 2010 and Create an Empty Website. Give a suitable name registration_demo.

Step 2. In Solution Explorer, you will get your empty website. Add a web form and SQL Database. Follow these steps.

For Web Form

registration_demo (Your Empty Website) - Right-click, Add New Item, then Web Form. Name it CheckBoxList_demo.aspx.

For SQL Server Database

registration_demo (Your Empty Website) - Right-click, Add New Item, then SQL Server Database. Add Database inside the App_Data_folder.

Database chamber

Step 3. In Server Explorer, click on your database Database.mdf  - Tables, then Add New Table and make a table like this.

Table - tbl_data and don’t forget to make ID as IS Identity -- True

Table

Design chamber

Step 4. Now make some designs for your application by going to registration_demo.aspx and trying the code like this.

registration_demo.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>
    <style type="text/css">
        .style1 {
            width: 263px;
        }
          
        .style2 {
            width: 251px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="width:100%;">
                <tr>
                    <td class="style1"> Username:</td>
                    <td class="style2">
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                    <td> </td>
                </tr>
                <tr>
                    <td class="style1"> Email:</td>
                    <td class="style2">
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                    <td> </td>
                </tr>
                <tr>
                    <td class="style1"> Password:</td>
                    <td class="style2">
                        <asp:TextBox ID="TextBox3" runat="server" EnableTheming="True" TextMode="Password"></asp:TextBox>
                    </td>
                    <td> </td>
                </tr>
                <tr>
                    <td class="style1">
                        <asp:Label ID="Label1" runat="server"></asp:Label>
                    </td>
                    <td class="style2">
                        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
                    </td>
                    <td> </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Your design looks like this.

Design

Code chamber

Step 5. Now it’s time for server-side coding so that our application starts working. Open registration_demo.aspx.cs file and code it like the following.

registration_demo.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.Text;
using System.Data.SqlClient;

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)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string strpass = encryptpass(TextBox3.Text);
        SqlCommand cmd = new SqlCommand("insert into tbl_data values(@name,@email,@pass)", con);
        cmd.Parameters.AddWithValue("@name", TextBox1.Text);
        cmd.Parameters.AddWithValue("@email", TextBox2.Text);
        cmd.Parameters.AddWithValue("@pass", strpass);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();
        if (i != 0)
        {
            Label1.Text = "Registration Complete";
            Label1.ForeColor = System.Drawing.Color.ForestGreen;
        }
        else
        {
            Label1.Text = "Error while Registering";
            Label1.ForeColor = System.Drawing.Color.Red;
        }
    }

    public string encryptpass(string password)
    {
        string msg = "";
        byte[] encode = new byte[password.Length];
        encode = Encoding.UTF8.GetBytes(password);
        msg = Convert.ToBase64String(encode);
        return msg;
    }
}

Output chamber

Output

Save table Data.

Save table data


Similar Articles