将加密数据传递到url并通过php解密

时间:2016-07-09 10:26:18

标签: php

这是我的PHP代码,我将一些数据以加密形式传递给url。

<?php 
$cancel = encrypt($_GET['id'] . '|' . hotel($_GET['id'], 'area') . '|' . $_GET['roomid']);
<input type = "hidden" name = "cancel_return" value = "<?php echo ROOT_URL; ?>/canceled.php?data=<?php echo $cancel;?>" >

这是我获取网址数据的下一页:

$custom_decrypt = $_GET['data'];
$res = decrypt($custom_decrypt);
print_r( $res);

此加密和解密代码:

    function encrypt($text){

        $salt = 'DFS65'; $key = md5($salt);
        $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$key, $text, MCRYPT_MODE_CBC, md5($key));
        $encrypted = base64_encode($encrypted); return $encrypted;

      } 

   function decrypt($text) {
        $salt = 'DFS65';
        $key = md5($salt); 
        $data = base64_decode($text); 
        $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key, $data, MCRYPT_MODE_CBC, md5($key)); 
        $decrypted = rtrim($decrypted, "\0"); return $decrypted; 
    }

但我得到了解密结果:

  

3 =E j F 1tt 43a_ŋQ4

2 个答案:

答案 0 :(得分:0)

You errors:

  • 在第一行错过了php脚本的php关闭标记?>
  • 值应通过input值标记
  • 传递
  • 接收者文件网址应位于action
  • form参数中
  • var的名称应该通过input名称参数
  • 传递

这是你固定的代码。它已被修改以进行调试,因此请检查并根据您的需要进行更新

<强>的index.php:

<?php

$_GET['id'] = '1';
$_GET['roomid'] = '2';

function encrypt($text){

    $salt = 'DFS65'; $key = md5($salt);
    $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$key, $text, MCRYPT_MODE_CBC, md5($key));
    $encrypted = base64_encode($encrypted); return $encrypted;

}

function decrypt($text) {
    $salt = 'DFS65';
    $key = md5($salt);
    $data = base64_decode($text);
    $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key, $data, MCRYPT_MODE_CBC, md5($key));
    $decrypted = rtrim($decrypted, "\0"); return $decrypted;
}

?>

<?php $cancel = encrypt($_GET['id']. '|'. $_GET['roomid']);?>

<form action="/canceled.php" method="get">
<input type="input" readonly="readonly" name="data" value="<?php echo $cancel;?>">
<input type="submit" >
</form>

和cancel.php

<?php
function encrypt($text){

    $salt = 'DFS65'; $key = md5($salt);
    $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$key, $text, MCRYPT_MODE_CBC, md5($key));
    $encrypted = base64_encode($encrypted); return $encrypted;

}

function decrypt($text) {
    $salt = 'DFS65';
    $key = md5($salt);
    $data = base64_decode($text);
    $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key, $data, MCRYPT_MODE_CBC, md5($key));
    $decrypted = rtrim($decrypted, "\0"); return $decrypted;
}



$custom_decrypt = $_GET['data'];
$res = decrypt($custom_decrypt);
print_r($res);

<强>输出:

1|2

答案 1 :(得分:0)

看看你的意见:

<input type="hidden" name="cancel_return" value=".......">

输入字段名称为cancel_return,但在服务器上您尝试获取未设置的属性:

$custom_decrypt = $_GET['data'];

实际应该是:

$custom_decrypt = $_GET['cancel_return'];

此($_GET['cancel_return'])将返回加密值。然后,您可以解密该值并使用parse_url从解密的URL中的查询中获取data值。

$cancel_return = $_GET['cancel_return'];                       // Get the CORRECT value sent from the client 
$cancel_return_url = parse_url($cancel_return);                // Parse the URL that was passed to the server and return its components
parse_str($cancel_return_url['query'], $cancel_return_query);  // Parse the parameters of the query string of the url
echo decrypt($cancel_return_query['data']);                    // decrypt the 'data' parameter of the query string
相关问题