Rumah >pembangunan bahagian belakang >tutorial php >php mcrypt_encrypt 后 json_decode结果为null

php mcrypt_encrypt 后 json_decode结果为null

WBOY
WBOYasal
2016-06-20 12:59:021204semak imbas

$key = '1234567890123456';    $iv = '1234567890123456';    $os = array(        'os' => php_uname('s'),        'time' => time(),        );    $os = json_encode($os);    $crypttext = base64_encode((mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $os, MCRYPT_MODE_CBC, $iv)));    $crypttext = base64_decode($crypttext);    $crypttext = (mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypttext, MCRYPT_MODE_CBC, $iv));    $crypttext = json_decode($crypttext);    var_dump($crypttext);

求解


回复讨论(解决方案)

$key = '1234567890123456';$iv = '1234567890123456';$os = array(  'os' => php_uname('s'),  'time' => time(),);var_dump( $os = json_encode($os));$crypttext = base64_encode((mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $os, MCRYPT_MODE_CBC, $iv))); $crypttext = base64_decode($crypttext); var_dump( $crypttext = (mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypttext, MCRYPT_MODE_CBC, $iv)));echo bin2hex($crypttext), PHP_EOL;//尾部多了11个空字符$crypttext = json_decode(trim($crypttext));//删掉就对了var_dump($crypttext);
string(37) "{"os":"Windows NT","time":1428450826}"string(48) "{"os":"Windows NT","time":1428450826}"7b226f73223a2257696e646f7773204e54222c2274696d65223a313432383435303832367d0000000000000000000000object(stdClass)#1 (2) {  ["os"]=>  string(10) "Windows NT"  ["time"]=>  int(1428450826)}

原来也有解码这问题,看到跟原来不同了,一直不清楚原来是空格字符。。。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn