gpt4 book ai didi

codeigniter - 解密显示错误认证失败

转载 作者:行者123 更新时间:2023-12-05 01:35:33 24 4
gpt4 key购买 nike

我正在使用 codeigniter 4。为什么当我尝试在 View 中加密我的 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>

这是我的 Controller :

 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 Decrypting:authentication failed. "

最佳答案

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

  1. 设置初始 key 后创建一个新 key @ App\Config\Encryption.php
  2. 创建一个编码 key :设置您的加密 key 您的加密 key 必须与所使用的加密算法允许的一样长。对于 AES-256,长度为 256 位或 32 个字节(字符)。

key 应尽可能随机,不能是常规文本字符串,也不能是哈希函数的输出等。要创建合适的 key ,可以使用加密库的 createKey() 方法。

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

$key = Encryption::createKey(32);

key 可以存放在app/Config/Encryption.php中,也可以自己设计一个存储机制,在加/解密时动态传递 key 。

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

public $key = 'YOUR KEY';

注意:通过复制和过去将生成的 key 传递到此处会损坏二进制表示,因此请继续阅读下文

编码键或结果

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

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

If you use base64_encode() make sure to use the base64_decode() in the constructor below and in encrypting your messages

例如:

// 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)

#动态传递 key 在你的 App\Config\Encryption.php 中

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

//这样它仍然以二进制形式传递给库:像这样:

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

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

关于codeigniter - 解密显示错误认证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62781769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com