Home > Back-end >  Codeigniter 4: failed to decrypt encrypted post data
Codeigniter 4: failed to decrypt encrypted post data

Time:12-27

Sorry for my English I'am newbe in Codeigniter 4. I try to decrypt data from encrypted post request. I'm succeed encrypted my data in views, but can't to decrypting back again in my controller after post it with javascript. Here my code My Controller

<?php namespace App\Controllers; 
use CodeIgniter\Controller;
use App\Models\Store_MDL;
class Home extends BaseController
{ 
    protected $ProductModel; 
    protected $request;protected $session;protected $encrypt;
    public function __construct() {
        $this->ProductModel = new Store_MDL();
        $this->request = \Config\Services::request();
        $this->encrypt = \Config\Services::encrypter();
        $this->session = \Config\Services::session();
        $this->session->start();
    }
    public function index()
    {
        $data['encrypter'] = $this->encrypt;
        return view('home_page', $data);
    }

    public function encthis()
    {   
        $val = $_POST['str'];
        $encrypters = $this->encrypt;
        //$ciphertext = $encrypters->encrypt('My secret message');  
        echo $encrypters->decrypt($val);
        //echo $val;
        return view('product/product-v');
    }   
}

here my views home_page.php

<?php $n=0; foreach ($product as $rows){
                    $n  ;
                    //echo count($rows);
                    if($n < count($rows)){
                        $dev = '<div ></div>';
                    }else{$dev = '';}
                    if($n % 2 == 0){
                        echo '<a href="#" value = "'.base_url().'/home/encthis" data-v="'.$encrypter->encrypt($rows['prno']).'" class = "n-link"><div >
                                <div >
                                    <h1 >'.$rows['prname'].'</h1>
                                    <p >
                                        '.$rows['prdescription'].'
                                    </p>
                                    <h2 >Rp. '.$rows['prprice'].'</h2>
                                </div>
                                <div >
                                    <img src="'.base_url().'/public/uploads/'.$rows['prpath'].'/'.$rows['primage'].'" >
                                </div>
                            </div></a>'.$dev;
                    }else{
                        echo '<a href="#" value = "'.base_url().'/home/encthis" data-v="'.$encrypter->encrypt($rows['prno']).'" class = "n-link"><div >
                                <div >
                                    <img src="'.base_url().'/public/uploads/'.$rows['prpath'].'/'.$rows['primage'].'" >
                            </div>
                            <div >
                                <h1 >'.$rows['prname'].'</h1>
                                <p >
                                    '.$rows['prdescription'].'
                                </p>
                                <h2 >Rp. '.$rows['prprice'].'</h2>
                            </div>
                        </div></a>'.$dev;
                    }
                } ?>

than here my script

<script>    
    $('.n-link').on('click', function () {
                var link = $(this).attr('value');
                var pos = $(this).attr('data-v');
                $.post(link, { str:pos})
                  .done(function(data) {
                    document.getElementById("context").innerHTML = data;
                    
                }); 
            }); 
</script>

I always get notification 500 (Internal Server Error).

CodePudding user response:

After struggling hours, i find some clue, just change

$encrypter->encrypt($rows['prno']); with base64_encode($encrypter->encrypt($rows['prno']));

and in controller, change

$encrypters->decrypt($val); with $encrypters->decrypt(base64_decode($val));

Hope its useful

  • Related