Save Encrypted Password In Database In ASP.NET

We make a small registration from 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 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 design for your application by going to registration_demo.aspx and try the code like this:

registration_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.         <style type="text/css">  
  8.             .style1 {  
  9.                 width: 263px;  
  10.             }  
  11.               
  12.             .style2 {  
  13.                 width: 251px;  
  14.             }  
  15.         </style>  
  16.     </head>  
  17.   
  18.     <body>  
  19.         <form id="form1" runat="server">  
  20.             <div>  
  21.                 <table style="width:100%;">  
  22.                     <tr>  
  23.                         <td class="style1"> Username:</td>  
  24.                         <td class="style2">  
  25.                             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  26.                         </td>  
  27.                         <td>  </td>  
  28.                     </tr>  
  29.                     <tr>  
  30.                         <td class="style1"> Email:</td>  
  31.                         <td class="style2">  
  32.                             <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  33.                         </td>  
  34.                         <td>  </td>  
  35.                     </tr>  
  36.                     <tr>  
  37.                         <td class="style1"> Password:</td>  
  38.                         <td class="style2">  
  39.                             <asp:TextBox ID="TextBox3" runat="server" EnableTheming="True" TextMode="Password"></asp:TextBox>  
  40.                         </td>  
  41.                         <td>  </td>  
  42.                     </tr>  
  43.                     <tr>  
  44.                         <td class="style1">  
  45.                             <asp:Label ID="Label1" runat="server"></asp:Label>  
  46.                         </td>  
  47.                         <td class="style2">  
  48.                             <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" /> </td>  
  49.                         <td>  </td>  
  50.                     </tr>  
  51.                 </table>  
  52.             </div>  
  53.         </form>  
  54.     </body>  
  55.   
  56.     </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
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Text;  
  8. using System.Data.SqlClient;  
  9. public partial class _Default: System.Web.UI.Page  
  10. {  
  11.     SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {}  
  14.     protected void Button1_Click(object sender, EventArgs e)  
  15.     {  
  16.         string strpass = encryptpass(TextBox3.Text);  
  17.         SqlCommand cmd = new SqlCommand("insert into tbl_data values(@name,@email,@pass)", con);  
  18.         cmd.Parameters.AddWithValue("@name", TextBox1.Text);  
  19.         cmd.Parameters.AddWithValue("@email", TextBox2.Text);  
  20.         cmd.Parameters.AddWithValue("@pass", strpass);  
  21.         con.Open();  
  22.         int i = cmd.ExecuteNonQuery();  
  23.         con.Close();  
  24.         if (i != 0)  
  25.         {  
  26.             Label1.Text = "Registration Complete";  
  27.             Label1.ForeColor = System.Drawing.Color.ForestGreen;  
  28.         }  
  29.         else  
  30.         {  
  31.             Label1.Text = "Error while Registring";  
  32.             Label1.ForeColor = System.Drawing.Color.Red;  
  33.         }  
  34.     }  
  35.     public string encryptpass(string password)  
  36.     {  
  37.         string msg = "";  
  38.         byte[] encode = new byte[password.Length];  
  39.         encode = Encoding.UTF8.GetBytes(password);  
  40.         msg = Convert.ToBase64String(encode);  
  41.         return msg;  
  42.     }  
  43. }  
Output chamber

Output

Save table Data

Save table Data

 

Up Next
    Ebook Download
    View all
    Learn
    View all