chandu gummadi

chandu gummadi

  • NA
  • 67
  • 71.6k

CONVERT PASSWORD ENCRYPT TO DECRYPT AND COMPARE

Mar 21 2013 1:18 AM
HI friends iam saving password in to database in encrypt format but again i want convert database encrypt password in to decrypt but iam not getting this one.

it is encrypted and saved in database properly but it is not decrypted.
please any one help me send me some examples

 protected void btnsave_Click(object sender, EventArgs e)
    {
        //HashPassword cs = new HashPassword();
        string uid = txtuserid.Text;
        string pwd = txtpwd.Text;
        string strencrypt = Encrypt(uid, "NIC");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into zzzTestPwd values('" + txtuserid.Text.Trim() + "','" + strencrypt + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();

        //string enc =encrypt(txtpwd.text, "nic");
        ////dbsappl.execute("insert into login_tbl values('" + drop_dept.selectedvalue + "','" + txtfullname.text + "','" + txtuserid.text + "','" + enc + "','y')");
        //dbsappl.execute("insert into zzztestpwd values('" + txtuserid.text + "','" + enc + "','y')");
        lblMsg.Text = "user created successfully";
        lblMsg.Visible = true;
    }
    public string Encrypt(string input, string key)
    {
        string strReturnVal = "";
        byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);
        TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
        tripleDES.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
        //tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
        tripleDES.Mode = CipherMode.ECB;
        tripleDES.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = tripleDES.CreateEncryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
        tripleDES.Clear();
        strReturnVal = Convert.ToBase64String(resultArray, 0, resultArray.Length);
        return strReturnVal;

    }
protected void btnlogin_Click(object sender, EventArgs e)
    {
        string uid = txtuserid.Text;
        string pwd = txtpwd.Text;
        //string strdecrypt=Decrypt(uid,pwd);
        try
        {
            string CPass = txtpwd.Text.Trim();

            con.Open();
            DataTable dt = new DataTable();
            SqlDataAdapter ada = new SqlDataAdapter("select Password from zzzTestPwd where UserId='" + txtuserid.Text + "'", con);
            ada.Fill(dt);
            con.Close();
            if (dt.Rows.Count > 0)
            {
                string t = dt.Rows[0]["Password"].ToString();
                string Dec = Decrypt(dt.Rows[0]["Password"].ToString(), "NIC");
                string NPass = FormsAuthentication.HashPasswordForStoringInConfigFile(Dec + Session["rnd"].ToString(), "MD5").ToLower();
                if (CPass == NPass)
                {
                    lblmsg.Visible = true;
                    lblmsg.Text = "Matched";
                }
                else
                {
                    lblmsg.Visible = true;
                    lblmsg.Text = "passwords miss matched";
                }
            }
        }
        catch (Exception ex) { }
    }
    public string Decrypt(string input, string key)
    {



        byte[] inputArray = Convert.FromBase64String(input);
        TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
        tripleDES.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
        //tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
        tripleDES.Mode = CipherMode.ECB;
        tripleDES.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = tripleDES.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
        tripleDES.Clear();
        return UTF8Encoding.UTF8.GetString(resultArray);
    }
thanking you