I need to pass some encrypted value through URL. Is there any way to avoid some characters such as slash (/) in the value we are getting after encryption? Because in codeigniter, character such as slash is used for separating parameters in the URL. Please note that i don't want any suggestion to don't pass the encrypted string in URL :)
4 Answers
Use the PHP urlencode function after encryption: http://php.net/manual/en/function.urlencode.php and use urldecode in the script that's processing the GET data.
1 Comment
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);
}
}
-
$key = $this->config->item('encryption_key');
$outboundlink = urlencode( $this->encrypt->encode($segment, $key, TRUE) );
$inboundlink = rawurldecode( $this->encrypt->decode( $segment, $key) );
Comments
One idea is to represent encrypted string as hex using only 0-9 and a-f and optionally x.
But i suggest you to use TLS (better SSL) for protecting your data, because now you probably encrypt data using JS so keys are visible to attacker.
Do not call current scheme security or encryption, but just obfuscation.
1 Comment
What you want is not an encryption (which implies security) but an encoding. In practice, your options are basically:
- urlencode (Use this if you want the string to be somewhat legible. This also will give you a shorter string than the other two options)
- base64 (use this if you don't want the string to be legible and also want to conserve space in your string)
- hex (use this if you don't want the string to be legible and you don't care about string length)
I wouldn't choose hex. Decide whether you want the string to be legible and pick one of the two first.