dongxun1142 2016-03-22 19:09
浏览 113
已采纳

在urlencode / urldecode之前使用加密是否安全。 加密的GET url不会返回相同的值,偶尔会将所有+替换为空格

Hi I am using an encryption function to encrypt my URI before getting it urlencoded and function to finally decrypt it back after I urldecoded in receiving page. Its working fine often but occasionally urldecode replacing all the + with spaces thus making my decrypt function fail.

Here are my encrypt and decrypt functions:

function encryptIt( $q ) {
    $cryptKey  = 'aJB0rGtIn5UB1xG40efydp';
    $qEncoded      = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
    return( $qEncoded );
}

function decryptIt( $q ) {
    $cryptKey  = 'aJB0rGtIn5UB1xG40efydp';
    $qDecoded      = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), base64_decode( $q ), MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ), "\0");
    return( $qDecoded );
}

And here is how I am preparing my GET Url :

                $encrypted_id = encryptIt( $id );

                $uri=urlencode($encrypted_id);
                $uri="http://example.com/xyz.php?id=".$uri; 

And then my receiving script:

               $id=urldecode($_GET['id']);
               $id=decryptIt($id);

Is it safe to use encryption/decryption and URlencode / urldecode at same time? or I am doing it wrong way somewhere .

  • 写回答

1条回答 默认 最新

  • duanke3985 2016-03-22 19:53
    关注

    Don't call urldecode() in the receiver. PHP automatically decodes all the URL parameters before it puts them into $_GET. So you're decoding twice; the automatic decode translates %2B to +, and then your call to urldecode() translates + to space.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来