正在显示
1 个修改的文件
包含
120 行增加
和
17 行删除
| 1 | <?php | 1 | <?php |
| 2 | + | ||
| 2 | namespace lib; | 3 | namespace lib; |
| 3 | 4 | ||
| 4 | class WXBizDataCrypt | 5 | class WXBizDataCrypt |
| 5 | { | 6 | { |
| 7 | + | ||
| 6 | private $appid; | 8 | private $appid; |
| 7 | private $sessionKey; | 9 | private $sessionKey; |
| 8 | 10 | ||
| @@ -11,8 +13,9 @@ class WXBizDataCrypt | @@ -11,8 +13,9 @@ class WXBizDataCrypt | ||
| 11 | * @param $sessionKey string 用户在小程序登录后获取的会话密钥 | 13 | * @param $sessionKey string 用户在小程序登录后获取的会话密钥 |
| 12 | * @param $appid string 小程序的appid | 14 | * @param $appid string 小程序的appid |
| 13 | */ | 15 | */ |
| 14 | - public function __construct( $appid, $sessionKey) | 16 | + public function __construct($appid, $sessionKey) |
| 15 | { | 17 | { |
| 18 | + | ||
| 16 | $this->sessionKey = $sessionKey; | 19 | $this->sessionKey = $sessionKey; |
| 17 | $this->appid = $appid; | 20 | $this->appid = $appid; |
| 18 | } | 21 | } |
| @@ -26,42 +29,142 @@ class WXBizDataCrypt | @@ -26,42 +29,142 @@ class WXBizDataCrypt | ||
| 26 | * | 29 | * |
| 27 | * @return int 成功0,失败返回对应的错误码 | 30 | * @return int 成功0,失败返回对应的错误码 |
| 28 | */ | 31 | */ |
| 29 | - public function decryptData( $encryptedData, $iv, &$data ) | 32 | + public function decryptData($encryptedData, $iv, &$data) |
| 30 | { | 33 | { |
| 31 | if (strlen($this->sessionKey) != 24) { | 34 | if (strlen($this->sessionKey) != 24) { |
| 32 | return ErrorCode::$IllegalAesKey; | 35 | return ErrorCode::$IllegalAesKey; |
| 33 | } | 36 | } |
| 34 | - $aesKey=base64_decode($this->sessionKey); | 37 | + $aesKey = base64_decode($this->sessionKey); |
| 35 | 38 | ||
| 36 | 39 | ||
| 37 | if (strlen($iv) != 24) { | 40 | if (strlen($iv) != 24) { |
| 38 | return ErrorCode::$IllegalIv; | 41 | return ErrorCode::$IllegalIv; |
| 39 | } | 42 | } |
| 40 | - $aesIV=base64_decode($iv); | 43 | + $aesIV = base64_decode($iv); |
| 41 | 44 | ||
| 42 | - $aesCipher=base64_decode($encryptedData); | 45 | + $aesCipher = base64_decode($encryptedData); |
| 43 | 46 | ||
| 44 | - $result=openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); | 47 | + $pc = new Prpcrypt($aesKey); |
| 48 | + $result = $pc->decrypt($aesCipher, $aesIV); | ||
| 45 | 49 | ||
| 46 | - $dataObj=json_decode($result); | ||
| 47 | - print_r($aesCipher); | ||
| 48 | - print_r("======="); | ||
| 49 | - print_r($this->appid); | ||
| 50 | - return; | ||
| 51 | - if( $dataObj == NULL ) | ||
| 52 | - { | ||
| 53 | - return ErrorCode::$IllegalBuffer; | 50 | + if ($result[0] != 0) { |
| 51 | + return $result[0]; | ||
| 54 | } | 52 | } |
| 55 | - if( $dataObj->watermark->appid != $this->appid ) | ||
| 56 | - { | ||
| 57 | 53 | ||
| 54 | + $dataObj = json_decode($result[1]); | ||
| 55 | + if ($dataObj == NULL) { | ||
| 58 | return ErrorCode::$IllegalBuffer; | 56 | return ErrorCode::$IllegalBuffer; |
| 59 | } | 57 | } |
| 60 | - $data = $result; | 58 | + if ($dataObj->watermark->appid != $this->appid) { |
| 59 | + return ErrorCode::$IllegalBuffer; | ||
| 60 | + } | ||
| 61 | + $data = $result[1]; | ||
| 61 | return ErrorCode::$OK; | 62 | return ErrorCode::$OK; |
| 62 | } | 63 | } |
| 63 | 64 | ||
| 64 | } | 65 | } |
| 66 | + | ||
| 67 | +/** | ||
| 68 | + * PKCS7Encoder class | ||
| 69 | + * | ||
| 70 | + * 提供基于PKCS7算法的加解密接口. | ||
| 71 | + */ | ||
| 72 | +class PKCS7Encoder | ||
| 73 | +{ | ||
| 74 | + public static $block_size = 16; | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * 对需要加密的明文进行填充补位 | ||
| 78 | + * @param $text 需要进行填充补位操作的明文 | ||
| 79 | + * @return 补齐明文字符串 | ||
| 80 | + */ | ||
| 81 | + function encode($text) | ||
| 82 | + { | ||
| 83 | + $block_size = PKCS7Encoder::$block_size; | ||
| 84 | + $text_length = strlen($text); | ||
| 85 | + //计算需要填充的位数 | ||
| 86 | + $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size); | ||
| 87 | + if ($amount_to_pad == 0) { | ||
| 88 | + $amount_to_pad = PKCS7Encoder::block_size; | ||
| 89 | + } | ||
| 90 | + //获得补位所用的字符 | ||
| 91 | + $pad_chr = chr($amount_to_pad); | ||
| 92 | + $tmp = ""; | ||
| 93 | + for ($index = 0; $index < $amount_to_pad; $index++) { | ||
| 94 | + $tmp .= $pad_chr; | ||
| 95 | + } | ||
| 96 | + return $text . $tmp; | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + /** | ||
| 100 | + * 对解密后的明文进行补位删除 | ||
| 101 | + * @param decrypted 解密后的明文 | ||
| 102 | + * @return 删除填充补位后的明文 | ||
| 103 | + */ | ||
| 104 | + function decode($text) | ||
| 105 | + { | ||
| 106 | + | ||
| 107 | + $pad = ord(substr($text, -1)); | ||
| 108 | + if ($pad < 1 || $pad > 32) { | ||
| 109 | + $pad = 0; | ||
| 110 | + } | ||
| 111 | + return substr($text, 0, (strlen($text) - $pad)); | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | +} | ||
| 115 | + | ||
| 116 | +/** | ||
| 117 | + * Prpcrypt class | ||
| 118 | + * | ||
| 119 | + * | ||
| 120 | + */ | ||
| 121 | +class Prpcrypt | ||
| 122 | +{ | ||
| 123 | + public $key; | ||
| 124 | + | ||
| 125 | + function __construct($k) | ||
| 126 | + { | ||
| 127 | + $this->key = $k; | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + /** | ||
| 131 | + * 对密文进行解密 | ||
| 132 | + * @param string $aesCipher 需要解密的密文 | ||
| 133 | + * @param string $aesIV 解密的初始向量 | ||
| 134 | + * @return string 解密得到的明文 | ||
| 135 | + */ | ||
| 136 | + public function decrypt($aesCipher, $aesIV) | ||
| 137 | + { | ||
| 138 | + | ||
| 139 | + | ||
| 140 | + try { | ||
| 141 | + | ||
| 142 | + $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); | ||
| 143 | +print_r($module);return; | ||
| 144 | + mcrypt_generic_init($module, $this->key, $aesIV); | ||
| 145 | + | ||
| 146 | + //解密 | ||
| 147 | + $decrypted = mdecrypt_generic($module, $aesCipher); | ||
| 148 | + mcrypt_generic_deinit($module); | ||
| 149 | + mcrypt_module_close($module); | ||
| 150 | + } catch (Exception $e) { | ||
| 151 | + return array(ErrorCode::$IllegalBuffer, null); | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + | ||
| 155 | + try { | ||
| 156 | + //去除补位字符 | ||
| 157 | + $pkc_encoder = new PKCS7Encoder; | ||
| 158 | + $result = $pkc_encoder->decode($decrypted); | ||
| 159 | + | ||
| 160 | + } catch (Exception $e) { | ||
| 161 | + //print $e; | ||
| 162 | + return array(ErrorCode::$IllegalBuffer, null); | ||
| 163 | + } | ||
| 164 | + return array(0, $result); | ||
| 165 | + } | ||
| 166 | +} | ||
| 167 | + | ||
| 65 | class ErrorCode | 168 | class ErrorCode |
| 66 | { | 169 | { |
| 67 | public static $OK = 0; | 170 | public static $OK = 0; |
-
请 注册 或 登录 后发表评论