The hash algorithm is allowed to create larger number of data using the small number of data.
Step 1: Create a simple login form as in the following screenshot:
By using the following HTML code:
- <asp:Table ID="Table1" runat="server" Width="418px" Height="209px">
- <asp:TableRow>
- <asp:TableCell>
- User Name
- </asp:TableCell>
- <asp:TableCell>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- </asp:TableCell>
- </asp:TableRow>
- <asp:TableRow>
- <asp:TableCell>
- Password
- </asp:TableCell>
- <asp:TableCell>
- <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
- </asp:TableCell>
- </asp:TableRow>
- <asp:TableRow>
- <asp:TableCell>
- <asp:Button ID="Button1" runat="server" Text="Sign Up" OnClick="Message_click" />
- </asp:TableCell>
- <asp:TableCell>
- <asp:Button ID="Button2" runat="server" Text="login" OnClick="login_click" />
- </asp:TableCell>
- <asp:TableCell>
- <asp:Label ID="Label1" runat="server" />
- </asp:TableCell>
- </asp:TableRow>
- </asp:Table>
Step 2: Now create the Database table as in the following screenshot:
Step 3: After creating the table use the following coding in Sign up button to get user name and password:
- public void Message_click(object sender, EventArgs e)
- {
- string username = TextBox1.Text.ToString();
- String password = TextBox2.Text;
-
- string pass = encryption(password);
- Label1.Text = pass;
-
- if (username.Length > 0 && password.Length > 0)
- {
-
- string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
- SqlConnection con = new SqlConnection(connection);
- String passwords = encryption(password);
- con.Open();
-
- String search = "SELECT * FROM UserAccount WHERE (UserName = '" + username + "');";
- SqlCommand cmds = new SqlCommand(search, con);
- SqlDataReader sqldrs = cmds.ExecuteReader();
- if (sqldrs.Read())
- {
- String passed = (string)sqldrs["Password"];
- Label1.Text = "Username Already Taken";
- }
- else
- {
- try
- {
-
- string sql = "INSERT INTO UserAccount (UserName, Password) VALUES ('" + username + "','" + passwords + "');";
- SqlCommand cmd = new SqlCommand(sql, con);
- cmd.ExecuteNonQuery();
- String Message = "saved Successfully";
- Label1.Text = Message.ToString();
- TextBox1.Text = "";
- TextBox2.Text = "";
- Response.Redirect("Default2.aspx");
- }
- catch (Exception ex)
- {
- Label1.Text = ex.ToString();
- }
- con.Close();
- }
- }
-
- else
- {
- String Message = "Username or Password is empty";
- Label1.Text = Message.ToString();
- }
- }
Step 4: Now encrypt the password by using the following method.
- public string encryption(String password)
- {
- MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
- byte[] encrypt;
- UTF8Encoding encode = new UTF8Encoding();
-
- encrypt = md5.ComputeHash(encode.GetBytes(password));
- StringBuilder encryptdata = new StringBuilder();
-
- for (int i = 0; i < encrypt.Length; i++)
- {
- encryptdata.Append(encrypt[i].ToString());
- }
- return encryptdata.ToString();
- }
After inserting the values inside the DB it will be like the following:
In the above Database table the user name is given as it is but the password is in encrypted String format
Step 5: Now login by using already created username and password.
Add the following code in the
Login button. - public void login_click(object sender, EventArgs e)
- {
- String username = TextBox1.Text.ToString();
- String password = TextBox2.Text;
- string con = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
- SqlConnection connection = new SqlConnection(con);
- connection.Open();
- ncrypt the given password
- string passwords = encryption(password);
- String query = "SELECT UserName, Password FROM UserAccount WHERE (UserName = '" + username + "') AND (Password = '"+passwords+"');";
-
- SqlCommand cmd = new SqlCommand(query, connection);
- SqlDataReader sqldr = cmd.ExecuteReader();
- if (sqldr.Read())
- {
- Response.Redirect("Default3.aspx");
- }
- else
- {
- Label1.Text = "User or password is in correct not found";
-
- }
-
- connection.Close();
- }
If the Password is correct it will take us to the Default3.aspx page, else it will show a message in the following label box:
Decryption is not possible using MD5, so I have again converted the entered password and then checked the value with the database.