Overview

Packages

  • None
  • WxRobot
    • Admin
    • Cmd
    • CoreFunctions
    • Exends
    • Install
    • Table
    • Uninstall
    • WxSDK

Classes

  • ErrorCode
  • PKCS7Encoder
  • Prpcrypt
  • SHA1
  • Weixin_BaseCore
  • WeiXin_SDK
  • Weixin_Template
  • WXBizMsgCrypt
  • WxRobot_Admin
  • WxRobot_Admin_Menu_Extends
  • WxRobot_Admin_Menu_Instro
  • WxRobot_Admin_Menu_Menu
  • WxRobot_Admin_Menu_Records
  • WxRobot_Admin_Menu_Reply
  • WxRobot_Admin_Menu_Setting
  • WxRobot_Admin_Menu_Statistics
  • WxRobot_Cmd
  • WxRobot_Cmd_Event
  • WxRobot_Cmd_Event_User
  • WxRobot_Cmd_Text
  • WxRobot_Extends
  • WxRobot_Install
  • WxRobot_Robot
  • WxRobot_SDK
  • WxRobot_Table_Extends
  • WxRobot_Table_Menu
  • WxRobot_Table_Records
  • WxRobot_Table_Reply
  • WxRobot_Uninstall
  • WxRobot_Wp
  • XMLParse

Functions

  • wx_admin_log
  • wx_is_xml
  • wx_notice_msg
  • wx_parse_xml
  • wx_random_big_pic
  • wx_random_small_pic
  • wx_request_array
  • wx_request_decode
  • wx_request_is_encode
  • wx_request_xml
  • wx_send_encode
  • Overview
  • Package
  • Class
  1: <?php
  2: 
  3: include_once "errorCode.php";
  4: 
  5: /**
  6:  * PKCS7Encoder class
  7:  *
  8:  * 提供基于PKCS7算法的加解密接口.
  9:  */
 10: class PKCS7Encoder
 11: {
 12:     public static $block_size = 32;
 13: 
 14:     /**
 15:      * 对需要加密的明文进行填充补位
 16:      * @param $text 需要进行填充补位操作的明文
 17:      * @return 补齐明文字符串
 18:      */
 19:     function encode($text)
 20:     {
 21:         $block_size = PKCS7Encoder::$block_size;
 22:         $text_length = strlen($text);
 23:         //计算需要填充的位数
 24:         $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
 25:         if ($amount_to_pad == 0) {
 26:             $amount_to_pad = PKCS7Encoder::block_size;
 27:         }
 28:         //获得补位所用的字符
 29:         $pad_chr = chr($amount_to_pad);
 30:         $tmp = "";
 31:         for ($index = 0; $index < $amount_to_pad; $index++) {
 32:             $tmp .= $pad_chr;
 33:         }
 34:         return $text . $tmp;
 35:     }
 36: 
 37:     /**
 38:      * 对解密后的明文进行补位删除
 39:      * @param decrypted 解密后的明文
 40:      * @return 删除填充补位后的明文
 41:      */
 42:     function decode($text)
 43:     {
 44: 
 45:         $pad = ord(substr($text, -1));
 46:         if ($pad < 1 || $pad > 32) {
 47:             $pad = 0;
 48:         }
 49:         return substr($text, 0, (strlen($text) - $pad));
 50:     }
 51: 
 52: }
 53: 
 54: /**
 55:  * Prpcrypt class
 56:  *
 57:  * 提供接收和推送给公众平台消息的加解密接口.
 58:  */
 59: class Prpcrypt
 60: {
 61:     public $key;
 62: 
 63:     function Prpcrypt($k)
 64:     {
 65:         $this->key = base64_decode($k . "=");
 66:     }
 67: 
 68:     /**
 69:      * 对明文进行加密
 70:      * @param string $text 需要加密的明文
 71:      * @return string 加密后的密文
 72:      */
 73:     public function encrypt($text, $appid)
 74:     {
 75: 
 76:         try {
 77:             //获得16位随机字符串,填充到明文之前
 78:             $random = $this->getRandomStr();
 79:             $text = $random . pack("N", strlen($text)) . $text . $appid;
 80:             // 网络字节序
 81:             $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
 82:             $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
 83:             $iv = substr($this->key, 0, 16);
 84:             //使用自定义的填充方式对明文进行补位填充
 85:             $pkc_encoder = new PKCS7Encoder;
 86:             $text = $pkc_encoder->encode($text);
 87:             mcrypt_generic_init($module, $this->key, $iv);
 88:             //加密
 89:             $encrypted = mcrypt_generic($module, $text);
 90:             mcrypt_generic_deinit($module);
 91:             mcrypt_module_close($module);
 92: 
 93:             //print(base64_encode($encrypted));
 94:             //使用BASE64对加密后的字符串进行编码
 95:             return array(ErrorCode::$OK, base64_encode($encrypted));
 96:         } catch (Exception $e) {
 97:             //print $e;
 98:             return array(ErrorCode::$EncryptAESError, null);
 99:         }
100:     }
101: 
102:     /**
103:      * 对密文进行解密
104:      * @param string $encrypted 需要解密的密文
105:      * @return string 解密得到的明文
106:      */
107:     public function decrypt($encrypted, $appid)
108:     {
109: 
110:         try {
111:             //使用BASE64对需要解密的字符串进行解码
112:             $ciphertext_dec = base64_decode($encrypted);
113:             $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
114:             $iv = substr($this->key, 0, 16);
115:             mcrypt_generic_init($module, $this->key, $iv);
116: 
117:             //解密
118:             $decrypted = mdecrypt_generic($module, $ciphertext_dec);
119:             mcrypt_generic_deinit($module);
120:             mcrypt_module_close($module);
121:         } catch (Exception $e) {
122:             return array(ErrorCode::$DecryptAESError, null);
123:         }
124: 
125: 
126:         try {
127:             //去除补位字符
128:             $pkc_encoder = new PKCS7Encoder;
129:             $result = $pkc_encoder->decode($decrypted);
130:             //去除16位随机字符串,网络字节序和AppId
131:             if (strlen($result) < 16)
132:                 return "";
133:             $content = substr($result, 16, strlen($result));
134:             $len_list = unpack("N", substr($content, 0, 4));
135:             $xml_len = $len_list[1];
136:             $xml_content = substr($content, 4, $xml_len);
137:             $from_appid = substr($content, $xml_len + 4);
138:         } catch (Exception $e) {
139:             //print $e;
140:             return array(ErrorCode::$IllegalBuffer, null);
141:         }
142:         if ($from_appid != $appid)
143:             return array(ErrorCode::$ValidateAppidError, null);
144:         return array(0, $xml_content);
145: 
146:     }
147: 
148: 
149:     /**
150:      * 随机生成16位字符串
151:      * @return string 生成的字符串
152:      */
153:     function getRandomStr()
154:     {
155: 
156:         $str = "";
157:         $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
158:         $max = strlen($str_pol) - 1;
159:         for ($i = 0; $i < 16; $i++) {
160:             $str .= $str_pol[mt_rand(0, $max)];
161:         }
162:         return $str;
163:     }
164: 
165: }
166: 
167: ?>
API documentation generated by ApiGen