我需要通过URL传递一些加密值。有没有办法避免加密后我们得到的值中的某些字符,如斜杠(/)?因为在codeigniter中,斜杠等字符用于分隔URL中的参数。请注意,我不希望任何建议不在URL中传递加密的字符串:)
答案 0 :(得分:5)
加密后使用PHP urlencode
函数:http://php.net/manual/en/function.urlencode.php并在处理GET数据的脚本中使用urldecode
。
答案 1 :(得分:5)
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) );
答案 2 :(得分:0)
一个想法是仅使用0-9和a-f以及可选的x。
将加密的字符串表示为十六进制但我建议您使用TLS(更好的SSL)来保护您的数据,因为现在您可能使用JS加密数据,因此攻击者可以看到密钥。
不要调用当前的方案安全性或加密,而只是混淆。
答案 3 :(得分:-1)
这不是正确的方法,但它可能会帮助你很多。我为此做了一些交替的方法。
<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);
}
答案 4 :(得分:-2)
您想要的不是加密(这意味着安全性),而是编码。在实践中,您的选择基本上是:
我不会选择十六进制。决定你是否希望字符串清晰易读,先选择其中一个字符串。