Codeigniter奇怪的加密密码 [英] Codeigniter strange encryption of password

查看:212
本文介绍了Codeigniter奇怪的加密密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Codeigniter的新用户,并尝试进行用户注册。我遇到了奇怪的事情。起初,我会告诉我在做什么我的密码值:

  $ password = stripslashes($ password); 
$ password = htmlspecialchars($ password);
$ password = md5($ password);
$ password = strrev($ password);

然后我将它保存到数据库:

  $ data = array(
'email'=> $ email,
'reg_type'=> $ reg_type,
' password'=> $ password
);

$ this-> CI-> db-> insert('user',$ data);

无论我输入什么密码,总是保存这个值: e7248fce8990089e402b00f89dc8d14d



当我要登录页面(加密代码是相同的),它返回一个不同的md5值看起来是正确的)。



有人可以解释为什么会发生和如何解决它?或者您也可以提出另一种密码加密方法。



谢谢。

解决方案

空变量





这意味着您的$ password变量为空,您可能在某个地方有一个错误,其中包含您的下一个 code> $ _ POST 代码。



使用Blowfish



如注释中所述,您不应该使用 md5()



如果您有PHP 5.5+,您可以使用 password_hash()函数:



$ password = password_hash($ password,PASSWORD_BCRYPT);



并使用codeigniter post $ c>函数代替 stripslashes() htmlspecialchars()



示例:

  //从表单中获取密码
$ password = $ this-> input-> post('field_name',true); //添加true运行XSS过滤器。

//散列密码
$ password = password_hash($ password,PASSWORD_BCRYPT);

//数组数组
$ data = array(
'email'=> $ email,
'reg_type'=> $ reg_type,
'password'=> $ password
);

//保存到DB
$ this-> CI-> db-> insert('user',$ data);



编辑



password_hash()自行处理盐化。我从代码中删除了额外的salting。 (见@martinstoeckli评论)


I'm new to Codeigniter and trying to make a user registration. And I've met strange thing. At first I'll tell what exactly I'm doing with my password value:

$password = stripslashes($password);
$password = htmlspecialchars($password);
$password = md5($password);
$password = strrev($password);

And then I'm saving it to the DB:

$data = array(
    'email'     => $email,
    'reg_type'  => $reg_type,
    'password'  => $password
);

$this->CI->db->insert('user', $data);

And no matter what password I enter, It's always saving this value: e7248fce8990089e402b00f89dc8d14d

And when I'm going to login page (code of encryption is the same), it's returning me a different md5 values (and it's look like correct).

Can somebody explain why it's happens and how to solve it? Or maybe you can propose some another method of password's encryption.

Thank you.

解决方案

Empty variable:

e7248fce8990089e402b00f89dc8d14d is the reversed hash of an empty string.

This means your $password variable is empty, you probably have a bug somewhere with your $_POST code.

Use Blowfish:

As mentioned in the comments, you shouldn't use md5() any more.

If you have PHP 5.5+ you can use the password_hash() function:

$password = password_hash($password, PASSWORD_BCRYPT);

And use the codeigniter post() function instead of stripslashes() and htmlspecialchars().

Example:

//Get password from form
$password = $this->input->post('field_name', true); //adding true runs the XSS filter.

//Hash Password
$password = password_hash($password, PASSWORD_BCRYPT);

//Data Array
$data = array(
    'email'     => $email,
    'reg_type'  => $reg_type,
    'password'  => $password
);

//Save to DB
$this->CI->db->insert('user', $data);

EDIT:

password_hash() handles salting on it's own. I removed the additional salting from the code. (See @martinstoeckli comment)

这篇关于Codeigniter奇怪的加密密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆