解密使用jsonparser从php发送的数据时出错

时间:2016-04-04 19:26:25

标签: php android encryption

我正在尝试使用我的服务器中的HTTP请求(Jsonparser)解密从Android收到的数据,解密数据的行在下面的代码中进行了注释。 android中出现错误:解析数据org.json.JSONException时出错,数据未传递给服务器。当我评论这些行时,数据正常到达服务器。如何在将数据插入数据库之前解密数据?

<?php 
include("dbConnect.php");
$ID =$_POST["id"];
    $Tencrypted =$_POST["temp"];
    $Pencrypted =$_POST["pulse"];
$Mencrypted =$_POST["motion"];
//$mcrypt = new MCrypt();

//$Tdecrypted = $mcrypt->decrypt($Tencrypted);

//$Pdecrypted = $mcrypt->decrypt($Pencrypted);
//$Mdecrypted = $mcrypt->decrypt($Mencrypted);

if($ID=="1")
$query = "INSERT INTO William(Temperature,Pulse,Motion) VALUES('$Tencrypted','$Pencrypted','$Mencrypted')";
else if($ID=="2")
$query = "INSERT INTO Jack(Temperature,Pulse,Motion) VALUES('$Tencrypted','$Pencrypted','$Mdecrypted')";
else if($ID=="3")
$query = "INSERT INTO Sam(Temperature,Pulse,Motion) VALUES('$Tencrypted','$Pencrypted','$Mencrypted')";

    $result = mysql_query($query) or die(mysql_error());
$arr=array('success'=>$result);
echo json_encode($arr);
/*class MCrypt
{
    private $iv = 'fedcba9876543210'; #Same as in JAVA
private $key = '0123456789abcdef'; #Same as in JAVA

function __construct()
{
}

 * @param string $str
 * @param bool $isBinary whether to encrypt as binary or not. Default is: false
 * @return string Encrypted data

function encrypt($str, $isBinary = false)
{
    $iv = $this->iv;
    $str = $isBinary ? $str : utf8_decode($str);
    $td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
    mcrypt_generic_init($td, $this->key, $iv);
    $encrypted = mcrypt_generic($td, $str);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $isBinary ? $encrypted : bin2hex($encrypted);
}

 * @param string $code
 * @param bool $isBinary whether to decrypt as binary or not. Default is: false
 * @return string Decrypted data

function decrypt($code, $isBinary = false)
{

    $code = $this->hex2bin($code);
              $iv = $this->iv;

              $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

              mcrypt_generic_init($td, $this->key, $iv);
              $decrypted = mdecrypt_generic($td, $code);

              mcrypt_generic_deinit($td);
              mcrypt_module_close($td);

              return utf8_encode(trim($decrypted));
}
protected function hex2bin($hexdata)
{
    $bindata = '';
    for ($i = 0; $i < strlen($hexdata); $i += 2) {
        $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
    }

    return $bindata;
}
}

*/  
?>

感谢您耐心等待阅读代码。请帮帮我。

0 个答案:

没有答案