Thursday 8 November 2012

how to encrypt password MD5 in asp.net


<script src="md5.js"  type="text/javascript"></script>

  <script type="text/javascript">
      function form_validate(str) {
     
          if (document.getElementById("<%=txtcurrentpassword.ClientID%>").value == "") {
              alert("Please enter current password");
              document.getElementById("<%=txtcurrentpassword.ClientID%>").focus();
              return false;
          }
          if (document.getElementById("<%=txtnewpass.ClientID%>").value == "") {
              alert("Please enter New password");
              document.getElementById("<%=txtnewpass.ClientID%>").focus();
              return false;
          }
          if (document.getElementById("<%=txtconpass.ClientID%>").value == "") {
              alert("Please enter conform New password");
              document.getElementById("<%=txtconpass.ClientID%>").focus();
              return false;
          }

          if (document.getElementById('<%= txtcurrentpassword.ClientID %>').value != "") {
                                     
              var Encryptpwd = md5(document.getElementById('<%= txtcurrentpassword.ClientID %>').value);

              alert(Encryptpwd);
           
              alert(str);
         
              var Rand = md5(str + (Encryptpwd));
              alert(Rand);
              document.getElementById('<%= txtcurrentpassword.ClientID %>').value = Rand;
              alert('hi');
              //return true;
          }

          if (document.getElementById('<%= txtnewpass.ClientID %>').value != "") {

              var Encryptpwd = md5(document.getElementById('<%= txtnewpass.ClientID %>').value);

              document.getElementById('<%=hidden1.ClientID %>').value = Encryptpwd;

                       
          }  
       
          if (document.getElementById('<%= txtconfpass.ClientID %>').value != "") {
           
              var Encryptpwd = md5(document.getElementById('<%= txtconpass.ClientID %>').value);

              document.getElementById('<%=hidden2.ClientID %>').value = Encryptpwd;

              return true;
          }    
      }            
    </script>    
 <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <table cellpadding="0" cellspacing="0"  width="100%" class="tableheight"  align="center" style="padding:25px;vertical-align:top;">
            <tr>
        <td align="center">
     
            <table cellpadding="0" cellspacing="0" class="col_table1">            
            <tr>
                                <td class="col2" align ="right">
                                        Current Password
                                </td>
                                <td class="Col_Right1">
                                <asp:TextBox ID ="txtcurrentpassword" runat ="server" Width="350px" Height="17" MaxLength="10" TextMode="Password"></asp:TextBox>
                                 
                                </td>
            </tr>
            <tr>
                                <td class="col2" align ="right">
                                        New Password
                                </td>
                                <td class="Col_Right1">
                                <asp:TextBox ID ="txtnewpass" runat ="server" Width="350px" Height="17" MaxLength="10" TextMode="Password"></asp:TextBox>                                
                                 
                                </td>
            </tr>
            <tr>
                                <td class="col2" align ="right">
                                        conform Password
                                </td>
                                <td class="Col_Right1">
                                <asp:TextBox ID ="txtconpass" runat ="server" Width="350px" Height="17" MaxLength="10" TextMode="Password"></asp:TextBox>                                    
                                 
                                </td>
            </tr>
            <tr>
                                <td class="Col_Right"  colspan="2" align="center">
                                <asp:Button ID="btnsubmit" runat="server" Text="Submit"
                                      onclick="btnsubmit_Click"/>
           
                                <asp:Button ID="btnreset" runat="server" Text="Reset"
                                         onclick="btnreset_Click" />
                                                       <asp:HiddenField ID="hidden1" runat="server"/>  
                                                       <asp:HiddenField ID="hidden2" runat="server"/>  
              </td>
              </tr>                  
                </table>
               </td>
               </tr>
               </table>
    </ContentTemplate>
    </asp:UpdatePanel>


protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] != null)
        {
            if (!IsPostBack)
            {
                Random rd = new Random();
                Session["rnumb"] = rd.Next();
                string rno = Convert.ToString(Session["rnumb"]);
                btnsubmit.Attributes.Add("onClick", "return form_validate('" + rno + "');");
                       
            }
        }
     
    }

  protected void btnsubmit_Click(object sender, EventArgs e)        
    {
         int id = Convert.ToInt32(Session["UserId"]);

         string curpass = txtcurrentpassword.Text;

         string newpass = txtnewpass.Text;

         string confpass = txtconpass.Text;

         string newpass1 = hidden1.Value;

         string confpass1 = hidden2.Value;

         IQueryable<string> pass = from p in dc.Users
                                   where p.Id == id
                                   select p.Password;

         foreach (string i in pass)
         {
             Session["p"] = i;
         }
         string pName = Session["p"].ToString();
     
         string dtrand =Session["rnumb"].ToString().Trim()+pName;

        string dtmd5 = FormsAuthentication.HashPasswordForStoringInConfigFile(dtrand, "MD5");

        if (dtmd5.ToString().ToLower() == txtcurrentpassword.Text.ToLower())
         {
             if (txtnewpass.Text.ToLower() == txtconpass.Text.ToLower())
             {
                 var query = (from u in dc.Users
                              where u.Id == id
                              select u).FirstOrDefault();

                 query.Password = newpass1.ToString();

           

                 var query1 = (from p in dc.Passwords
                               where p.UId == id
                               select p).FirstOrDefault();
                 query1.Pass = newpass.ToString();

                 dc.SubmitChanges();

                 ScriptManager.RegisterStartupScript(this, GetType(), "msg", "<script>alert('Record successfully Changed');</script>", false);

                 txtcurrentpassword.Text = "";

                 txtnewpass.Text = "";

                 txtconpass.Text = "";

                 txtcurrentpassword.Focus();
             
             }
             else
             {
                 ScriptManager.RegisterStartupScript(this, GetType(), "msg", "<script>alert('Re enter New Password');</script>", false);

                 txtconpass.Text = "";

                 txtnewpass.Focus();
             }
         }
         else
         {
             ScriptManager.RegisterStartupScript(this, GetType(), "msg", "<script>alert('Password Missmatch');</script>", false);

             txtcurrentpassword.Text = "";

             txtnewpass.Text = "";

             txtconpass.Text = "";

             txtcurrentpassword.Focus();
       
         }


No comments:

Post a Comment