Codeigniter 解密显示错误身份验证失败

Codeigniter 解密显示错误身份验证失败,codeigniter,codeigniter-4,Codeigniter,Codeigniter 4,我正在使用CodeIgniter4。 为什么我的代码在试图加密视图中的URL ID时显示错误 视图: 这是我的模型: public function edit_aktivitas($id) { return $this->db->table('t_aktivitas')->where('id_aktivitas', $id)->get()->getRowArray(); } 我犯了这个错误 “CodeIgnite

我正在使用CodeIgniter4。 为什么我的代码在试图加密视图中的URL ID时显示错误

视图:

这是我的模型:

    public function edit_aktivitas($id)
    {      
        return $this->db->table('t_aktivitas')->where('id_aktivitas', $id)->get()->getRowArray();
    }
我犯了这个错误

“CodeIgniter\Encryption\Exceptions\EncryptionException解密: 身份验证失败。“


我遇到了同样的错误,兄弟,我试图修复它,所以这是我的解决方案,对我来说非常有效

  • 在设置starter key@App\Config\Encryption.php之后创建一个新密钥
  • 创建编码密钥: 设置加密密钥 您的加密密钥必须与使用的加密算法的长度相同。对于AES-256,长度为256位或32字节(字符)
  • 密钥应尽可能随机,不能是常规文本字符串,也不能是哈希函数的输出等。要创建正确的密钥,可以使用加密库的createKey()方法

    //$key将被分配一个32字节(256位)的随机密钥

    $key = Encryption::createKey(32);
    
    密钥可以存储在app/Config/Encryption.php中,也可以设计自己的存储机制,并在加密/解密时动态传递密钥

    要将密钥保存到app/Config/Encryption.php,请打开文件并设置:

    public $key = 'YOUR KEY';
    
    注意:通过copy和pass在此处传递生成的密钥将损坏二进制表示,因此继续读取

    编码键或结果

    您会注意到createKey()方法输出二进制数据,这很难处理(即复制粘贴可能会损坏它),因此您可以使用bin2hex()、hex2bin()或Base64编码 以更友好的方式使用钥匙。例如:

    // Get a hex-encoded representation of the key:
      $encoded = bin2hex(Encryption::createKey(32));
      echo $encoded
    
    如果使用base64_encode(),请确保在下面的构造函数和加密消息时使用base64_decode()

    e、 g:

    #动态传递密钥 在App\Config\Encryption.php中

    //创建一个构造函数,该构造函数将使用hex2bin()在配置中动态放置相同的值

    //因此,它仍然作为二进制文件传递到库: 像这样:

    public function __construct(){
       //copy the encoded key and pass it in here
       $this->key = hex2bin($encoded);
    }
    

    然后再次尝试加密和解密文本

    我也有同样的问题。如上所述,通过创建加密密钥非常重要,但还要注意
    $data1=$encrypter->encrypt($data1)生成二进制数据。
    不要在数据库中保存二进制数据,而是使用
    bin2hex()
    base64\u encode()
    (尚未尝试base64)对生成的二进制数据进行编码,然后保存。例如:

    加密

    encrypter = \Config\Services::encrypter(); 
    $data = 'SOME_TEXT';                         
    $encrypted_data =  bin2hex($encrypter->encrypt($data));
    
    $decrypted_data = $encrypter->decrypt(hex2bin($encrypted_data));
    echo $decrypted_data;
    
    解密

    encrypter = \Config\Services::encrypter(); 
    $data = 'SOME_TEXT';                         
    $encrypted_data =  bin2hex($encrypter->encrypt($data));
    
    $decrypted_data = $encrypter->decrypt(hex2bin($encrypted_data));
    echo $decrypted_data;
    
    $decrypted_data = $encrypter->decrypt(hex2bin($encrypted_data));
    echo $decrypted_data;