sessionKey = $sessionKey; return $this; } /** * @param mixed $iv * @return Decode */ public function setIv($iv) { $this->iv = $iv; return $this; } /** * @param mixed $encryptedData * @return Decode */ public function setEncryptedData($encryptedData) { $this->encryptedData = $encryptedData; return $this; } /** * @param mixed $appId * @return Decode */ public function setAppId($appId) { $this->appId = $appId; return $this; } /** * @param $asArray * @return array|mixed * @throws \Exception */ public function decode($asArray) { if (strlen($this->sessionKey) != 24) { throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey); } $aesKey = base64_decode($this->sessionKey); if (strlen($this->iv) != 24) { throw new \Exception('base64解密失败', $this->IllegalIv); } $aesIV = base64_decode($this->iv); $aesCipher = base64_decode($this->encryptedData); $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV); if ($result === false) { throw new \Exception('aes 解密失败', $this->IllegalBuffer); } $dataObj = json_decode($result); if ($dataObj->watermark->appid != $this->appId) { throw new \Exception('aes 解密失败', $this->IllegalBuffer); } if ($asArray) { return get_object_vars($dataObj); } return $dataObj; } }