【发布时间】:2016-09-04 03:05:28
【问题描述】:
在我的表格中,密码是加密形式。 我使用 MD5 加密密码。现在,如果数据库中存在 emailid,我想发送密码。 一切正常...但密码以加密形式通过电子邮件发送给用户。
我如何在发送电子邮件之前解密它并将原始密码发送到用户电子邮件。
下面是我的代码..
function forgotpassword() {
$this->layout = "layout_login";
if (!empty($this->request->data)) {
$email = $this->request->data['User']['email'];
if (!empty($email)) {
$user = $this->User->find('first', array(
'conditions' => array(
'User.email' => $this->request->data['User']['email'],
'User.status' => 1
)
));
if(!$user) {
$this->Session->setFlash("No Such E-mail address registerd with us");
} else {
$subject = "Account Password from Kaya Dispatch";
$this->Email->from = 'luckybajpai87@gmail.com';
$to = trim($this->request->data['User']['email']);
$this->Email->sendAs = 'both';
$this->Email->to = $to;
$this->Email->subject = $subject;
$email = $user['User']['email'];
$password = md5($user['User']['password']);
$message = "";
$message .= "Please find the below Email ID and Password of your account: <br/><br/>";
$message .= "<b>Your Email:</b> " .$email. "<br/>";
$message .= "<b>Your Password:</b> " . $password . "<br/>";
$message .= "<br/>Thanks, <br/>Support Team";
if ($this->Email->send($message)) {
$this->Session->setFlash("Password Send Successfully to your email");
} else {
$this->Session->setFlash("Something Went Wrong.Email is not send");
}
}
}
}
}
【问题讨论】:
-
从不。您无法还原 MD5,因为它是哈希
-
你永远不能这样做。散列(你没有使用加密)是单向的,它不能被取消散列。即使可以,您也永远无法获得用户的密码。
-
旁注:解密密码并将其发送给用户确实不是一个好主意,因为它不安全。如果用户忘记密码,更好的方法是重置密码。另外,
md5()不安全,请改用password_hash()和password_verify -
不是一个好主意,原因太多。您应该做的是发送一个唯一的令牌,并在他们使用本世纪的散列函数选择自己的密码(重置)时将其删除。