Вопрос

I would like to encrypt and decrypt the get parameters in CodeIgniter.

I have written the following code:

 <a href="<?php echo site_url('package?product='.$this->encrypt->encode('database').
  '&price='.$this->encrypt->encode('5000')); ?>">BUY NOW</a>

For testing, this in my package.php view in which I have written the following:

 echo $this->encrypt->decode($_GET['product']);
 echo "<br/>";
 echo $this->encrypt->decode($_GET['price']);

But when I click on the link, sometimes it shows both values, sometimes only one of the values and sometimes nothing...

Это было полезно?

Решение

This solves my problem:

Update

Just create the below class in your library folder and name the file Encryption then you can load it in our autoload.php or directly in your controller. See an example:

$this->load->library('encryption'); //in controller
$autoload['libraries'] = array('database','encryption'); // In autoload.php

The Encryption class:

class Encryption {

    var $skey = "MUHAMMAD";

    public function safe_b64encode($string) {

        $data = base64_encode($string);
        $data = str_replace(array('+', '/', '='), array('-', '_', ''), $data);
        return $data;
    }

    public function safe_b64decode($string) {
        $data = str_replace(array('-', '_'), array('+', '/'), $string);
        $mod4 = strlen($data) % 4;
        if ($mod4) {
            $data .= substr('====', $mod4);
        }
        return base64_decode($data);
    }

    public function encode($value) {

        if (!$value) {
            return false;
        }
        $text = $value;
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->skey, $text, MCRYPT_MODE_ECB, $iv);
        return trim($this->safe_b64encode($crypttext));
    }

    public function decode($value) {

        if (!$value) {
            return false;
        }
        $crypttext = $this->safe_b64decode($value);
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->skey, $crypttext, MCRYPT_MODE_ECB, $iv);
        return trim($decrypttext);
    }

}

And to use the library class after loading it, see below:

$encrypt = $this->encryption->encode('Anything...');
$decrypt = $this->encryption->decode($encrypt);

Другие советы

Try to check this configuration, inside application/config/config.php

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';

Maybe the decode is trying to pass some char that is not inside permitted_uri_chars

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top