|
@@ -19,7 +19,8 @@ namespace app\api\controller;
|
|
|
|
|
|
use app\api\controller\AuthController as Controller;
|
|
|
|
|
|
-class Gmdemo extends Controller{
|
|
|
+class GmController extends Controller
|
|
|
+{
|
|
|
|
|
|
//以下参数都需要金融云提供
|
|
|
//AES偏移量
|
|
@@ -39,57 +40,60 @@ class Gmdemo extends Controller{
|
|
|
{
|
|
|
// 初始化加密扩展
|
|
|
echo "--------php_crypto_init 初始化开始--------\n";
|
|
|
- $path = "D:/phpstudy_pro/Extensions/php/php7.3.4nts/libcryptAPIsm_lnx64.so";
|
|
|
+ $path = "/www/server/php/73/libcryptAPIsm_lnx64.so";
|
|
|
php_crypto_init($path);
|
|
|
echo "--------php_crypto_init 初始化结束-------\n";
|
|
|
}
|
|
|
|
|
|
- function string2hex($string){
|
|
|
+ public function string2hex($string)
|
|
|
+ {
|
|
|
$hex = '';
|
|
|
- for($i = 0;$i < strlen($string); $i++){
|
|
|
+ for ($i = 0; $i < strlen($string); $i++) {
|
|
|
$ch = dechex(ord($string[$i]));
|
|
|
- if(strlen($ch)==1){
|
|
|
- $ch = "0".$ch;
|
|
|
+ if (strlen($ch) == 1) {
|
|
|
+ $ch = "0" . $ch;
|
|
|
}
|
|
|
- $hex .=$ch;
|
|
|
+ $hex .= $ch;
|
|
|
}
|
|
|
return $hex;
|
|
|
}
|
|
|
+
|
|
|
//获取token
|
|
|
- public function getToken(){
|
|
|
+ public function getToken()
|
|
|
+ {
|
|
|
//获取6位数字符串
|
|
|
- $seqNO = (string)rand(100000,999999);
|
|
|
+ $seqNO = (string)rand(100000, 999999);
|
|
|
//获取16位随机字符串并md5 转大写 得到
|
|
|
$key = strtoupper(md5($this->getKey()));
|
|
|
-
|
|
|
-
|
|
|
- //echo $this->string2hex(base64_decode(self::PRIVATE_KEY));
|
|
|
-
|
|
|
- echo $this->string2hex(base64_decode(self::PUBLIC_KEY));
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+ //echo $this->string2hex(base64_decode(self::PRIVATE_KEY));
|
|
|
+
|
|
|
+ echo $this->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($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);
|
|
|
|
|
|
//拼接参数数组
|
|
|
$data = [
|
|
|
'appID' => $this->appID,
|
|
|
'seqNO' => $seqNO,
|
|
|
- 'random'=> strtoupper(md5($seqNO)),
|
|
|
- 'sm2EncryptData' => $this->SM2Encrypt($key,$gf_k), //sm2加密key
|
|
|
- 'sm2Sign' => $this->SM2Sign($key,$k2,$k1), //sm2加密key
|
|
|
+ 'random' => strtoupper(md5($seqNO)),
|
|
|
+ 'sm2EncryptData' => $this->SM2Encrypt($key, $gf_k), //sm2加密key
|
|
|
+ 'sm2Sign' => $this->SM2Sign($key, $k2, $k1), //sm2加密key
|
|
|
];
|
|
|
|
|
|
//拼接签名参数,md5转大写
|
|
|
$data['sign'] = $this->SM3Crypt($data['random'] . $data['seqNO'] . $this->appSecretKey . $key);
|
|
|
|
|
|
//发送post接口请求
|
|
|
- $res = $this->https_post($this->tokenUrl,$data);
|
|
|
-
|
|
|
+ $res = $this->https_post($this->tokenUrl, $data);
|
|
|
+
|
|
|
//解密返回token数据
|
|
|
- $token = $this->SM2Decrypt(base64_decode($res['sm2EncryptData']),$k1);
|
|
|
+ $token = $this->SM2Decrypt(base64_decode($res['sm2EncryptData']), $k1);
|
|
|
|
|
|
$this->token = $token;
|
|
|
//返回token
|
|
@@ -105,8 +109,8 @@ class Gmdemo extends Controller{
|
|
|
'id' => 8989
|
|
|
],
|
|
|
'body' => [
|
|
|
- 'code'=>"000000",
|
|
|
- 'msg'=>"测试请求"
|
|
|
+ 'code' => "000000",
|
|
|
+ 'msg' => "测试请求"
|
|
|
]
|
|
|
];
|
|
|
$res = $this->scanPaymentCode($data);
|
|
@@ -116,12 +120,12 @@ class Gmdemo extends Controller{
|
|
|
}
|
|
|
|
|
|
//业务请求封装
|
|
|
- public function scanPaymentCode($array=[])
|
|
|
+ public 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);
|
|
|
+ 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));
|
|
@@ -130,7 +134,7 @@ class Gmdemo extends Controller{
|
|
|
//数据转json
|
|
|
$json = json_encode($array, JSON_UNESCAPED_UNICODE);
|
|
|
//获取随机6位字符串数字
|
|
|
- $seqNO = (string)rand(100000,999999);
|
|
|
+ $seqNO = (string)rand(100000, 999999);
|
|
|
//获取随机字符串秘钥,md5 并转大写
|
|
|
$key = strtoupper(md5($this->getKey()));
|
|
|
//拼接请求数据
|
|
@@ -139,40 +143,40 @@ class Gmdemo extends Controller{
|
|
|
'seqNO' => $seqNO,
|
|
|
'signMethod' => "SM3",
|
|
|
'encryptMethod' => "SM4",
|
|
|
- 'appAccessToken'=> $this->getToken(),//获取token
|
|
|
- 'sm2EncryptData' => $this->SM2Encrypt($key,$gf_k), //sm2加密key
|
|
|
- 'sm2Sign' => $this->SM2Sign($key,$k2,$k1), //sm2加密key
|
|
|
+ 'appAccessToken' => $this->getToken(),//获取token
|
|
|
+ 'sm2EncryptData' => $this->SM2Encrypt($key, $gf_k), //sm2加密key
|
|
|
+ 'sm2Sign' => $this->SM2Sign($key, $k2, $k1), //sm2加密key
|
|
|
];
|
|
|
|
|
|
//拼接签名参数,md5转大写
|
|
|
$data['sign'] = $this->SM3Crypt($json . $data['seqNO'] . $this->appSecretKey . $key);
|
|
|
//AES加密业务数据
|
|
|
- $data['reqData'] = $this->SM4Encrypt($json,$data['seqNO'] . $data['appAccessToken'] . $this->appSecretKey . $key);
|
|
|
+ $data['reqData'] = $this->SM4Encrypt($json, $data['seqNO'] . $data['appAccessToken'] . $this->appSecretKey . $key);
|
|
|
|
|
|
dump("业务数据json");
|
|
|
dump($json);
|
|
|
dump("请求数据");
|
|
|
dump($data);
|
|
|
- $res = $this->https_post("https://mouldai.com/apiSIT/tzyj/scanPaymentCode",$data);
|
|
|
+ $res = $this->https_post("https://mouldai.com/apiSIT/tzyj/scanPaymentCode", $data);
|
|
|
|
|
|
//解密sm2密钥
|
|
|
- $sm2Key = $this->SM2Decrypt(base64_decode($res['sm2EncryptData']),$k1);
|
|
|
+ $sm2Key = $this->SM2Decrypt(base64_decode($res['sm2EncryptData']), $k1);
|
|
|
|
|
|
- dump("sm2解密数据:".$sm2Key);
|
|
|
+ dump("sm2解密数据:" . $sm2Key);
|
|
|
|
|
|
//SM4解密数据
|
|
|
- $rspData = $this->SM4Decrypt($res['rspData'],$res['seqNO'] .$data['appAccessToken']. $this->appSecretKey. $sm2Key);
|
|
|
+ $rspData = $this->SM4Decrypt($res['rspData'], $res['seqNO'] . $data['appAccessToken'] . $this->appSecretKey . $sm2Key);
|
|
|
|
|
|
- dump("sm4解密报文数据:".$rspData);
|
|
|
+ dump("sm4解密报文数据:" . $rspData);
|
|
|
|
|
|
//sm3验签数据校验
|
|
|
- $sm3SignData = $this->SM3Crypt($rspData . $res['seqNO'] .$this->appSecretKey . $sm2Key);
|
|
|
+ $sm3SignData = $this->SM3Crypt($rspData . $res['seqNO'] . $this->appSecretKey . $sm2Key);
|
|
|
|
|
|
- dump("sm3响应报文拼接加签:".$sm3SignData);
|
|
|
+ dump("sm3响应报文拼接加签:" . $sm3SignData);
|
|
|
|
|
|
$signData = $res['sign'];
|
|
|
|
|
|
- dump("sm3响应报文签名:".$signData);
|
|
|
+ dump("sm3响应报文签名:" . $signData);
|
|
|
|
|
|
if ($sm3SignData == $signData) {
|
|
|
dump("验签成功!");
|
|
@@ -181,6 +185,7 @@ class Gmdemo extends Controller{
|
|
|
dump($res);
|
|
|
return $res;
|
|
|
}
|
|
|
+
|
|
|
//获取post数据并解密解签获得业务数据
|
|
|
public function getPostData()
|
|
|
{
|
|
@@ -215,14 +220,15 @@ class Gmdemo extends Controller{
|
|
|
}
|
|
|
|
|
|
//SM2Encrypt
|
|
|
- function SM2Encrypt($data,$pubkey){
|
|
|
+ function SM2Encrypt($data, $pubkey)
|
|
|
+ {
|
|
|
//sm2加密
|
|
|
$recode = php_SM2Encrypt($data, $cipher, $pubkey);
|
|
|
|
|
|
echo "<br/>recode:$recode \n";
|
|
|
|
|
|
//php_SM2Encrypt接口返回的密文值是c1c3c2格式,以下示例转DER编码
|
|
|
- $recode = php_SM2FormatConvert(102,$cipher, $der);
|
|
|
+ $recode = php_SM2FormatConvert(102, $cipher, $der);
|
|
|
echo "<br/>recode:$recode \n";
|
|
|
|
|
|
//SM2加密字符串转base64编码
|
|
@@ -234,30 +240,32 @@ class Gmdemo extends Controller{
|
|
|
}
|
|
|
|
|
|
//SM2Decrypt
|
|
|
- function SM2Decrypt($data,$privkey){
|
|
|
+ function SM2Decrypt($data, $privkey)
|
|
|
+ {
|
|
|
//php_SM2Decrypt 接收密文值是c1c3c2格式,若对方给的密文值是DER格式 则通过如下方式转换,然后再传入接口解密
|
|
|
- $recode = php_SM2FormatConvert(101,$data,$c1c3c2);
|
|
|
+ $recode = php_SM2FormatConvert(101, $data, $c1c3c2);
|
|
|
echo "<br/>recode:$recode \n";
|
|
|
|
|
|
//解密
|
|
|
- $recode = php_SM2Decrypt($c1c3c2,$plain,$privkey);
|
|
|
+ $recode = php_SM2Decrypt($c1c3c2, $plain, $privkey);
|
|
|
echo "<br/>recode:$recode \n";
|
|
|
echo "<br/>解密数据plain: $plain\n";
|
|
|
return $plain;
|
|
|
}
|
|
|
|
|
|
//SM2Sign
|
|
|
- function SM2Sign($data,$pubkey,$privkey){
|
|
|
+ function SM2Sign($data, $pubkey, $privkey)
|
|
|
+ {
|
|
|
//签名
|
|
|
- $recode = php_SM2Sign($data, $redata,$privkey,$pubkey);
|
|
|
+ $recode = php_SM2Sign($data, $redata, $privkey, $pubkey);
|
|
|
echo "<br/>recode:$recode \n";
|
|
|
|
|
|
//php_SM2Sign接口返回的签名值是RS格式,以下示例转DER编码
|
|
|
$php_func = 'php_SM2FormatConvert';
|
|
|
- $recode = $php_func(202,$redata, $der);
|
|
|
+ $recode = $php_func(202, $redata, $der);
|
|
|
echo "<br/>recode:$recode \n";
|
|
|
- echo "<br/>data: ".$data." len:".strlen($data)." \n";
|
|
|
- echo "<br/>signValue DER: ".base64_encode($der)." len:".strlen($der)." \n";
|
|
|
+ echo "<br/>data: " . $data . " len:" . strlen($data) . " \n";
|
|
|
+ echo "<br/>signValue DER: " . base64_encode($der) . " len:" . strlen($der) . " \n";
|
|
|
|
|
|
//加签数据base64返回
|
|
|
$sm2SignData = base64_encode($der);
|
|
@@ -265,59 +273,63 @@ class Gmdemo extends Controller{
|
|
|
}
|
|
|
|
|
|
//SM3Crypt
|
|
|
- function SM3Crypt($data){
|
|
|
- $recode = php_SM3Crypt($data,$sm3hash);
|
|
|
+ function SM3Crypt($data)
|
|
|
+ {
|
|
|
+ $recode = php_SM3Crypt($data, $sm3hash);
|
|
|
echo "<br/>recode:$recode \n";
|
|
|
//加签数据base64返回
|
|
|
return strtoupper($this->string2hex($sm3hash));
|
|
|
}
|
|
|
|
|
|
//16进制转换为二进制
|
|
|
- function hex2String($hexdata){
|
|
|
- $bindata="";
|
|
|
- for ($i=0;$i < strlen($hexdata);$i+=2) {
|
|
|
- $bindata.=chr(hexdec(substr($hexdata,$i,2)));
|
|
|
+ function hex2String($hexdata)
|
|
|
+ {
|
|
|
+ $bindata = "";
|
|
|
+ for ($i = 0; $i < strlen($hexdata); $i += 2) {
|
|
|
+ $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
|
|
|
}
|
|
|
return $bindata;
|
|
|
}
|
|
|
|
|
|
//SM4Encrypt
|
|
|
- function SM4Encrypt($data,$password){
|
|
|
+ function SM4Encrypt($data, $password)
|
|
|
+ {
|
|
|
//加密密码做特殊处理 先MD5转换大写 从第8位开始截取16个字符串
|
|
|
- $password = substr(strtoupper(md5($password)),8,16);
|
|
|
- echo "<br/>【SM4】MD5加密密码=======".$password;
|
|
|
+ $password = substr(strtoupper(md5($password)), 8, 16);
|
|
|
+ echo "<br/>【SM4】MD5加密密码=======" . $password;
|
|
|
|
|
|
//接口是密文key,将明文key加密后再传入php_SM4CBCCrypt进行数据加密
|
|
|
//不必要每次都加密key,建议手工加密一次,将key密文保存使用,key明文保存至安全的地方
|
|
|
//若是会话级别的key则自行定策略
|
|
|
- $recode = php_CryptKey(0, $password,$key);
|
|
|
+ $recode = php_CryptKey(0, $password, $key);
|
|
|
echo "<br/>recode:$recode \n";
|
|
|
|
|
|
//加密
|
|
|
- $recode = php_SM4CBCCrypt(0,$data,$redata,$key,$this->iv);
|
|
|
+ $recode = php_SM4CBCCrypt(0, $data, $redata, $key, $this->iv);
|
|
|
echo "<br/>recode:$recode \n";
|
|
|
- echo "<br/>redata: ".base64_encode($redata)." \n";
|
|
|
+ echo "<br/>redata: " . base64_encode($redata) . " \n";
|
|
|
|
|
|
$SM4Encryptdata = base64_encode($redata);
|
|
|
return $SM4Encryptdata;
|
|
|
}
|
|
|
|
|
|
//SM4Encrypt
|
|
|
- function SM4Decrypt($data,$password){
|
|
|
- echo "<br/>SM4解密数据=======".$data;
|
|
|
+ function SM4Decrypt($data, $password)
|
|
|
+ {
|
|
|
+ echo "<br/>SM4解密数据=======" . $data;
|
|
|
|
|
|
//加密密码做特殊处理
|
|
|
- $password = substr(strtoupper(md5($password)),8,16);
|
|
|
- echo "<br/>MD5密码=======".$password;
|
|
|
+ $password = substr(strtoupper(md5($password)), 8, 16);
|
|
|
+ echo "<br/>MD5密码=======" . $password;
|
|
|
|
|
|
//接口是密文key,将明文key加密后再传入php_SM4CBCCrypt进行数据加密
|
|
|
//不必要每次都加密key,建议手工加密一次,将key密文保存使用,key明文保存至安全的地方
|
|
|
//若是会话级别的key则自行定策略
|
|
|
- $recode = php_CryptKey(0, $password,$key);
|
|
|
+ $recode = php_CryptKey(0, $password, $key);
|
|
|
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, $this->iv);
|
|
|
echo "<br/>sm4[解密]recode:$recode \n";
|
|
|
return $dedata;
|
|
|
}
|
|
@@ -327,25 +339,29 @@ class Gmdemo extends Controller{
|
|
|
* @param 密文数据 $data
|
|
|
* @return 原文结果 string
|
|
|
*/
|
|
|
- private function decryptByPublicKey($data) {
|
|
|
+ private function decryptByPublicKey($data)
|
|
|
+ {
|
|
|
$data = base64_decode($data);
|
|
|
- openssl_public_decrypt($data,$decrypted,$this->publicKey,OPENSSL_PKCS1_PADDING);//公钥解密
|
|
|
+ openssl_public_decrypt($data, $decrypted, $this->publicKey, OPENSSL_PKCS1_PADDING);//公钥解密
|
|
|
return $decrypted;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 私钥加密
|
|
|
* @param 原始数据 $data
|
|
|
* @return 密文结果 string
|
|
|
*/
|
|
|
- private function encryptByPrivateKey($data) {
|
|
|
- openssl_private_encrypt($data,$encrypted,$this->privateKey,OPENSSL_PKCS1_PADDING);//私钥加密
|
|
|
+ private function encryptByPrivateKey($data)
|
|
|
+ {
|
|
|
+ openssl_private_encrypt($data, $encrypted, $this->privateKey, OPENSSL_PKCS1_PADDING);//私钥加密
|
|
|
$encrypted = base64_encode($encrypted);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
|
|
|
return $encrypted;
|
|
|
}
|
|
|
|
|
|
//封装返回数据
|
|
|
- private function runJson($code="000000",$msg="完成"){
|
|
|
- return json(['code'=>$code,'message'=>$msg]);
|
|
|
+ private function runJson($code = "000000", $msg = "完成")
|
|
|
+ {
|
|
|
+ return json(['code' => $code, 'message' => $msg]);
|
|
|
|
|
|
//以下加密信息返回,暂时不用使用
|
|
|
// $data = [
|
|
@@ -369,7 +385,7 @@ class Gmdemo extends Controller{
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
$header [] = 'Content-Type:application/x-www-form-urlencoded';
|
|
|
$ch = curl_init();
|
|
|
- curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
|
|
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
@@ -384,6 +400,7 @@ class Gmdemo extends Controller{
|
|
|
$tmpInfo1 = json_decode($tmpInfo, true);
|
|
|
return $tmpInfo1;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 获得随机字符串
|
|
|
**/
|