【问题标题】:How to Decrypt an Encrypted Password from Database using Codeigniter如何使用 Codeigniter 从数据库中解密加密密码
【发布时间】:2021-01-20 01:28:31
【问题描述】:

我正在尝试解密存储在 MySQL Workbench 数据库中的密码。我使用了 codeigniter 的 Encrypt() 函数。它进入数据库就好了。但是当我尝试运行这段代码时,我得到了错误:消息:strlen()期望参数1是字符串,给定文件名的对象:库/加密.php 我想将通过表单输入的密码与数据库中的解密密码进行比较,看看它们是否匹配。我不知道如何纠正这个问题,我知道这可能是一个非常菜鸟的问题,但我很困惑。感谢您的帮助!

                {               
                    $this->db->select("custPassword"); 
                    $this->db->from('customer');
                    $this->db->where('custEmail', $customerEmail);
                    $passencrypted = $this->db->get();
                    
                    $passplain = $this->encryption->decrypt($passencrypted);
                    
                    $this->db->select("custNumber"); 
                    $this->db->from('customer');
                    $this->db->where('custEmail', $customerEmail);
                    $this->db->where('custPassword', $passplain);
                    $query = $this->db->get();
            
                    $count = $query->num_rows();
                    if($count == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;```

【问题讨论】:

  • 通常密码没有加密而是散列。因此,您无需解密密码以将其与未加密的输入进行比较,而是对输入进行哈希处理以与数据库中的哈希密码进行比较。见here

标签: php codeigniter encryption


【解决方案1】:

BigDog123 欢迎加入社区。​​p>

行中的问题

$passencrypted = $this->db->get();
$passplain = $this->encryption->decrypt($passencrypted);

作为 Codeignitor 文档 $this->db->get() 返回作为对象的数据库结果 (CI_DB_result)。因此,当您将 $passencrypted 传递给解密方法时,您传递的是对象而不是字符串。 $this->encryption->decrypt() 接受字符串作为参数。

对于解决方案,您需要使用CI_DB_result类中的result()或其他方法,了解更多here

$passencrypted = $this->db->get()->row();
if (isset($passencrypted))
{
    $passplain = $this->encryption->decrypt($passencrypted->custPassword);
}

注意:最好将密码散列并存储,而不是加密并存储。

【讨论】:

  • 非常感谢!抱歉回复晚了!
猜你喜欢
  • 2021-07-13
  • 1970-01-01
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 2013-04-10
  • 2023-03-17
  • 2015-04-01
  • 1970-01-01
相关资源
最近更新 更多