【问题标题】:functions used to encrypt password in php?用于在php中加密密码的函数?
【发布时间】:2013-01-07 17:23:08
【问题描述】:

我正在编写一个允许用户注册的 PHP 站点,我正在使用 codeigniter php,我想知道加密密码的最佳功能 和这个函数有什么区别?

【问题讨论】:

  • 使用 md5() 函数加密密码你可以使用 $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5') ;在代码点火器中
  • 改进@Arunkumar 的评论:不要使用 md5 加密密码。
  • @Mathieu Imbert 为什么说不要使用 md5 加密密码?
  • @Arunkumar — 坏了。
  • 查看 the password hashing faq — 和哈希密码,不要加密它们。

标签: php codeigniter encryption passwords


【解决方案1】:

密码几乎不应该被加密。相反,它们应该是单向散列。

一般来说,bcrypt is recommended,因为它可以抵抗暴力破解,而常见的替代品如 md5sha1 会失败。

【讨论】:

  • 之前没遇到过bcrypt。听起来我需要更新我的哈希技术..
【解决方案2】:

使用 PHPass:http://www.openwall.com/phpass/

phpass 支持的首选(最安全)散列方法是基于 OpenBSD 样式 Blowfish 的 bcrypt,我们的公共域 crypt_blowfish 包(用于 C 应用程序)也支持,在 PHP 中称为 CRYPT_BLOWFISH,回退到 BSDI -style 扩展的基于 DES 的散列,在 PHP 中称为 CRYPT_EXT_DES,最后的手段是回退到在 phpass 本身中实现的基于 MD5 的加盐和可变迭代计数密码散列(也称为可移植散列)。

把它放在application/third_party,并使用vanilla PHP来加载它(不是CI的加载器):

require_once APPPATH.'third_party/phpass-0.3/PasswordHash.php';
$hash_iterations = 100;
$portable_hashes = FALSE;
$phpass = new PasswordHash($hash_iterations, $portable_hashes);

示例用法:

// Hash a password before storing it in the DB
$hashed_password = $phpass->HashPassword($user_input);

// Check a given password against a stored hashed password
$is_valid = $phpass->CheckPassword($user_input, $stored_hash_of_password);

【讨论】:

    【解决方案3】:

    这是我在 codeigniter 中使用的自定义加密类

    <?php
    class Encryption {
        var $skey   = "EsUriEncKey2012"; 
    
        public  function safe_b64encode($string) {
    
            $data = base64_encode($string);
            $data = str_replace(array('+','/','='),array('-','_',''),$data);
            return $data;
        }
    
        public function safe_b64decode($string) {
            $data = str_replace(array('-','_'),array('+','/'),$string);
            $mod4 = strlen($data) % 4;
            if ($mod4) {
                $data .= substr('====', $mod4);
            }
            return base64_decode($data);
        }
    
        public  function encode($value){ 
    
            if(!$value){return false;}
            $text = $value;
            $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
            $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
            $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->skey, $text, MCRYPT_MODE_ECB, $iv);
            return trim($this->safe_b64encode($crypttext)); 
        }
    
        public function decode($value){
    
            if(!$value){return false;}
            $crypttext = $this->safe_b64decode($value); 
            $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
            $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
            $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->skey, $crypttext, MCRYPT_MODE_ECB, $iv);
            return trim($decrypttext);
        }
    }
    ?>
    

    【讨论】:

    • 不要滚动自己的加密货币。
    • 他正在使用 PHP 的 mcrypt,所以他没有推出自己的加密,只是围绕 AES (Rijndael_256) 构建了一个包装器。即使您仍然应该使用盐对密码进行哈希处理,而不是使用双向算法。
    • 这是一种糟糕的密码存储方式。 不要使用它。
    猜你喜欢
    • 2014-06-25
    • 1970-01-01
    • 2015-07-30
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    相关资源
    最近更新 更多