Search code examples
javascriptcodeigniterpostencryption

Codeigniter 4: failed to decrypt encrypted post data


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 class="divider mb-3 mt-n2"></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 class="row">
                                <div class="col-6">
                                    <h1 class="font-20 mb-0 line-height-l">'.$rows['prname'].'</h1>
                                    <p class="mb-3">
                                        '.$rows['prdescription'].'
                                    </p>
                                    <h2 class="font-16 mb-0">Rp. '.$rows['prprice'].'</h2>
                                </div>
                                <div class="col-6">
                                    <img src="'.base_url().'/public/uploads/'.$rows['prpath'].'/'.$rows['primage'].'" class="img-fluid rounded-circle shadow-xl">
                                </div>
                            </div></a>'.$dev;
                    }else{
                        echo '<a href="#" value = "'.base_url().'/home/encthis" data-v="'.$encrypter->encrypt($rows['prno']).'" class = "n-link"><div class="row">
                                <div class="col-6">
                                    <img src="'.base_url().'/public/uploads/'.$rows['prpath'].'/'.$rows['primage'].'" class="img-fluid rounded-circle shadow-xl">
                            </div>
                            <div class="col-6">
                                <h1 class="font-18 mb-0 line-height-l">'.$rows['prname'].'</h1>
                                <p class="mb-3">
                                    '.$rows['prdescription'].'
                                </p>
                                <h2 class="font-16 mb-0">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).


Solution

  • 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