Codeigniter:在将表单提交给控制器之前,我如何加密密码


Codeigniter: How can I encrypt password before submitting the form to the controller?

我有一个简单的html登录表单

<form method="post" action="http://www.example.com/login">
    <input type="text" name="text_box" />
    <input type="password" name="pass_word" />
    <input type="submit" name="submit">
</form>

当我提交表格时,在控制器中

public function login(){
    $pass_word = $this->input->post('pass_word');
    die($pass_word);
}

这里的问题是,它显示了普通密码,如果我在控制器中键入123456,我会得到123456

  1. 这是安全问题吗
  2. 任何人都可以通过wireshark这样的网络监控工具获取我的密码吗

如何避免这种情况?在发送到控制器之前,我可以加密视图中的密码吗?

如果您的帖子数据(密码等)被截获,那么它将以明文形式显示。使用SSL/HTTPS将为您发送的数据提供加密。我不会依赖客户端JavaScript或任何类似的东西来验证/登录用户。它可能也会给你的用户更多的信心,看到一个安全的连接正在被使用。

首先,我只是读了一些关于SSL和HTTPS的一般内容,以及SSL证书——Wiki、谷歌和SO都是不错的地方,那里有很多信息。

对于在CI中使用SSL/HTTPS,我发现以下内容很有用:

  • http://sajjadhossain.com/2008/10/27/ssl-https-urls-and-codeigniter/
  • http://nigel.mcbryde.com.au/2009/03/working-with-ssl-in-codeigniter/
  • 如何让CodeIgniter使用SSL加载特定页面

特别是Nigel的帖子中的forcessl函数:

在application/helper中创建一个名为ssl_helper.php 的文件

if (!function_exists('force_ssl'))
{
    function force_ssl()
    {
        $CI =& get_instance();
        $CI->config->config['base_url'] =
                 str_replace('http://', 'https://',
                 $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] != 443)
        {
            redirect($CI->uri->uri_string());
        }
    }
}
function remove_ssl()
{
    $CI =& get_instance();
    $CI->config->config['base_url'] =
                  str_replace('https://', 'http://',
                  $CI->config->config['base_url']);
    if ($_SERVER['SERVER_PORT'] != 80)
    {
        redirect($CI->uri->uri_string());
    }
}

加载助手,然后在任何控制器的构造函数中需要ssl,只需插入:

force_ssl();

在每个你不想让ssl放入的控制器中:

if (function_exists('force_ssl')) remove_ssl();

这是一种编程方法,另一种方法是使用.htaccess(如果您使用的是Apache)。

是的,这肯定是一个安全问题。就像你说的,任何拥有Wireshark(或类似产品)的人都有可能拦截它

这可能不是你想要依赖JavaScript的东西。一般来说,你会使用像SSL/HTTPS这样的东西,有一篇博客文章(有点旧)详细介绍了如何使用CodeIgniter,但它很常见,所以你可以在谷歌上找到一个更新的教程。

http://sajjadhossain.com/2008/10/27/ssl-https-urls-and-codeigniter/http://nigel.mcbryde.com.au/2009/03/working-with-ssl-in-codeigniter/.

更新:Lefter的答案提供了更多细节,并且是正确的,请同时查看/而不是

浏览http://codeigniter.com/user_guide/libraries/encryption.html。关于加密和解密的一切都在这里描述。

执行数据加密并将其作为字符串返回。示例:

$pass_word = $this->encrypt->encode(input->post('pass_word'));  

解密编码字符串。示例:

$encrypted_pass_word = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';
$plaintext_pass_word = $this->encrypt->decode($encrypted_pass_word);

如果你不想使用配置文件中的第二个参数,你可以选择通过第二个来传递加密密钥。通过链接阅读有关它的详细说明。

$password = sha1($this->input->post('password'));
or 
$hash= $this->input->post('password');
$password= $this->encrypt->sha1($hash);

您可以使用md5而不是sha1,但sha1d5d5&sha1是不可解码的

使用https有一种更简单的方法。

我写了以下几行/application/config/config.php文件,它可以完美地工作:

$protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) ? 'https' : 'http';
$config['base_url'] = $protocol.'://www.yoursite.com';