Encrypt & Decreypt Password in Asp.net c#

Posted by Unknown On Monday, 18 March 2013 0 comments
First store the Password enter by user in string varible.
string encpass = enc_pass(TextBox6.Text);

Now enc_pass function is as follow...

 


now store the encpass string variable in table follow way

con.Open();
SqlCommand cmd = new SqlCommand("user1", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@pass", encpass);
con.Close();

con is connection string variable.


For Decryption password from database to textbox


protected void Button1_Click(object sender, EventArgs e)
    {
       string p=string.Empty;
        int id=0;
        con.Open();
        SqlCommand cmd = new SqlCommand("encpass", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@email", TextBox1.Text);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt=new DataTable();
        adp.Fill(dt);
        if(dt.Rows.Count>0)
        {
            id=Convert.ToInt32(dt.Rows[0]["uid"]);
            p=dt.Rows[0]["upass"].ToString();
        }
        string decpass = dec_pass(p);
        if(decpass==TextBox2.Text)
        {
            Session["login"] = id;
            Response.Redirect("vprofile.aspx");
              }
      
        else
        {
            Label4.Visible = true;
        }
       
        con.Close();
    }
    private string dec_pass(string password)
    {
        string decryptpwd = string.Empty;
        UTF8Encoding encodepwd = new UTF8Encoding();
        Decoder dcode = encodepwd.GetDecoder();
       
        Byte[] todecode_byte = Convert.FromBase64String(password);
        int charcount = dcode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decode_char = new char[charcount];
        dcode.GetChars(todecode_byte, 0, todecode_byte.Length, decode_char, 0);
        decryptpwd = new string(decode_char);
        return decryptpwd;

    }
  


0 comments:

Post a Comment