Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发送邮件时需要解密密码_C#_Asp.net_Email_Encryption - Fatal编程技术网

C# 发送邮件时需要解密密码

C# 发送邮件时需要解密密码,c#,asp.net,email,encryption,C#,Asp.net,Email,Encryption,创建用户时密码凭证将以加密格式保存在数据库中。现在我想要的是 当用户选择“忘记密码”选项时,他需要填写电子邮件ID,并将相应的密码发送到他的电子邮件ID 问题是密码只以加密格式提供, 例如:- 3ab315c4b788dc6de20ff5f64574501f 下面是我用用户名和详细信息发送邮件的代码 DataSet ds = new DataSet(); using (SqlConnection conn = new SqlConnection(System.Configurat

创建
用户时
密码凭证将以加密格式保存在数据库中。现在我想要的是

当用户选择“忘记密码”选项时,他需要填写电子邮件ID,并将相应的密码发送到他的电子邮件ID

问题是密码只以加密格式提供, 例如:-

3ab315c4b788dc6de20ff5f64574501f

下面是我用用户名和详细信息发送邮件的代码

DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT username,password FROM tbl_User Where email= '" + txtEmail.Text.Trim() + "'", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            conn.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txtEmail.Text);
                // Recipient e-mail address.
                Msg.To.Add(txtEmail.Text);
                Msg.Subject = "Password Details";
                Msg.Body = "Hi, <br/>Please check your Login Details<br/><br/>Your Username is: " + ds.Tables[0].Rows[0]["username"] + "<br/><br/>Your Password is: " + ds.Tables[0].Rows[0]["password"] + "<br/><br/>";
                Msg.IsBodyHtml = true;
                // your remote SMTP server IP.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("test@test.com", "********");
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                Msg = null;
                //lbltxt.Text = "Your Password Details Sent to your mail";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Your Password Details Sent to your mail');window.location ='Login.aspx';", true);
                // Clear the textbox valuess
                txtEmail.Text = "";
            }
            else
            {
                Response.Write("<script>alert('Email Id you entered does not exist');</script>");
            }
        }

请指导。

这里有几个问题值得强调

  • 您的密码未加密,而是经过哈希处理

  • 您使用的是md5,它已经被证明坏了一段时间了

  • 首先你应该

    然后你不应该使用md5来散列你的密码-我建议你使用ready第三方解决方案,或者在尝试之前认真阅读这个主题

    然后,当恢复忘记的密码时,我建议您使用新密码重新设置它,而不是尝试发送旧密码(顺便说一句,不可能对密码进行反哈希)


    同样,在理想情况下,您不希望在电子邮件中发送密码,但有一个

    MD5是一个哈希函数,而不是加密函数。散列函数是不可逆的,因此不可能将用户的密码散列转换回其密码(忽略MD5中的任何弱点,或通用暴力或字典攻击)。任何以可恢复格式存储密码的尝试(例如,以可逆加密形式存储密码,或者更糟糕的是,以明文存储密码)都会危及用户的安全。通过电子邮件向用户发送其当前密码不是有效的帐户恢复机制,应该避免这种情况。@Iridium:那么,请告诉我最好的替代方法。我很乐意实现这一点,所以,我正在做的是,我在数据库中将密码保存为纯文本。这样最终用户就可以轻松地检索到它。这样可以吗?不可以,您永远不想在数据库中以纯文本形式存储密码,散列是正确的(只需使用md5以外的内容即可)。我建议使用适当的hash+salt来存储密码,并实现如下内容:。但是,如果您想要快速而肮脏的解决方案,您可以将用户密码设置为随机生成的内容,并使用新哈希更新数据库记录,然后将随机生成的密码发送给用户(但不建议这样做……)
    private string md5(string sPassword)
        {
            MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(sPassword);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            return s.ToString();
        }