|
@@ -14,21 +14,23 @@
|
|
|
* 9、encrypt aes加密方法
|
|
* 9、encrypt aes加密方法
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
-namespace app\api\controller;
|
|
|
|
|
|
|
+namespace crmeb\services;
|
|
|
|
|
|
|
|
|
|
|
|
|
-use app\api\controller\AuthController as Controller;
|
|
|
|
|
|
|
+use function halt;
|
|
|
|
|
+use function input;
|
|
|
|
|
+use function json;
|
|
|
|
|
|
|
|
-class GmController extends Controller
|
|
|
|
|
|
|
+class GmService
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
//以下参数都需要金融云提供
|
|
//以下参数都需要金融云提供
|
|
|
//AES偏移量
|
|
//AES偏移量
|
|
|
- protected $iv = 'abcdefghABCDEFGH';
|
|
|
|
|
- protected $appID = "96c8268e-cfa4-497f-9329-a5e3417f0825";
|
|
|
|
|
- protected $appSecretKey = "921b3c06-eaed-7b1c-e053-010000717745";
|
|
|
|
|
- protected $tokenUrl = "https://mouldai.com/apiSIT/tzyj/approveDev";//回归环境
|
|
|
|
|
- protected $token;
|
|
|
|
|
|
|
+ protected static $iv = 'abcdefghABCDEFGH';
|
|
|
|
|
+ protected static $appID = "96c8268e-cfa4-497f-9329-a5e3417f0825";
|
|
|
|
|
+ protected static $appSecretKey = "921b3c06-eaed-7b1c-e053-010000717745";
|
|
|
|
|
+ protected static $tokenUrl = "https://mouldai.com/apiSIT/tzyj/approveDev";//回归环境
|
|
|
|
|
+ protected static $token;
|
|
|
//合作机构自有公钥
|
|
//合作机构自有公钥
|
|
|
const PUBLIC_KEY = "DE3w4DgjV+np0oHQzSDUgvlxGKPhi/gHBRp8dtjun+z6uqrRJE6B1qswZpaSCs3tp0tm98ZjjL9RTuNh4dyUuA==";
|
|
const PUBLIC_KEY = "DE3w4DgjV+np0oHQzSDUgvlxGKPhi/gHBRp8dtjun+z6uqrRJE6B1qswZpaSCs3tp0tm98ZjjL9RTuNh4dyUuA==";
|
|
|
//合作机构自有私钥
|
|
//合作机构自有私钥
|
|
@@ -36,16 +38,16 @@ class GmController extends Controller
|
|
|
//泰隆银行公钥
|
|
//泰隆银行公钥
|
|
|
const CGB_PUBLIC_KEY = "kZYtvaa+HIImu0xhONxte8wyXmMBe7HKKf1i1fqU7Wf8PrsmvAguPNClPXfOoxqW8gmy5qrFK9oX3tvmrq4r0A==";
|
|
const CGB_PUBLIC_KEY = "kZYtvaa+HIImu0xhONxte8wyXmMBe7HKKf1i1fqU7Wf8PrsmvAguPNClPXfOoxqW8gmy5qrFK9oX3tvmrq4r0A==";
|
|
|
|
|
|
|
|
- public function _initialize()
|
|
|
|
|
|
|
+ public static function _initialize()
|
|
|
{
|
|
{
|
|
|
// 初始化加密扩展
|
|
// 初始化加密扩展
|
|
|
- echo "--------php_crypto_init 初始化开始--------\n";
|
|
|
|
|
|
|
+// echo "--------php_crypto_init 初始化开始--------\n";
|
|
|
$path = "/www/server/php/73/libcryptAPIsm_lnx64.so";
|
|
$path = "/www/server/php/73/libcryptAPIsm_lnx64.so";
|
|
|
php_crypto_init($path);
|
|
php_crypto_init($path);
|
|
|
- echo "--------php_crypto_init 初始化结束-------\n";
|
|
|
|
|
|
|
+// echo "--------php_crypto_init 初始化结束-------\n";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function string2hex($string)
|
|
|
|
|
|
|
+ public static function string2hex($string)
|
|
|
{
|
|
{
|
|
|
$hex = '';
|
|
$hex = '';
|
|
|
for ($i = 0; $i < strlen($string); $i++) {
|
|
for ($i = 0; $i < strlen($string); $i++) {
|
|
@@ -59,49 +61,49 @@ class GmController extends Controller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//获取token
|
|
//获取token
|
|
|
- public function getToken()
|
|
|
|
|
|
|
+ public static function getToken()
|
|
|
{
|
|
{
|
|
|
//获取6位数字符串
|
|
//获取6位数字符串
|
|
|
$seqNO = (string)rand(100000, 999999);
|
|
$seqNO = (string)rand(100000, 999999);
|
|
|
//获取16位随机字符串并md5 转大写 得到
|
|
//获取16位随机字符串并md5 转大写 得到
|
|
|
- $key = strtoupper(md5($this->getKey()));
|
|
|
|
|
|
|
+ $key = strtoupper(md5(self::getKey()));
|
|
|
|
|
|
|
|
|
|
|
|
|
//echo $this->string2hex(base64_decode(self::PRIVATE_KEY));
|
|
//echo $this->string2hex(base64_decode(self::PRIVATE_KEY));
|
|
|
|
|
|
|
|
- echo $this->string2hex(base64_decode(self::PUBLIC_KEY));
|
|
|
|
|
|
|
+ echo self::string2hex(base64_decode(self::PUBLIC_KEY));
|
|
|
|
|
|
|
|
|
|
|
|
|
//初始化转换密钥信息
|
|
//初始化转换密钥信息
|
|
|
- php_HextoAsc($this->string2hex(base64_decode(self::PRIVATE_KEY)), $k1);
|
|
|
|
|
- php_HextoAsc($this->string2hex(base64_decode(self::PUBLIC_KEY)), $k2);
|
|
|
|
|
- php_HextoAsc($this->string2hex(base64_decode(self::CGB_PUBLIC_KEY)), $gf_k);
|
|
|
|
|
|
|
+ php_HextoAsc(self::string2hex(base64_decode(self::PRIVATE_KEY)), $k1);
|
|
|
|
|
+ php_HextoAsc(self::string2hex(base64_decode(self::PUBLIC_KEY)), $k2);
|
|
|
|
|
+ php_HextoAsc(self::string2hex(base64_decode(self::CGB_PUBLIC_KEY)), $gf_k);
|
|
|
|
|
|
|
|
//拼接参数数组
|
|
//拼接参数数组
|
|
|
$data = [
|
|
$data = [
|
|
|
- 'appID' => $this->appID,
|
|
|
|
|
|
|
+ 'appID' => self::$appID,
|
|
|
'seqNO' => $seqNO,
|
|
'seqNO' => $seqNO,
|
|
|
'random' => strtoupper(md5($seqNO)),
|
|
'random' => strtoupper(md5($seqNO)),
|
|
|
- 'sm2EncryptData' => $this->SM2Encrypt($key, $gf_k), //sm2加密key
|
|
|
|
|
- 'sm2Sign' => $this->SM2Sign($key, $k2, $k1), //sm2加密key
|
|
|
|
|
|
|
+ 'sm2EncryptData' => self::SM2Encrypt($key, $gf_k), //sm2加密key
|
|
|
|
|
+ 'sm2Sign' => self::SM2Sign($key, $k2, $k1), //sm2加密key
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
//拼接签名参数,md5转大写
|
|
//拼接签名参数,md5转大写
|
|
|
- $data['sign'] = $this->SM3Crypt($data['random'] . $data['seqNO'] . $this->appSecretKey . $key);
|
|
|
|
|
|
|
+ $data['sign'] = self::SM3Crypt($data['random'] . $data['seqNO'] . self::$appSecretKey . $key);
|
|
|
|
|
|
|
|
//发送post接口请求
|
|
//发送post接口请求
|
|
|
- $res = $this->https_post($this->tokenUrl, $data);
|
|
|
|
|
|
|
+ $res = self::https_post(self::$tokenUrl, $data);
|
|
|
|
|
|
|
|
//解密返回token数据
|
|
//解密返回token数据
|
|
|
- $token = $this->SM2Decrypt(base64_decode($res['sm2EncryptData']), $k1);
|
|
|
|
|
|
|
+ $token = self::SM2Decrypt(base64_decode($res['sm2EncryptData']), $k1);
|
|
|
|
|
|
|
|
- $this->token = $token;
|
|
|
|
|
|
|
+ self::$token = $token;
|
|
|
//返回token
|
|
//返回token
|
|
|
return $token;
|
|
return $token;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//业务请求数据
|
|
//业务请求数据
|
|
|
- public function postJry()
|
|
|
|
|
|
|
+ public static function postJry()
|
|
|
{
|
|
{
|
|
|
//创建模拟请求数据,必须包含head,body
|
|
//创建模拟请求数据,必须包含head,body
|
|
|
$data = [
|
|
$data = [
|
|
@@ -113,22 +115,19 @@ class GmController extends Controller
|
|
|
'msg' => "测试请求"
|
|
'msg' => "测试请求"
|
|
|
]
|
|
]
|
|
|
];
|
|
];
|
|
|
- $res = $this->scanPaymentCode($data);
|
|
|
|
|
|
|
+ $res = self::scanPaymentCode($data);
|
|
|
dump("返回结果");
|
|
dump("返回结果");
|
|
|
halt($res);
|
|
halt($res);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//业务请求封装
|
|
//业务请求封装
|
|
|
- public function scanPaymentCode($array = [])
|
|
|
|
|
|
|
+ public static function scanPaymentCode($array = [])
|
|
|
{
|
|
{
|
|
|
//初始化转换密钥信息
|
|
//初始化转换密钥信息
|
|
|
- php_HextoAsc($this->string2hex(base64_decode(self::PRIVATE_KEY)), $k1);
|
|
|
|
|
- php_HextoAsc($this->string2hex(base64_decode(self::PUBLIC_KEY)), $k2);
|
|
|
|
|
- php_HextoAsc($this->string2hex(base64_decode(self::CGB_PUBLIC_KEY)), $gf_k);
|
|
|
|
|
-// $this->string2hex(base64_decode(self::PRIVATE_KEY));
|
|
|
|
|
-// $this->string2hex(base64_decode(self::PUBLIC_KEY));
|
|
|
|
|
-// $this->string2hex(base64_decode(self::CGB_PUBLIC_KEY));
|
|
|
|
|
|
|
+ php_HextoAsc(self::string2hex(base64_decode(self::PRIVATE_KEY)), $k1);
|
|
|
|
|
+ php_HextoAsc(self::string2hex(base64_decode(self::PUBLIC_KEY)), $k2);
|
|
|
|
|
+ php_HextoAsc(self::string2hex(base64_decode(self::CGB_PUBLIC_KEY)), $gf_k);
|
|
|
|
|
|
|
|
|
|
|
|
|
//数据转json
|
|
//数据转json
|
|
@@ -136,41 +135,41 @@ class GmController extends Controller
|
|
|
//获取随机6位字符串数字
|
|
//获取随机6位字符串数字
|
|
|
$seqNO = (string)rand(100000, 999999);
|
|
$seqNO = (string)rand(100000, 999999);
|
|
|
//获取随机字符串秘钥,md5 并转大写
|
|
//获取随机字符串秘钥,md5 并转大写
|
|
|
- $key = strtoupper(md5($this->getKey()));
|
|
|
|
|
|
|
+ $key = strtoupper(md5(self::getKey()));
|
|
|
//拼接请求数据
|
|
//拼接请求数据
|
|
|
$data = [
|
|
$data = [
|
|
|
- 'appID' => $this->appID,
|
|
|
|
|
|
|
+ 'appID' => self::$appID,
|
|
|
'seqNO' => $seqNO,
|
|
'seqNO' => $seqNO,
|
|
|
'signMethod' => "SM3",
|
|
'signMethod' => "SM3",
|
|
|
'encryptMethod' => "SM4",
|
|
'encryptMethod' => "SM4",
|
|
|
- 'appAccessToken' => $this->getToken(),//获取token
|
|
|
|
|
- 'sm2EncryptData' => $this->SM2Encrypt($key, $gf_k), //sm2加密key
|
|
|
|
|
- 'sm2Sign' => $this->SM2Sign($key, $k2, $k1), //sm2加密key
|
|
|
|
|
|
|
+ 'appAccessToken' => self::getToken(),//获取token
|
|
|
|
|
+ 'sm2EncryptData' => self::SM2Encrypt($key, $gf_k), //sm2加密key
|
|
|
|
|
+ 'sm2Sign' => self::SM2Sign($key, $k2, $k1), //sm2加密key
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
//拼接签名参数,md5转大写
|
|
//拼接签名参数,md5转大写
|
|
|
- $data['sign'] = $this->SM3Crypt($json . $data['seqNO'] . $this->appSecretKey . $key);
|
|
|
|
|
|
|
+ $data['sign'] = self::SM3Crypt($json . $data['seqNO'] . self::$appSecretKey . $key);
|
|
|
//AES加密业务数据
|
|
//AES加密业务数据
|
|
|
- $data['reqData'] = $this->SM4Encrypt($json, $data['seqNO'] . $data['appAccessToken'] . $this->appSecretKey . $key);
|
|
|
|
|
|
|
+ $data['reqData'] = self::SM4Encrypt($json, $data['seqNO'] . $data['appAccessToken'] . self::$appSecretKey . $key);
|
|
|
|
|
|
|
|
dump("业务数据json");
|
|
dump("业务数据json");
|
|
|
dump($json);
|
|
dump($json);
|
|
|
dump("请求数据");
|
|
dump("请求数据");
|
|
|
dump($data);
|
|
dump($data);
|
|
|
- $res = $this->https_post("https://mouldai.com/apiSIT/tzyj/scanPaymentCode", $data);
|
|
|
|
|
|
|
+ $res = self::https_post("https://mouldai.com/apiSIT/tzyj/scanPaymentCode", $data);
|
|
|
|
|
|
|
|
//解密sm2密钥
|
|
//解密sm2密钥
|
|
|
- $sm2Key = $this->SM2Decrypt(base64_decode($res['sm2EncryptData']), $k1);
|
|
|
|
|
|
|
+ $sm2Key = self::SM2Decrypt(base64_decode($res['sm2EncryptData']), $k1);
|
|
|
|
|
|
|
|
dump("sm2解密数据:" . $sm2Key);
|
|
dump("sm2解密数据:" . $sm2Key);
|
|
|
|
|
|
|
|
//SM4解密数据
|
|
//SM4解密数据
|
|
|
- $rspData = $this->SM4Decrypt($res['rspData'], $res['seqNO'] . $data['appAccessToken'] . $this->appSecretKey . $sm2Key);
|
|
|
|
|
|
|
+ $rspData = self::SM4Decrypt($res['rspData'], $res['seqNO'] . $data['appAccessToken'] . self::$appSecretKey . $sm2Key);
|
|
|
|
|
|
|
|
dump("sm4解密报文数据:" . $rspData);
|
|
dump("sm4解密报文数据:" . $rspData);
|
|
|
|
|
|
|
|
//sm3验签数据校验
|
|
//sm3验签数据校验
|
|
|
- $sm3SignData = $this->SM3Crypt($rspData . $res['seqNO'] . $this->appSecretKey . $sm2Key);
|
|
|
|
|
|
|
+ $sm3SignData = self::SM3Crypt($rspData . $res['seqNO'] . self::$appSecretKey . $sm2Key);
|
|
|
|
|
|
|
|
dump("sm3响应报文拼接加签:" . $sm3SignData);
|
|
dump("sm3响应报文拼接加签:" . $sm3SignData);
|
|
|
|
|
|
|
@@ -187,7 +186,7 @@ class GmController extends Controller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//获取post数据并解密解签获得业务数据
|
|
//获取post数据并解密解签获得业务数据
|
|
|
- public function getPostData()
|
|
|
|
|
|
|
+ public static function getPostData()
|
|
|
{
|
|
{
|
|
|
//获取post数据
|
|
//获取post数据
|
|
|
$arr = input("post.");
|
|
$arr = input("post.");
|
|
@@ -197,16 +196,16 @@ class GmController extends Controller
|
|
|
|
|
|
|
|
//检验字段是否缺失
|
|
//检验字段是否缺失
|
|
|
if (!isset($arr['rsaEncryptData']) || !isset($arr['seqNO']) || !isset($arr['signMethod']) || !isset($arr['sign']) || !isset($arr['encryptMethod']) || !isset($arr['reqData']) || !isset($arr['appID'])) {
|
|
if (!isset($arr['rsaEncryptData']) || !isset($arr['seqNO']) || !isset($arr['signMethod']) || !isset($arr['sign']) || !isset($arr['encryptMethod']) || !isset($arr['reqData']) || !isset($arr['appID'])) {
|
|
|
- return $this->runJson("1000001", "缺少参数");
|
|
|
|
|
|
|
+ return self::runJson("1000001", "缺少参数");
|
|
|
}
|
|
}
|
|
|
dump("获取到的post数据");
|
|
dump("获取到的post数据");
|
|
|
dump($arr);
|
|
dump($arr);
|
|
|
//rsa解密秘钥字段rsaEncryptData
|
|
//rsa解密秘钥字段rsaEncryptData
|
|
|
- $rasKey = $this->decryptByPublicKey($arr['rsaEncryptData']);
|
|
|
|
|
|
|
+ $rasKey = self::decryptByPublicKey($arr['rsaEncryptData']);
|
|
|
dump("解密后的秘钥");
|
|
dump("解密后的秘钥");
|
|
|
dump($rasKey);
|
|
dump($rasKey);
|
|
|
//拼接秘钥
|
|
//拼接秘钥
|
|
|
- $key = $arr['seqNO'] . $this->token . $this->appSecretKey . $rasKey;
|
|
|
|
|
|
|
+ $key = $arr['seqNO'] . self::$token . self::$appSecretKey . $rasKey;
|
|
|
|
|
|
|
|
//秘钥转md5 转大写
|
|
//秘钥转md5 转大写
|
|
|
$key = strtoupper(md5($key));
|
|
$key = strtoupper(md5($key));
|
|
@@ -220,7 +219,7 @@ class GmController extends Controller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//SM2Encrypt
|
|
//SM2Encrypt
|
|
|
- function SM2Encrypt($data, $pubkey)
|
|
|
|
|
|
|
+ public static function SM2Encrypt($data, $pubkey)
|
|
|
{
|
|
{
|
|
|
//sm2加密
|
|
//sm2加密
|
|
|
$recode = php_SM2Encrypt($data, $cipher, $pubkey);
|
|
$recode = php_SM2Encrypt($data, $cipher, $pubkey);
|
|
@@ -240,7 +239,7 @@ class GmController extends Controller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//SM2Decrypt
|
|
//SM2Decrypt
|
|
|
- function SM2Decrypt($data, $privkey)
|
|
|
|
|
|
|
+ public static function SM2Decrypt($data, $privkey)
|
|
|
{
|
|
{
|
|
|
//php_SM2Decrypt 接收密文值是c1c3c2格式,若对方给的密文值是DER格式 则通过如下方式转换,然后再传入接口解密
|
|
//php_SM2Decrypt 接收密文值是c1c3c2格式,若对方给的密文值是DER格式 则通过如下方式转换,然后再传入接口解密
|
|
|
$recode = php_SM2FormatConvert(101, $data, $c1c3c2);
|
|
$recode = php_SM2FormatConvert(101, $data, $c1c3c2);
|
|
@@ -254,7 +253,7 @@ class GmController extends Controller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//SM2Sign
|
|
//SM2Sign
|
|
|
- function SM2Sign($data, $pubkey, $privkey)
|
|
|
|
|
|
|
+ public static function SM2Sign($data, $pubkey, $privkey)
|
|
|
{
|
|
{
|
|
|
//签名
|
|
//签名
|
|
|
$recode = php_SM2Sign($data, $redata, $privkey, $pubkey);
|
|
$recode = php_SM2Sign($data, $redata, $privkey, $pubkey);
|
|
@@ -273,16 +272,16 @@ class GmController extends Controller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//SM3Crypt
|
|
//SM3Crypt
|
|
|
- function SM3Crypt($data)
|
|
|
|
|
|
|
+ public static function SM3Crypt($data)
|
|
|
{
|
|
{
|
|
|
$recode = php_SM3Crypt($data, $sm3hash);
|
|
$recode = php_SM3Crypt($data, $sm3hash);
|
|
|
echo "<br/>recode:$recode \n";
|
|
echo "<br/>recode:$recode \n";
|
|
|
//加签数据base64返回
|
|
//加签数据base64返回
|
|
|
- return strtoupper($this->string2hex($sm3hash));
|
|
|
|
|
|
|
+ return strtoupper(self::string2hex($sm3hash));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//16进制转换为二进制
|
|
//16进制转换为二进制
|
|
|
- function hex2String($hexdata)
|
|
|
|
|
|
|
+ public static function hex2String($hexdata)
|
|
|
{
|
|
{
|
|
|
$bindata = "";
|
|
$bindata = "";
|
|
|
for ($i = 0; $i < strlen($hexdata); $i += 2) {
|
|
for ($i = 0; $i < strlen($hexdata); $i += 2) {
|
|
@@ -292,7 +291,7 @@ class GmController extends Controller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//SM4Encrypt
|
|
//SM4Encrypt
|
|
|
- function SM4Encrypt($data, $password)
|
|
|
|
|
|
|
+ public static function SM4Encrypt($data, $password)
|
|
|
{
|
|
{
|
|
|
//加密密码做特殊处理 先MD5转换大写 从第8位开始截取16个字符串
|
|
//加密密码做特殊处理 先MD5转换大写 从第8位开始截取16个字符串
|
|
|
$password = substr(strtoupper(md5($password)), 8, 16);
|
|
$password = substr(strtoupper(md5($password)), 8, 16);
|
|
@@ -305,7 +304,7 @@ class GmController extends Controller
|
|
|
echo "<br/>recode:$recode \n";
|
|
echo "<br/>recode:$recode \n";
|
|
|
|
|
|
|
|
//加密
|
|
//加密
|
|
|
- $recode = php_SM4CBCCrypt(0, $data, $redata, $key, $this->iv);
|
|
|
|
|
|
|
+ $recode = php_SM4CBCCrypt(0, $data, $redata, $key, self::$iv);
|
|
|
echo "<br/>recode:$recode \n";
|
|
echo "<br/>recode:$recode \n";
|
|
|
echo "<br/>redata: " . base64_encode($redata) . " \n";
|
|
echo "<br/>redata: " . base64_encode($redata) . " \n";
|
|
|
|
|
|
|
@@ -314,7 +313,7 @@ class GmController extends Controller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//SM4Encrypt
|
|
//SM4Encrypt
|
|
|
- function SM4Decrypt($data, $password)
|
|
|
|
|
|
|
+ public static function SM4Decrypt($data, $password)
|
|
|
{
|
|
{
|
|
|
echo "<br/>SM4解密数据=======" . $data;
|
|
echo "<br/>SM4解密数据=======" . $data;
|
|
|
|
|
|
|
@@ -329,37 +328,37 @@ class GmController extends Controller
|
|
|
echo "<br/>recode:$recode \n";
|
|
echo "<br/>recode:$recode \n";
|
|
|
|
|
|
|
|
//解密
|
|
//解密
|
|
|
- $recode = php_SM4CBCCrypt(1, base64_decode($data), $dedata, $key, $this->iv);
|
|
|
|
|
|
|
+ $recode = php_SM4CBCCrypt(1, base64_decode($data), $dedata, $key, self::$iv);
|
|
|
echo "<br/>sm4[解密]recode:$recode \n";
|
|
echo "<br/>sm4[解密]recode:$recode \n";
|
|
|
return $dedata;
|
|
return $dedata;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 公钥解密
|
|
* 公钥解密
|
|
|
- * @param 密文数据 $data
|
|
|
|
|
- * @return 原文结果 string
|
|
|
|
|
|
|
+ * @param mixed $data 密文数据
|
|
|
|
|
+ * @return string 原文结果
|
|
|
*/
|
|
*/
|
|
|
- private function decryptByPublicKey($data)
|
|
|
|
|
|
|
+ private static function decryptByPublicKey($data)
|
|
|
{
|
|
{
|
|
|
$data = base64_decode($data);
|
|
$data = base64_decode($data);
|
|
|
- openssl_public_decrypt($data, $decrypted, $this->publicKey, OPENSSL_PKCS1_PADDING);//公钥解密
|
|
|
|
|
|
|
+ openssl_public_decrypt($data, $decrypted, self::PUBLIC_KEY, OPENSSL_PKCS1_PADDING);//公钥解密
|
|
|
return $decrypted;
|
|
return $decrypted;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 私钥加密
|
|
* 私钥加密
|
|
|
- * @param 原始数据 $data
|
|
|
|
|
- * @return 密文结果 string
|
|
|
|
|
|
|
+ * @param mixed $data 原始数据
|
|
|
|
|
+ * @return string 密文结果
|
|
|
*/
|
|
*/
|
|
|
- private function encryptByPrivateKey($data)
|
|
|
|
|
|
|
+ private static function encryptByPrivateKey($data)
|
|
|
{
|
|
{
|
|
|
- openssl_private_encrypt($data, $encrypted, $this->privateKey, OPENSSL_PKCS1_PADDING);//私钥加密
|
|
|
|
|
- $encrypted = base64_encode($encrypted);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
|
|
|
|
|
- return $encrypted;
|
|
|
|
|
|
|
+ openssl_private_encrypt($data, $encrypted, self::PRIVATE_KEY, OPENSSL_PKCS1_PADDING);//私钥加密
|
|
|
|
|
+ //加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
|
|
|
|
|
+ return base64_encode($encrypted);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//封装返回数据
|
|
//封装返回数据
|
|
|
- private function runJson($code = "000000", $msg = "完成")
|
|
|
|
|
|
|
+ private static function runJson($code = "000000", $msg = "完成")
|
|
|
{
|
|
{
|
|
|
return json(['code' => $code, 'message' => $msg]);
|
|
return json(['code' => $code, 'message' => $msg]);
|
|
|
|
|
|
|
@@ -380,7 +379,7 @@ class GmController extends Controller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// curl post请求
|
|
// curl post请求
|
|
|
- private function https_post($url, $data = null)
|
|
|
|
|
|
|
+ private static function https_post($url, $data = null)
|
|
|
{
|
|
{
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
$header [] = 'Content-Type:application/x-www-form-urlencoded';
|
|
$header [] = 'Content-Type:application/x-www-form-urlencoded';
|
|
@@ -397,23 +396,18 @@ class GmController extends Controller
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
$tmpInfo = curl_exec($ch);
|
|
$tmpInfo = curl_exec($ch);
|
|
|
curl_close($ch);
|
|
curl_close($ch);
|
|
|
- $tmpInfo1 = json_decode($tmpInfo, true);
|
|
|
|
|
- return $tmpInfo1;
|
|
|
|
|
|
|
+ return json_decode($tmpInfo, true);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获得随机字符串
|
|
* 获得随机字符串
|
|
|
**/
|
|
**/
|
|
|
- private function getKey($length = 16)
|
|
|
|
|
|
|
+ private static function getKey($length = 16)
|
|
|
{
|
|
{
|
|
|
// 密码字符集,可任意添加你需要的字符
|
|
// 密码字符集,可任意添加你需要的字符
|
|
|
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ2345678';
|
|
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ2345678';
|
|
|
$key = '';
|
|
$key = '';
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
- // 这里提供两种字符获取方式
|
|
|
|
|
- // 第一种是使用 substr 截取$chars中的任意一位字符;
|
|
|
|
|
- // 第二种是取字符数组 $chars 的任意元素
|
|
|
|
|
- // $key .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
|
|
|
|
|
$key .= $chars[mt_rand(0, strlen($chars) - 1)];
|
|
$key .= $chars[mt_rand(0, strlen($chars) - 1)];
|
|
|
}
|
|
}
|
|
|
return $key;
|
|
return $key;
|