Home  >  Article  >  Backend Development  >  php mcrypt_encrypt 后 json_decode结果为null,该如何处理

php mcrypt_encrypt 后 json_decode结果为null,该如何处理

WBOY
WBOYOriginal
2016-06-13 12:17:411102browse

php mcrypt_encrypt 后 json_decode结果为null

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn