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

时间:2020-07-07 18:33:31

标签: codeigniter codeigniter-4

我正在使用codeigniter 4。 尝试对视图中的URL ID进行加密时,为什么我的代码显示错误?

视图:

<?php $
  encrypter = \Config\Services::encrypter(); 
  $data1 = $value['id_aktivitas'];                         
  $data1 =  $encrypter->encrypt($data1);
?>  

<a href="<?= base_url('aktivitas/edit_aktivitas/'.$data1) ?>" class="btn btn-warning">Edit</a>

这是我的控制器:

 public function edit_aktivitas($id)
 {
    $encrypter = \Config\Services::encrypter();     
    $id = $encrypter->decrypt($id);
    $data=['aktivitas'  => $this->AktivitasModel->edit_aktivitas($id)];
 }

这是我的模特:

    public function edit_aktivitas($id)
    {      
        return $this->db->table('t_aktivitas')->where('id_aktivitas', $id)->get()->getRowArray();
    }

我收到此错误

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

2 个答案:

答案 0 :(得分:1)

好吧,我遇到了同样的错误,我试图修复它,所以这对我来说是很好的解决方案。

  1. 设置启动键@ App \ Config \ Encryption.php后创建新密钥
  2. 创建编码密钥: 设置您的加密密钥 您的加密密钥必须在使用的加密算法允许的范围内。对于AES-256,长度为256位或32个字节(字符)。

密钥应该尽可能地随机,并且不能是常规文本字符串,也不能是哈希函数的输出等。要创建正确的密钥,可以使用加密库的createKey()方法。 / p>

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

$key = Encryption::createKey(32);

密钥可以存储在app / Config / Encryption.php中,或者您可以设计自己的存储机制,并在加密/解密时动态传递密钥。

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

public $key = 'YOUR KEY';

注意:通过复制和过去传递生成的密钥会损坏二进制表示形式,因此请继续阅读

编码键或结果

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

// Get a hex-encoded representation of the key:
  $encoded = bin2hex(Encryption::createKey(32));
  echo $encoded

如果您使用base64_encode(),请确保在下面的构造函数中以及在加密消息时使用base64_decode()

例如:

// if you use base64_encode do this
 $message = 'some message to encode';
 $encrypter = \Config\Services::encrypter();
 $encodedMsg = base64_encode($encrypter->encrypt($message));

 // decrypt the message 
 $txt = $encrypter->decrypt(base64_decode($encodedMsg))
 echo $txt;

// and in your App\Config\Encryption.php Constructor that will 
// dynamically decode the key to binary safe use 
base64_decode($encodedKey) 
// only if you encode the key using the 
base64_encode($key)

//And if you use 
bin2hex($key) 
// during key creation in the constructor use:
hex2bin($encodedKey) 
// and when transporting your message over url use: 
bin2hex($encodedMessageFromEncrypter->encrypt($msg)) 
// and decode it using 
hex2bin($transportedMessage)

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

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

//,以便仍将其作为二进制传递给库: 像这样:

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

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

答案 1 :(得分:0)

我也有同样的问题。正如上面 OBI PASCAL BANJUARE 创建 加密密钥 的回答所述,这很重要,但还要注意 $data1 = $encrypter->encrypt($data1); 生成二进制数据。 不要将二进制数据保存在数据库中,而是使用 bin2hex()base64_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;
相关问题