Reputation: 97
Hi I am making an app using CodeIgniter.
I want to encrypt the numeric id into an encrypted string.
from
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.
Upvotes: 1
Views: 17132
Reputation: 896
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.
Upvotes: 6
Reputation: 341
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);
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 109
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
Upvotes: -1
Reputation: 140
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<br>$x<br><br>";
}
}
}
Cosidering config/config.php has fallwowing set:
$config['encryption_key'] ="key_here"
and
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-\+=';
Upvotes: 0
Reputation: 205
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);
Upvotes: 0