123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- /**
- * Created by PhpStorm.
- * User: jianjun
- * Date: 2016/11/17
- * Time: 17:12
- */
- namespace api\libs;
- use yii\base\Exception;
- /**
- * 微信小程序加密解密包
- * Class WxBizDecrypt
- * @package api\lib
- */
- class WxBizDecrypt
- {
- public static $OK = 0;
- public static $IllegalAesKey = -41001;
- public static $IllegalIv = -41002;
- public static $IllegalBuffer = -41003;
- public static $DecodeBase64Error = -41004;
- public static $IllegalSessionKey = -41005;
- public static $block_size = 16;
- public static $appid = '';
- public static $appSecret = '';
- /**
- * 获取SessionKey 以及Openid
- * SessionKey 获取更多加密信息,如unionId
- * @param $code
- * @return Int
- */
- static function getSessionkey($code,&$data){
- $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".self::$appid."&secret=".self::$appSecret."&js_code=$code&grant_type=authorization_code";
- $result = self::getHtml($url);
- $resultObj = json_decode($result);
- if($resultObj== null){
- return self::$IllegalSessionKey;
- }
- $data = $result;
- return self::$OK;
- }
- protected static function getHtml($url){
- $ch = curl_init();
- $timeout = 5;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- /**
- * 检验数据的真实性,并且获取解密后的明文.
- * @param $encryptedData string 加密的用户数据
- * @param $iv string 与用户数据一同返回的初始向量
- * @param $data string 解密后的原文
- *
- * @return int 成功0,失败返回对应的错误码
- */
- static function decryptData( $encryptedData, $iv,$sessionKey, &$data )
- {
- if (strlen($sessionKey) != 24) {
- return self::$IllegalAesKey;
- }
- $aesKey=base64_decode($sessionKey);
- if (strlen($iv) != 24) {
- return self::$IllegalIv;
- }
- $aesIV=base64_decode($iv);
- $aesCipher=base64_decode($encryptedData);
- $result = self::decrypt($aesCipher,$aesIV,$aesKey);
- if ($result[0] != 0) {
- return $result[0];
- }
- $dataObj=json_decode( $result[1] );
- if( $dataObj == NULL )
- {
- return self::$IllegalBuffer;
- }
- if( $dataObj->watermark->appid != self::$appid )
- {
- return self::$IllegalBuffer;
- }
- $data = $result[1];
- return self::$OK;
- }
- /**
- * 对需要加密的明文进行填充补位
- * @param $text 需要进行填充补位操作的明文
- * @return 补齐明文字符串
- */
- static function encode( $text )
- {
- $text_length = strlen( $text );
- //计算需要填充的位数
- $amount_to_pad = self::$block_size - ( $text_length % self::$block_size );
- if ( $amount_to_pad == 0 ) {
- $amount_to_pad = self::$block_size;
- }
- //获得补位所用的字符
- $pad_chr = chr( $amount_to_pad );
- $tmp = "";
- for ( $index = 0; $index < $amount_to_pad; $index++ ) {
- $tmp .= $pad_chr;
- }
- return $text . $tmp;
- }
- /**
- * 对解密后的明文进行补位删除
- * @param decrypted 解密后的明文
- * @return 删除填充补位后的明文
- */
- static function decode($text)
- {
- $pad = ord(substr($text, -1));
- if ($pad < 1 || $pad > 32) {
- $pad = 0;
- }
- return substr($text, 0, (strlen($text) - $pad));
- }
- /**
- * 对密文进行解密
- * @param string $aesCipher 需要解密的密文
- * @param string $aesIV 解密的初始向量
- * @return string 解密得到的明文
- */
- static function decrypt( $aesCipher, $aesIV ,$key)
- {
- try {
- $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
- mcrypt_generic_init($module, $key, $aesIV);
- //解密
- $decrypted = mdecrypt_generic($module, $aesCipher);
- mcrypt_generic_deinit($module);
- mcrypt_module_close($module);
- } catch (Exception $e) {
- return array(self::$IllegalBuffer, null);
- }
- try {
- //去除补位字符
- $result = self::decode($decrypted);
- } catch (Exception $e) {
- //print $e;
- return array(self::$IllegalBuffer, null);
- }
- return array(0, $result);
- }
- }
|