发送邮件时需要解密密码
Password need to be decrypted while sending mail
创建时 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 已被证明已损坏一段时间。
首先你应该understand the difference between hashing and encrypting
那么你不应该使用 md5 来散列你的密码 - 我建议使用现成的第 3 方解决方案或者在尝试之前认真阅读该主题。
然后,当恢复忘记的密码时,我会建议您用新密码重新设置它,而不是尝试发送旧密码(顺便说一句。不可能对密码进行去哈希处理)。
同样在理想情况下,您不想在电子邮件中发送密码,但有一个 proper mechanism to recover password
创建时 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 已被证明已损坏一段时间。
首先你应该understand the difference between hashing and encrypting
那么你不应该使用 md5 来散列你的密码 - 我建议使用现成的第 3 方解决方案或者在尝试之前认真阅读该主题。
然后,当恢复忘记的密码时,我会建议您用新密码重新设置它,而不是尝试发送旧密码(顺便说一句。不可能对密码进行去哈希处理)。
同样在理想情况下,您不想在电子邮件中发送密码,但有一个 proper mechanism to recover password