1
votes

Hi I am making an app using CodeIgniter.

I want to encrypt the numeric id into an encrypted string.

from

http://example.com/post/6

to

http://example.com/post/v4th54u654khi3f23of23ir2h398eh2xi012

I tried the built-in Encryption lib

$this->encrypt->encode(6)

but it generates different encrypted strings for each page load, this is not what I want, I want permalink just like Youtube video ID.

I need the encrypted string also decryptedable.

6
Is the encryption level important, or it's just an aesthetic thing? Try base 64 maybe - Damien Pirsy
for security reason I want to encrypt the id - Jerr Wu

6 Answers

6
votes

did you set $config['encryption_key'] = "Test@123"; in config file

OR

you have to pass the key after string

$this->encrypt->encode(6, 'Test@123')

$this->encrypt->decode(6, 'Test@123')

i have extend the core library for this

create a file name MY_Encrypt.php with and put this file in application\libraries

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Encrypt extends CI_Encrypt
{

  function encode($string, $key = "", $url_safe = TRUE) {
      $ret = parent::encode($string, $key);

      if ($url_safe) {
          $ret = strtr($ret, array('+' => '.', '=' => '-', '/' => '~'));
      }

      return $ret;
  }

  function decode($string, $key = "") {
      $string = strtr($string, array('.' => '+', '-' => '=', '~' => '/'));

      return parent::decode($string, $key);
  } 

}  

?>

Now you can use

$this->encrypt->encode(6)

$this->encrypt->decode(6)

this will have same result.

0
votes

Codeigniter provides a library for this, you can just add in autoload.php file and you can use below lines for encode and decode. May be you will encode same value for ex. "$a" multiple times then encoded value $encodedA comes different but when you will go to decode that you will always get $decodedA= 123. $a = '123';$encodedA = $this->encrypt->encode($a);$decodedA = $this->encrypt->decode($encodedA);

0
votes

Simpliest way out of this scenario is :

to encode: $url_val=base64_encode($this->encryption->encrypt($var_to_encode));

to decode: $var_to_encode=$this->encryption->decrypt(base64_decode($url_val));

Test function

function test() {
    for($i=7000;$i<8000;$i++){
        $x=urlencode($this->encryption->encrypt($i));
        $y=$this->encryption->decrypt(urldecode($x));
        if ($y == $i) { //$y != $i cross test
            echo "$y&ltbr&gt;$x&ltbr&gt;&ltbr&gt;";
        }
    }
}

Cosidering config/config.php has fallwowing set:

$config['encryption_key'] ="key_here"

and

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

for generate encrypt id just like Youtube watch id i use Hashids Spark for CodeIgniter

https://github.com/sekati/codeigniter-hashids

the purpose of this helper is implementing Hashids to generate hashes (like YouTube or Bitly) from numbers to obfuscate database IDs.

installation just like at instruction i modify the hashids_helper.php at line 20

require_once FCPATH . 'sparks/sk-hashids/' . HASHIDS_VERSION . '/vendor/Hashids.php';

to

require_once FCPATH . 'vendor/Hashids.php';  //according to your Hashids path
0
votes

Instead of trying with encode and decode you can covert the id as SHA1. sha1 will generate the alphanumeric key, so you will not get url support error also. no need to encode-decode. this will generate a secured encrypted code.

$random_key = sha1($leadDetails->lead_number);
-1
votes

For those how are here and the library not working on php 7 because of mycrpt -> NEW LIBRARY: https://www.codeigniter.com/user_guide/libraries/encryption.html