已解决 php7.2加解密函数 /** * 对明文进行加密 * @param string $text 需要加密的明文 * @return string 加密后的密文 */ public function encrypt($text, $appid) { try { $random = $this->getRandomStr();//"aaaabbbbccccdddd"; $text = $random . pack("N", strlen($text)) . $text . $appid; $iv = substr($this->key, 0, 16); $pkc_encoder = new PKCS7Encoder; $text = $pkc_encoder->encode($text); $encrypted = openssl_encrypt($text,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv); return array(ErrorCode::$OK, $encrypted); } catch (Exception $e) { //print $e; return array(ErrorCode::$EncryptAESError, null); } } /** * 对密文进行解密 * @param string $encrypted 需要解密的密文 * @return string 解密得到的明文 */ public function decrypt($encrypted, $appid) { try { $iv = substr($this->key, 0, 16); $decrypted = openssl_decrypt($encrypted,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv); } catch (Exception $e) { return array(ErrorCode::$DecryptAESError, null); } try { //去除补位字符 $pkc_encoder = new PKCS7Encoder; $result = $pkc_encoder->decode($decrypted); //去除16位随机字符串,网络字节序和AppId if (strlen($result) < 16) return ""; $content = substr($result, 16, strlen($result)); $len_list = unpack("N", substr($content, 0, 4)); $xml_len = $len_list[1]; $xml_content = substr($content, 4, $xml_len); $from_appid = substr($content, $xml_len + 4); } catch (Exception $e) { //print $e; return array(ErrorCode::$IllegalBuffer, null); } if ($from_appid != $appid) return array(ErrorCode::$ValidateAppidError, null); return array(0, $xml_content); }
搜索Widget 城区搜索正常 城市搜索 加密数据无法解析出来- 当前 Bug 的表现(可附上截图) 搜索北京二手房价 请求数据{"ToUserName":"gh_c7606898905c","Encrypt":"ihPDCWTwYnb+UxT30WtJsIGEsXzXtKujXqdKBXQuGpbf8bdj58N/59w3uxbgZZsZ4TJ8VORfmFDOsqbc49p9Czv4mNCKHLofar9aqPZo3uGqHxH1+wrBTbD2z0Sh8ARX0LWNpaiRDILmv7YfCFn14kyyYR+rA1fstpRSZ0ZN6/FqC0+LO5Rfnp1pKwhSskv5JZ7AsggPpR87OXA0b62Fjun7UrBzryDsCLPrP7EuJVg439xEIjPeuRziF3kWM2iJF+myyuToMIVOFST9+M4tavzdMtmaX+xZOtji56YtRnTFtsM2vROjEMpb6VbjFDUC5/5y+pfrjY+2UoueB2qFCkwNyIragwMV6lrBgx1/goFeKJ61cHPbta2bLLPQI7qcPdRiLGBxkIE3+P91of5DDWly1WysglYoPUqDQAIsEPwOtyuVLiP2Ep8wgyXUNjdQRZeZ/HgDCbsjLY4P/7e7qT6tXEWVvo2CTakDhfz/04pv0EUMUi41gVgiybZz/w9EEOIDc9D//IZQwATUvrzKtVKwLx/eA6CegbclhMr2TzmbohCZlQJf5xwtcaXIE4/C1aNrlqe8GIZkpRhoLuLq9RnPInu4dnpIeoAGGlQL67x0eun+722FZnOKYA4qkBqr"} 解析出来为空 当搜索城区的时候正常(昌平二手房价) 请求数据{"ToUserName":"gh_c7606898905c","Encrypt":"OS5bUerjXe94YZfSuhkpxZ+EX+bK4A62k9Y8bAjXCuroq4ORCh/d+drYugwXXd+vBlMun5arXpEPfZcfaUGJrFdmbbTTXBiDOCDs+24VPqyztf9doVpri9dUAjuMr42AClyXj17BWFzcN5A5UsgZAhw2xBwMpi+Yp9C7J3LVjo33KSDNN+VO4AcFgrLe63dB0in2DxEo5UC29LUwPFzWnl6sX5cS9eO9VZkCDIxp2QVMdxdTol+jqtSMXmbC+aUqvTZRxYiKMHDEeD5lr7yTQIT1XBwKnXozNYVo7Hs9sjP+3xJkVnRTsRunmcWEs2DHzUr3IUxCLuBJsa/8Wtpe48SsainSNeHMiLPbatRuF/J/N7PoUm3kWKR90Ir/zQ8OE7BlEDhp2DC6rHZpWIOPZJyBQWKX5Txtkvlu2FLXcQrnRGaeDKecYXtIZr5e3xzAYKUHX/oU9wl7Sj0b18YM2LB3cvlzc06T2aoWtfDKfWGtKEdAwQ7HKvU/020n/JaDOhWVxjyIbLt4n8Q9xTKfYC/KXgd8AkmrHV0oIbyk4T644uyEdAcOEcVZXjsht2c/3GDeWcjWq0RZjTa1CShCP72ecBWCAQ6gszLv3WmLe/eQS1r1gUrndGc6ceMHBen4"} 解析数据 [0,"{\"ToUserName\":\"gh_c7606898905c\",\"FromUserName\":\"oITbH5cw7KCyArdxfEn_ATL5Nbtc\",\"CreateTime\":1541402593,\"MsgType\":\"event\",\"Event\":\"wxa_widget_data\",\"CacheKey\":\"be5039cb504b6b3ed016e9054767c268\",\"Query\":\"{\\\"type\\\":11,\\\"slot_list\\\":[{\\\"key\\\":\\\"district\\\",\\\"value\\\":\\\"\u660c\u5e73\u533a\\\"},{\\\"key\\\":\\\"gps_city\\\",\\\"value\\\":\\\"\u5317\u4eac\u5e02\\\"}],\\\"longitude\\\":116.466201782,\\\"latitude\\\":39.873622894,\\\"search_id\\\":\\\"2831188717034000340\\\"}\",\"Scene\":1}"] 解析函数 public function decrypt($encrypted, $appid) { try { $iv = substr($this->key, 0, 16); $decrypted = openssl_decrypt(base64_decode($encrypted),'AES-256-CBC',$this->key,OPENSSL_RAW_DATA,$iv); } catch (Exception $e) { return array(ErrorCode::$DecryptAESError, null); } try { //去除补位字符 $pkc_encoder = new PKCS7Encoder; $result = $pkc_encoder->decode($decrypted); //去除16位随机字符串,网络字节序和AppId if (strlen($result) < 16) return ""; $content = substr($result, 16, strlen($result)); $len_list = unpack("N", substr($content, 0, 4)); $xml_len = $len_list[1]; $xml_content = substr($content, 4, $xml_len); $from_appid = substr($content, $xml_len + 4); } catch (Exception $e) { //print $e; return array(ErrorCode::$IllegalBuffer, null); } if ($from_appid != $appid) return array(ErrorCode::$ValidateAppidError, null); return array(0, $xml_content); } php7.2版本 - 预期表现 - 复现路径 - 提供一个最简复现 Demo
2018-11-07