【问题标题】:Password need to be decrypted while sending mail发送邮件时需要解密密码
【发布时间】:2015-05-21 04:19:52
【问题描述】:

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

当用户选择Forgot password 选项时,他需要填写电子邮件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格式

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();
    }

请指导。

【问题讨论】:

  • MD5 是哈希函数,不是加密函数。散列函数是不可逆的,因此不可能将用户的密码散列转换回他们的密码(忽略 MD5 中的任何弱点,或一般的暴力破解或字典攻击)。任何以可恢复格式存储密码的尝试(例如,以可逆加密形式存储密码,或更糟糕的是,以纯文本形式存储)损害用户的安全。能够通过电子邮件向用户发送他们当前的密码并不是一种有效的帐户恢复机制,应该避免这种机制。
  • @Iridium:所以,请告诉我最好的替代方式。很乐意实现这一点

标签: c# asp.net email encryption


【解决方案1】:

这里有几个值得强调的问题。

  1. 您的密码未加密,而是经过哈希处理。

  2. 您使用的 md5 已被证明已损坏一段时间。

首先你应该understand the difference between hashing and encrypting

那么您不应该使用 md5 来散列您的密码 - 我建议您使用现成的 3rd 方解决方案,或者在尝试之前认真阅读该主题。

然后,当恢复忘记的密码时,我会建议您用新密码重新设置它,而不是尝试发送旧密码(顺便说一句。密码是不可能去散列的)。

另外,在理想情况下,您不想通过电子邮件发送密码,但有一个proper mechanism to recover password

【讨论】:

  • 所以,我正在做的是,我将密码作为纯文本保存在数据库中。以便最终用户可以轻松检索它。可以吗??
  • 不,您永远不想将密码作为纯文本存储在数据库中,散列是正确的(只需使用 md5 以外的其他东西)。我建议使用正确的 hash+salt 来存储密码并实现类似这样的东西:stackoverflow.com/a/1102817/3012759。但是如果您想要快速而肮脏的解决方案,您可以将用户密码设置为随机生成的内容并使用新哈希更新数据库记录并将随机生成的密码发送给用户(虽然不推荐这样做......)
猜你喜欢
  • 2016-05-04
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 2010-11-05
  • 2010-11-21
  • 1970-01-01
  • 2013-01-24
相关资源
最近更新 更多