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 :)
问题:
回答1:
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.
回答2:
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.
回答3:
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) );
回答4:
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.
回答5:
it's not correct method but it may help you a lot. I have done some alternating method for this.
<html>
<a href="<?= base_url('controller/category/').$this->encrypt->encode($category_men_top->category_id);?>">Category Name</a>
</html>
public function category()
{
$count=$this->uri->total_segments(); //getting total number of segment
$id=''; //assigning value
for ($i=3; $i<=$count ; $i++) {
$id=$id.$this->uri->segment($i).'/';
}
$id=rtrim($id,"/"); // removing '/' from the end of the whole string
echo "$id";
echo "<br>";
echo $this->encrypt->decode($id);
}