1

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.

2
  • Is the encryption level important, or it's just an aesthetic thing? Try base 64 maybe Commented Dec 1, 2014 at 6:10
  • for security reason I want to encrypt the id Commented Dec 1, 2014 at 6:14

6 Answers 6

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

it will generate the url safe string which is a good point, but (I think) question is about the permalink means the encoded string for 6 should be same every time when we encrypt 6. so that a user can use it as a permalink or absolute link
Yes it will be same and it will only change if you pass different key for encoding and decoding
@RanaSoyab, I tried your code but I am getting error undefined property: user_control::$encrypt
Should I add the file name in the config file?
0

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

Comments

0

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~%.:_\-\+=';

Comments

0

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

Comments

0

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

Comments

-1

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

1 Comment

Is not an explantion needed anwser..... It's only the new library... for everyone as i that fall in this tread, the anwsers in here doesnt work on php 7.. after 2 hours of searching arround how can be done i found that was been a codeigniter update and have a new library... the -1 is really needed for try to help the people who have php7? thank you then ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.