/** * url_crypt加密算法的解密函数 * * @param string $tpl 产品线的tpl标识 * @param string $key 加密所用的key * @param string $hash 16进制密文字符串 * @return 加密的原文或者false */ public static function url_decrypt($tpl, $key, $hash) { if (empty($hash) || strlen($hash) <= 4) { return false; } // first checek crc16 $hash = strtolower($hash); $fcrypt_hstr = substr($hash, 0, -4); $fcrypt_crc16 = intval(base_convert(substr($hash, -4), 16, 10)); // check crc16 if ($fcrypt_crc16 !== self::_crc16($fcrypt_hstr)) { return false; } // decode hstr to binary str // same use pack not hex2bin, nibble is not defined clearly $fcrypt_hash = pack('H*', $fcrypt_hstr); // decode hash to original stoken return fcrypt_decode_hmac(self::_buildFcryptKey($tpl, $key), $fcrypt_hash); }
/** * @brief 解密图片id * @param 加密前的字符串 * @return 正确返回pic_id,错误返回false * @author wenweidong * @date 2012/03/21 11:31:37 * @note */ public function decode_pic_url_crypt($strCrypt) { $data = pack('H*', $strCrypt); $data = fcrypt_decode_hmac($this->crypt_key, $data); if (strlen($data) == 8) { $result = $this->unpackUInt32(substr($data, 0, 4)); $intID1 = $result; $result = $this->unpackUInt32(substr($data, 4, 4)); $intID2 = $result; return $intID2; } else { if (strlen($data) == 16) { $intID1 = $this->unpackUInt64(substr($data, 0, 8)); $intID2 = $this->unpackUInt64(substr($data, 8, 8)); return $intID2; } else { return false; } } }
protected function _readResponse($handler) { //读取nshead $headbuf = $handler->read(36); if (!$headbuf) { $this->_log("read nshead failed\n"); $this->_last_errno = ZCACHE_CLIENT_ERR_READ; $this->_last_errmsg = "net read err"; return NULL; } //解析nshead $head = zcache_split_nshead($headbuf); if (!isset($head['body_len'])) { $this->_log("no body_len in nshead\n"); $this->_last_errno = ZCACHE_CLIENT_ERR_NSHEAD; $this->_last_errmsg = "nshead err"; return NULL; } //读取数据包内容 $retbuffer = $handler->read($head['body_len']); if (!$retbuffer) { $this->_log("read nsbody " . $head['body_len'] . " failed\n"); $this->_last_errno = ZCACHE_CLIENT_ERR_READ; $this->_last_errmsg = "net read err"; return NULL; } if ($head['reserverd']) { $decrypt_data = fcrypt_decode_hmac('key', $retbuffer); //$this->_log("fcrypt_decode_hmac: " . strlen($retbuffer) . " " . strlen($decrypt_data) ); return $decrypt_data; } else { return $retbuffer; } }