首页 > 解决方案 > 如何使用 Codeigniter 从数据库中解密加密密码

问题描述

我正在尝试解密存储在 MySQL Workbench 数据库中的密码。我使用了 codeigniter 的 Encrypt() 函数。它进入数据库就好了。但是当我尝试运行此代码时,出现错误:消息:strlen() 期望参数 1 为字符串,对象给定文件名:libraries/Encryption.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;```

标签: phpcodeigniterencryption

解决方案


BigDog123 欢迎加入社区。

行中的问题

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

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

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

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

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


推荐阅读