PHP JAVA AES加密 Apache Hex.encodeHex

<?php

namespace App\Http\Controllers;

use Utils;

class ThirdPartyInterfaceOfMINISO extends ThirdPartyInterface
{
    const SIGN = 'Sign'; // SIGN

  
    public function cancel($couponCode, string $remark, $keyValue=null, string $apiCancel='', string $apiQuery='')
    {
        $canUpdate = false;
        if (!empty($apiCancel))
        {
            $data['couponList'] = [
                'coupon_code' => $couponCode->code,
            ];
            $data['discardMsg'] = $remark;

            $post['_platform_num'] = $keyValue->platformNum;
            $post['appKey'] = $keyValue->appId;
            $post['nonce'] = sha1(Utils::microSecTime());
            $post['nonce'] = 'd3ddaa6d0eb5cc924ad2fcd71a9c97764f8b4784';
            $post['reqTime'] = Utils::microSecTime();
            $post['reqTime'] = '1598342006860';
            $post['_token'] = $keyValue->token;

            $iv = [
                //11/103/-76/-22/-53/-114/40/1/-112/-52/-105/80/21/-78/-122/122/
                '11',
                '103',
                '-76',
                '-22',
                '-53',
                '-114',
                '40',
                '1',
                '-112',
                '-52',
                '-105',
                '80',
                '21',
                '-78',
                '-122',
                '122',
            ];
            $post['_iv'] = bin2hex($this->bytesToStr($iv));
            $post['_iv'] = 'e8dd80cb33028d1a678abd9a1c4209da';

            $iv = pack("H*",$post['_iv']);
            $aesKey = pack("H*",$keyValue->aesKey);

            $cipherText = $this->encrypt(json_encode($data), $aesKey, $iv);

            $post['sign'] = $this->signing($post, $keyValue->appSecret, bin2hex($cipherText));
            $cookie = '';
            foreach ($post as $key => $val) {
                $cookie .= $key.'='.$val.';';
            }

            $resultUrl = $this->curlCookiePostJson($apiCancel, $cookie, json_encode($data));

            print_r($resultUrl);
        }

    }


    public function getBytes($string) {
        $bytes = array();
        for($i = 0; $i < strlen($string); $i++){
            echo $string[$i].'-';
            $bytes[] = ord($string[$i]);
        }
        return $bytes;
    }


    function bytesToStr($bytes)
    {
        $str = '';
        foreach ($bytes as $ch) {
            $str .= chr($ch);
        }
        return $str;
    }

    /**
     * 签名算法
     * @param $data
     * @param $appSecret
     * @param $iv
     * @return string
     */
    public function signing(array $data, string $appSecret, string $iv)
    {
        ksort($data);

        $str = '';
        foreach ($data as $key => $value) {
            $str .= $key . $value;
        }

        return strtoupper(md5($appSecret.$str.$iv));
    }


    public function encrypt($data, string $aesKey, string $aesIv)
    {
        //return openssl_encrypt($data, 'aes-256-cbc', $aesKey, OPENSSL_RAW_DATA, $aesIv);

        return openssl_encrypt($data, "aes-128-cbc", $aesKey, 1, $aesIv);
    }

    public function curlCookiePostJson(string $url, $cookie, string $json)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        if (substr($url, 0, 5) == 'https') {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证对等证书
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); //不检查服务器SSL证书
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_COOKIE , $cookie);

        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($curl, CURLOPT_TIMEOUT, 3600);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
        curl_setopt($curl, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json+cipher',
            'Content-Length: ' . strlen($json)
        ]);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

}
<?php

namespace App\Http\Controllers;

use Utils;

class ThirdPartyInterfaceSO extends ThirdPartyInterface
{
    const SIGN = 'sign'; // SIGN

    /**
     * @param $couponCode
     * @param string $remark
     * @param null $keyValue
     * @param string $apiCancel
     * @param string $apiQuery
     * @return mixed
     */
    public function cancel($couponCode, string $remark, $keyValue=null, string $apiCancel='', string $apiQuery='')
    {
        $canUpdate = false;
        if (!empty($apiCancel))
        {
            // 作废数据
            $code[] =  [
                'coupon_code' => $couponCode->code,
            ];
            $data['couponList'] = json_encode($code);
            $data['discardMsg'] = $remark;
            // cookie消息主体
            $post['_platform_num'] = $keyValue->platformNum;
            $post['appKey'] = $keyValue->appId;
            $post['nonce'] = sha1(Utils::microSecTime());
            $post['reqTime'] = Utils::microSecTime();
            $post['_token'] = $keyValue->token;
            $post['_iv'] = 'e8dd80cb33028d1a678abd9a1c4209da';
            // 十六进制转换为二进制
            $iv = pack("H*",$post['_iv']);
            $aesKey = pack("H*",$keyValue->aesKey);
            // 二进制转换为十六进制
            $cipherText = bin2hex($this->encrypt(json_encode($data), $aesKey, $iv));
            // 签名
            $post['sign'] = $this->signing($post, $keyValue->appSecret, $cipherText);
            // cookie
            $cookie = '';
            foreach ($post as $key => $val) {
                $cookie .= $key.'='.$val.';';
            }
            // 作废券请求
            $result = $this->curlCookiePostJson($apiCancel, $cookie, $cipherText);
            $this->log->info('[作废]['.$this->key.'][三方数据]', $result);
            $result = json_decode($result, true);

            // success是否成功, code=0表示成功,其他均为失败。
            if(isset($result['success'],$result['code']) && $result['success'] && $result['code'] == 0){
                // 处理结果 $result['content']['result']['ok'] = 1成功 0失败
                if(isset($result['content'], $result['content']['result']) && $result['content']['result']['ok']){
                    $this->log->info('[作废]['.$this->key.'][成功]'.$couponCode->serial_number, $result);
                    $canUpdate = true;
                }else{
                    $this->log->warn('[作废]['.$this->key.'][失败]'.$couponCode->serial_number, $result, '', json_encode($post), true);
                }
            }
        }
        // 数据更新
        return $this->cancelUpdate($couponCode, $remark, $canUpdate);
    }



    /**
     * 签名算法
     * @param $data
     * @param $appSecret
     * @param $iv
     * @return string
     */
    public function signing(array $data, string $appSecret, string $iv)
    {
        ksort($data);
        $str = '';
        foreach ($data as $key => $value) {
            $str .= $key . $value;
        }
        return strtoupper(md5($appSecret.$str.$iv));
    }


    /**
     * aes加密
     * @param $data
     * @param string $aesKey
     * @param string $aesIv
     * @return string
     */
    public function encrypt($data, string $aesKey, string $aesIv)
    {
        return openssl_encrypt($data, "aes-128-cbc", $aesKey, 1, $aesIv);
    }


    /**
     * aes解密
     * @param $data
     * @param string $aesKey
     * @param string $aesIv
     * @return string
     */
    public function decrypt($data, string $aesKey, string $aesIv)
    {
        //$cipherText = $this->decrypt(pack("H*", $cipherText), $aesKey, $iv);
        return openssl_decrypt($data, 'aes-128-cbc', $aesKey, 1, $aesIv);
    }


    /**
     * curl
     * @param string $url
     * @param $cookie
     * @param $string
     * @return mixed
     */
    public function curlCookiePostJson(string $url, $cookie, $string)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        if (substr($url, 0, 5) == 'https') {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证对等证书
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); //不检查服务器SSL证书
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_COOKIE , $cookie);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($curl, CURLOPT_TIMEOUT, 3600);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
        curl_setopt($curl, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json+cipher',
            'Content-Length: ' . strlen($string)
        ]);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值