Kathirmalan
Kathirmalan

Reputation: 66

Codeigniter decode function not returning value

I have problem with decoding the encoded value after using str_replace(). It works perfect with encoding but when i try to decode it, it returns nothing. i have also configured the $config['encryption_key'] but i am still facing this issue in codeigniter 3.x. please help me to get through this issue.

/*encoding*/
$tmp = $this->encrypt->encode($val);
$encoded_val = str_replace(array('+','/','='),array('-','_',''),$tmp); //removal of specific characters to eliminate uri segment issue
return $encoded_val;

/*decoding*/
$decoded_val = str_replace(array('-','_',''),array('+','/','='),$val);  //getting original encoded value    
$tmp =  $this->encrypt->decode($decoded_val);                    
return $tmp; 



//encryption key
$config['encryption_key'] = "someencryptionkey";

Upvotes: 1

Views: 740

Answers (1)

jeroen
jeroen

Reputation: 91792

Your code cannot work reliably: The only times you will be able to get the original value back, is when the encoded value does not contain any of the -, _ or = characters.

If it does, your replacement will make it impossible to decode because when you do:

$decoded_val = str_replace(array('-','_',''),array('+','/','='),$val); 

you will not get the value back that you had when you used $this->encrypt->encode($val). For example all - characters will be converted to + characters, so if your original encoded value had any, now it will not.

And of course replacing an empty character with = does not really make any sense.

To be able to encode and decode reliably, don't do any character replacements after encoding / before decoding.

Upvotes: 2

Related Questions