$key = 'test'; $result_array = array('name' => 'ta', 'age' => 28); $str = encode($result_array); function encode($result_array) { global $key;//JSON_PRETTY_PRINT JSON_UNESCAPED_UNICODE $result = json_encode($result_array, JSON_UNESCAPED_UNICODE); //$result = str_replace(":null", ':""', $result); $str = $result; $block = mcrypt_get_block_size('des', 'ecb'); $pad = $block - (strlen($str) % $block); $str .= str_repeat(chr($pad), $pad); $encode = base64_encode(mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB)); return $encode; } //echo $str;exit; function decode($str) { global $key; $encode = $str; $json = mcrypt_decrypt(MCRYPT_DES, $key, base64_decode($encode), MCRYPT_MODE_ECB); $block = mcrypt_get_block_size('des', 'ecb'); $pad = ord($json[($len = strlen($json)) - 1]); $json = substr($json, 0, strlen($json) - $pad); return $json; } function _encrypt($str) { global $key; $block = mcrypt_get_block_size('des', 'ecb'); $pad = $block - (strlen($str) % $block); $str .= str_repeat(chr($pad), $pad); return base64_encode(mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB)); }