php将图片文件转成base64编码

/**
 * 获取图片的Base64编码(不支持url)
 * @date 2020-03-28
 * @param $img_file 传入本地图片地址
 * @param $type 是否带前缀, 1:带, 0:不带
 * @return array 0编码结果  1图片类型
 */
function imgToBase64($img_file, $type=1) {
    $img_base64 = '';
    if (file_exists($img_file)) {
        $app_img_file = $img_file; // 图片路径
        $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
        $fp = fopen($app_img_file, "r"); // 图片是否可读权限
        if ($fp) {
            $filesize = filesize($app_img_file);
            $content = fread($fp, $filesize);
            $file_content = chunk_split(base64_encode($content)); // base64编码
            switch ($img_info[2]) {           //判读图片类型
                case 1: $img_type = "gif";
                    break;
                case 2: $img_type = "jpg";
                    break;
                case 3: $img_type = "png";
                    break;
            }
			//合成图片的base64编码
            if($type){
				$img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;
			}else{
				$img_base64 = $file_content;
			}
        }
        fclose($fp);
    }
    return array($img_base64, $img_type);  //返回图片的base64及图片类型
}
//用法
$result = imgToBase64('/www/html/ken.01h.net/xxx.png');
//解码
$base64_string = explode(',', $result[0]); //截取data:image/png;base64, 这个逗号后的字符
$data = base64_decode($base64_string[1]);  //对截取后的字符使用base64_decode进行解码
file_put_contents('/www/html/ken.01h.net/yyy.'.$result[1], $data); //写入文件并保存

PHP使用对称加密算法(3DES)加密及解密示例

<?php
/**
 *    DESede(3DES)操作类库
 *    @author    Ken(QQ:2480990710)
 *    @blog		 http://ken.01h.net/
 *    @date      2020-3-25
 */
class DESede
{
    private $key = "";
    private $iv = "";

    /**
     * 构造
     * @param string $key
     * @param string $iv
     */
    function __construct($key, $iv)
    {
        $this->key = $key;
        $this->iv = $iv;
    }

    /**
     *加密
     * @param <type> $value
     * @return <type>
     */
    public function encrypt($value)
    {
        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');  //ecb模式
        $value = $this->PaddingPKCS7($value);
        @mcrypt_generic_init($td, $this->key, $this->iv);
        $ret = mcrypt_generic($td, $value);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $ret;
    }

    /**
     *解密
     * @param <type> $value
     * @return <type>
     */
    public function decrypt($value)
    {
        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');  //ecb模式
        @mcrypt_generic_init($td, $this->key, $this->iv);
        $ret = trim(mdecrypt_generic($td, $value));
        $ret = $this->UnPaddingPKCS7($ret);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $ret;
    }

    private function PaddingPKCS7($data)
    {
        $block_size = mcrypt_get_block_size('tripledes', 'ecb');  //ecb模式
        $padding_char = $block_size - (strlen($data) % $block_size);
        $data .= str_repeat(chr($padding_char), $padding_char);
        return $data;
    }

    private function UnPaddingPKCS7($text)
    {
        $pad = ord($text{strlen($text) - 1});
        if ($pad > strlen($text)) {
            return false;
        }
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
            return false;
        }
        return substr($text, 0, -1 * $pad);
    }
}

//用法
$key = '0123456789ABCDEFFEDCBA98765432100123456789ABCDEF';
$iv = '';
$msg = '6210985840034852662';
$key = hex2bin($key);  //上面的key是16进制的,所以这里转为二进制。
$des = new DESede($key, $iv);
$result1 = $des->encrypt($msg);
echo '加密结果(base64编码): ' . base64_encode($result1) . '<br />';
echo '加密结果(16进制): ' . bin2hex($result1) . '<br />';
$result2 = $des->decrypt($result1);
echo '解密结果: ' . $result2;

?>

关于WEB资金系统转账的安全保护,防止极快的重复并发请求绕过系统余额不足的判断

发现很多项目都有一个漏洞:比如账户A有100元,账户B有0元,然后从账户A转100元给账户B,正常的代码流程是,先检测账户A余额是否足够,然后从账户A中扣除100元,再给账户B增加100元,结果变成账户A剩下0元,账户B剩下100元,转账操作成功,逻辑上并没有问题,但是,如果该转账请求瞬间执行30次,结果发现其中大约有4、5次都是成功的,其余的请求则会报余额不足,原因是那4、5次成功的请求中余额不足的判断是同一时间执行的,所以都可以通过,假如有5次都通过了,结果很有可能就变成,账户A扣除5×100元(余额-400元),账户B增加了5×100元(余额500元),这样问题就大了,我们期望的结果是,无论瞬间请求多少次都要保证只有一次可以通过余额不足的检测,那么解决的办法就是让这段代码队列执行。

在PHP中让代码队列执行最常用的方法就是非阻塞的文件排他锁,示例如下:

//非阻塞的文件排他锁
$fp = fopen("/lock.txt", "w+");
if(!flock($fp, LOCK_EX | LOCK_NB)){
	echo '系统繁忙';
	exit();
}
//读取用户余额,判断是否足够
//......
if(){  //余额足够
	//执行转账逻辑
	//......
	flock($fp, LOCK_UN);  //释放锁
	echo '转账成功';
}else{
	echo '余额不足';
}
fclose($fp);

银联条码支付小微商户进件PHP版DEMO

本示例依赖于银联条码支付【统一】扫码支付PHP版DEMO,此DEMO可在银联官网下载(https://up.95516.com/open/openapi/doc?index_1=1&index_2=1&chapter_1=238&chapter_2=268),新增内容已在代码中标注说明。

<?php
/**
 * 支付接口调测例子
 * ================================================================
 * index 进入口,方法中转
 * submitOrderInfo 提交订单信息
 * queryOrder 查询订单
 * 
 * ================================================================
 */
require('Utils.class.php');
require('config/config.php');
require('class/RequestHandler.class.php');
require('class/ClientResponseHandler.class.php');
require('class/PayHttpClient.class.php');

Class Request{
  

    private $resHandler = null;
    private $reqHandler = null;
    private $pay = null;
    private $cfg = null;
    
    public function __construct(){
        $this->Request();
    }

    public function Request(){
        $this->resHandler = new ClientResponseHandler();
        $this->reqHandler = new RequestHandler();
        $this->pay = new PayHttpClient();
        $this->cfg = new Config();

        $this->reqHandler->setGateUrl($this->cfg->C('url'));

        $sign_type = $this->cfg->C('sign_type');
        
        if ($sign_type == 'MD5') {
            $this->reqHandler->setKey($this->cfg->C('key'));
            $this->resHandler->setKey($this->cfg->C('key'));
            $this->reqHandler->setSignType($sign_type);
        } else if ($sign_type == 'RSA_1_1' || $sign_type == 'RSA_1_256') {
            $this->reqHandler->setRSAKey($this->cfg->C('private_rsa_key'));
            $this->resHandler->setRSAKey($this->cfg->C('public_rsa_key'));
            $this->reqHandler->setSignType($sign_type);
        }
    }
	
	/* ------------------------------------------------- 新增内容 start ------------------------------------------------- */
	
    /**
     * 图片上传 2020-3-14 By Ken (http://ken.01h.net/) QQ:2480990710
     */
	public function picUpload($path){
		$post_data['partner'] = $this->cfg->C('sign_agentno');//合作伙伴ID即机构号
		$post_data['serviceName'] = 'pic_upload';//服务名称
		$post_data['signType'] = $this->cfg->C('sign_type');//签名类型
		$post_data['data'] = '';//请求数据
		//签名
		$signPars = "";
		ksort($post_data);
		foreach($post_data as $k => $v) {
			if("" != $v &amp;&amp; "dataSign" != $k &amp;&amp; "signType" != $k) {
				$signPars .= $k . "=" . $v . "&amp;";
			}
		}
		$signPars = substr($signPars, 0, -1) . $this->reqHandler->getKey();
		$post_data['dataSign'] = strtolower(md5($signPars));
		//图片
		if(substr(PHP_VERSION, 0, 3)<5.5){  //PHP5.5以下版本
			$post_data['picFile'] = '@'.$path;
		}else{  //PHP5.5及以上版本
			$post_data['picFile'] = new CURLFile($path);
		}
		$this->pay->setReqContent($this->cfg->C('up_url'), $post_data);
		if($this->pay->call()){
			$res = json_decode(json_encode(simplexml_load_string($this->pay->getResContent(), 'SimpleXMLElement', LIBXML_NOCDATA)), true);  //将XML转为array
			if($res['isSuccess'] == 'T'){  //成功
				return $res['pic'];
			}else{
				return array('status'=>500,'msg'=>'Error Code:'.$res['errorCode'].' Error Message:'.$res['errorMsg']);
			}
		}else{
			return array('status'=>500,'msg'=>'Response Code:'.$this->pay->getResponseCode().' Error Info:'.$this->pay->getErrInfo());
		}
	}
	
    /**
     * 小微商户进件 2020-3-14 By Ken (http://ken.01h.net/) QQ:2480990710
     */
	public function smallMchAdd($merchant){  //merchantName 、 outMerchantId  、 chPayAuth 、 limitCreditPay  、 merchantDetail['merchantShortName'] \ ['industrId'] \ ['province'] \ ['city'] \ ['county'] \ ['address'] \ ['email'] \ ['customerPhone'] \ ['principal'] \ ['principalMobile'] \ ['idCode'] \ ['indentityPhoto'] 、 bankAccount['accountCode'] \ ['bankId'] \ ['accountName'] \ ['accountType'] \ ['contactLine'] \ ['bankName'] \ ['province'] \ ['city'] \ ['idCardType'] \ ['idCard'] \ ['tel'] 、 mchPayConfs[0]['nodename'] \ ['payTypeId'] \ ['billRate'] 、 mchPayConfs[1]['nodename'] \ ['payTypeId'] \ ['billRate'] 、 mchPayConfs[2]['nodename'] \ ['payTypeId'] \ ['billRate'] \ ['billRate1'] ……
		$post_data['partner'] = $this->cfg->C('sign_agentno');//合作伙伴ID即机构号
		$post_data['serviceName'] = 'small_mch_add';//服务名称
		$post_data['signType'] = $this->cfg->C('sign_type');//签名类型
		//数组转XML
		$merchant_xml = simplexml_load_string('<merchant />');
		$this->create_xml($merchant, $merchant_xml);
		$data = $merchant_xml->saveXML();
		$post_data['data'] = str_replace('<?xml version="1.0"?>', '', $data);//请求数据
		//签名
		$signPars = "";
		ksort($post_data);
		foreach($post_data as $k => $v) {
			if("" != $v &amp;&amp; "dataSign" != $k &amp;&amp; "signType" != $k) {
				$signPars .= $k . "=" . $v . "&amp;";
			}
		}
		$signPars = substr($signPars, 0, -1) . $this->reqHandler->getKey();
		$post_data['dataSign'] = strtolower(md5($signPars));
		//组装请求串
        $o = "";
        foreach ($post_data as $k => $v) {
            $o.= "$k=" . urlencode($v) . "&amp;";
        }
        $post_data = substr($o, 0, -1);
		$this->pay->setReqContent($this->cfg->C('up_url'), $post_data);
		if($this->pay->call()){
			$res = json_decode(json_encode(simplexml_load_string($this->pay->getResContent(), 'SimpleXMLElement', LIBXML_NOCDATA)), true);  //将XML转为array
			if($res['isSuccess'] == 'T'){  //成功
				return $res['merchant'];
			}else{
				return array('status'=>500,'msg'=>'Error Code:'.$res['errorCode'].' Error Message:'.$res['errorMsg']);
			}
		}else{
			return array('status'=>500,'msg'=>'Response Code:'.$this->pay->getResponseCode().' Error Info:'.$this->pay->getErrInfo());
		}
	}
	private function create_xml($arr, $xml) {
		foreach($arr as $k=>$v) {
			if(is_array($v)) {  //数组
				if(array_keys($v) !== range(0, count($v)-1)) {  //关联数组
					$x = $xml->addChild($k);
					$this->create_xml($v, $x);
				} else {  //索引数组
					$x = $xml->addChild($k);
					foreach ($v as $vv) {
						$y = $x->addChild($vv['nodename']);
						unset($vv['nodename']);
						$this->create_xml($vv, $y);
					}
				}
			} else {
				$xml->addChild($k, $v);
			}
		}
	}
	
	/* ------------------------------------------------- 新增内容 end ------------------------------------------------- */
	
    /**
     * 提交订单
     */
    public function submitOrderInfo($info){
		
	}

?>

调用示例如下:

<?php
//调取接口
include './request.php';
$req = new Request();
//上传身份证图片
$indentityPhoto = array();
$card_front = $req->picUpload(dirname(__FILE__).'/upload/card_front.png');
$indentityPhoto[] = $card_front;
$card_back = $req->picUpload(dirname(__FILE__).'/upload/card_back.png');
$indentityPhoto[] = $card_back;
//上传收款银行卡照片
$accountCodePhoto = $req->picUpload(dirname(__FILE__).'/upload/bankPhoto.png');
//上传门头照
$mainPhoto = $req->picUpload(dirname(__FILE__).'/upload/shopPhoto.png');
//商户信息
$info = array();
//商户基本信息
$info['merchantName'] = '商户_张三';  //商户名称
$info['outMerchantId'] = 'UP_' . date('YmdHis') . rand(10, 99);  //外商户号
$info['chPayAuth'] = '1';  //授权机构交易
$info['limitCreditPay'] = '2';  //是否可用信用卡支付, 0:是   1:否   2:不限制
//商户详情信息
$info['merchantDetail']['merchantShortName'] = $info['merchantName'];  //商户简称
$info['merchantDetail']['industrId'] = '5331';  //行业类别: 5331 杂货便利店
$info['merchantDetail']['province'] = '3900';  //省份: 3900 福建
$info['merchantDetail']['city'] = '3930';  //城市: 3930 厦门
$info['merchantDetail']['county'] = '3933';  //区县: 3933 湖里
$info['merchantDetail']['address'] = '高新软件园华骏国际大厦七楼702单元';  //地址
$info['merchantDetail']['email'] = '2480990710@qq.com';  //邮箱
$info['merchantDetail']['customerPhone'] = '13600010002';  //客服电话
$info['merchantDetail']['principal'] = '张三';  //负责人
$info['merchantDetail']['principalMobile'] = '13600010002';  //负责人手机号
$info['merchantDetail']['idCode'] = '350604198705281012';  //证件号码
$info['merchantDetail']['indentityPhoto'] = implode(';', $indentityPhoto);  //身份证图片,多张图片以分号;隔开
$info['merchantDetail']['accountCodePhoto'] = $accountCodePhoto;  //收款银行卡照片
$info['merchantDetail']['mainPhoto'] = $mainPhoto;  //门头照
//银行账户信息
$info['bankAccount']['accountCode'] = '6222021402018746261';  //银行卡号
$info['bankAccount']['bankId'] = '1';  //开户银行名编号
$info['bankAccount']['accountName'] = '张三';  //开户人
$info['bankAccount']['accountType'] = '1';  //帐户类型  1:个人
$info['bankAccount']['contactLine'] = '102100099996';  //联行号
$info['bankAccount']['bankName'] = '中国工商银行厦门支行';  //开户支行名称
$info['bankAccount']['province'] = '3900';  //开户支行所在省
$info['bankAccount']['city'] = '3930';  //开户支行所在市
$info['bankAccount']['idCardType'] = '1';  //持卡人证件类型  1:身份证
$info['bankAccount']['idCard'] = '350604198705281012';  //持卡人证件号码
$info['bankAccount']['tel'] = '13600010002';  //银行账户手机号
//商户支付类型信息
$info['mchPayConfs'][] = array('nodename'=>'mchPayConf', 'payTypeId'=>'10014751', 'billRate'=>'3.8');  //支付宝
$info['mchPayConfs'][] = array('nodename'=>'mchPayConf', 'payTypeId'=>'10014761', 'billRate'=>'3.8');
$info['mchPayConfs'][] = array('nodename'=>'mchPayConf', 'payTypeId'=>'10014771', 'billRate'=>'3.8');
$info['mchPayConfs'][] = array('nodename'=>'mchPayConf', 'payTypeId'=>'10002016', 'billRate'=>'6', 'billRate1'=>'6', 'billRate2'=>'6', 'billRate3'=>'6');  //云闪付
$info['mchPayConfs'][] = array('nodename'=>'mchPayConf', 'payTypeId'=>'10000973', 'billRate'=>'6', 'billRate1'=>'6', 'billRate2'=>'6', 'billRate3'=>'6');
$info['mchPayConfs'][] = array('nodename'=>'mchPayConf', 'payTypeId'=>'10000974', 'billRate'=>'6', 'billRate1'=>'6', 'billRate2'=>'6', 'billRate3'=>'6');
$info['mchPayConfs'][] = array('nodename'=>'mchPayConf', 'payTypeId'=>'10000959', 'billRate'=>'3.8');  //微信
$info['mchPayConfs'][] = array('nodename'=>'mchPayConf', 'payTypeId'=>'10000960', 'billRate'=>'3.8');
$info['mchPayConfs'][] = array('nodename'=>'mchPayConf', 'payTypeId'=>'10000961', 'billRate'=>'3.8');
//发送请求
$result = $req->smallMchAdd($info);
if($result['merchantId']){  //进件成功
	//
}else{
	//
}
?>

PHP使用非对称加密算法(RSA)签名及验签、加密及解密示例

两个密钥:公开密钥(public_key)(以下简称公钥)和私有密钥(private_key)(以下简称私钥)。
公钥与私钥是一对,如果用公钥对数据进行签名(或加密),只有用对应的私钥才能验签(或解密);如果用私钥对数据进行签名(或加密),那么只有用对应的公钥才能验签(或解密)。
因为签名(或加密)和验签(或解密)使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。
两种应用场景:一种是签名与验签,这个只保证了数据来源的合法性,也就是你收到的数据是由你预期的对方发过来的;另一种是加密与解密,这个可以保证你的数据不会在网上裸奔,即便被劫持了也没关系。(注:传统的MD5只能对数据进行签名和验签,因为MD5是不可逆的,加密了就无法解密)
一般的应用场景都是用私钥签名、用公钥验签,或者用公钥加密、用私钥解密。
以支付宝的支付场景为例,在支付订单的时候,我们把订单交易数据通过我们自己的私钥(rsa_private_key)进行签名然后传给支付宝的服务器,支付宝的服务器接收到数据后会用我们自己的公钥(rsa_public_key)进行验签,这就是为什么要把我们自己的公钥(rsa_public_key)通过支付宝控制台传上去的原因了;而在订单异步通知的时候,支付宝会用他们自己的私钥(alipay_private_key)对订单通知数据进行签名然后传给我们的服务器,我们的服务器在接收到数据后只能用支付宝的公钥(alipay_public_key)进行验签,这就是为什么我们要去支付宝控制台获取到支付宝公钥的原因了。
如果要对传输的数据进行加密,那么我们在发送数据之前要用支付宝的公钥对数据加密,而支付宝那边收到数据后则用他们自己的私钥进行解密;而支付宝那边在发送数据之前就会用我们的公钥对数据进行加密,而我们在收到数据后则用我们自己的私钥进行解密。
PHP的RSA签名与验签及加密与解密代码示例如下:
<?php
/**
 *    RSA操作类库
 *    @author    Ken(QQ:2480990710)
 *    @blog		 http://ken.01h.net/
 *    @date      2020-3-12
 */
class Rsa{
	
	//RSA or RSA2
	public $signType = 'RSA';
	
	//私钥文件路径(与私钥值二选一)
	public $rsaPrivateKeyFilePath;
	//私钥值
	public $rsaPrivateKey;
	
	//公钥文件路径(与公钥值二选一)
	public $rsaPublicKeyFilePath;
	//公钥值
	public $rsaPublicKey;
	
	/**
	 * 校验$value是否非空
	 *  if not set ,return true;
	 *  if is null , return true;
	 */
	private function checkEmpty($value) {
		if (!isset($value))
			return true;
		if ($value === null)
			return true;
		if (trim($value) === "")
			return true;

		return false;
	}
	
	/**     
     * 获取私钥     
     * @return bool|resource     
     */    
    private function getPrivateKey()
    {        
		if($this->checkEmpty($this->rsaPrivateKeyFilePath)){
			$priKey = $this->rsaPrivateKey;
			$res = "-----BEGIN RSA PRIVATE KEY-----\n" .
				wordwrap($priKey, 64, "\n", true) .
				"\n-----END RSA PRIVATE KEY-----";
		}else {
			$priKey = file_get_contents($this->rsaPrivateKeyFilePath);
			$res = openssl_pkey_get_private($priKey);
		}
		return $res;
    }

    /**     
     * 获取公钥     
     * @return bool|resource     
     */    
    private function getPublicKey()
    {
		if($this->checkEmpty($this->rsaPublicKeyFilePath)){
			$pubKey = $this->rsaPublicKey;
			$res = "-----BEGIN PUBLIC KEY-----\n" .
				wordwrap($pubKey, 64, "\n", true) .
				"\n-----END PUBLIC KEY-----";
		}else {
			$pubKey = file_get_contents($this->rsaPublicKeyFilePath);
			$res = openssl_pkey_get_public($pubKey);
		}
		return $res;
    }
	
    /**     
     * 私钥签名     
	 * @param string $data  
     * @return null|string    
     */ 
	public function sign($data) {
		$res = $this->getPrivateKey();
		if ("RSA2" == $this->signType) {
			openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256);
		} else {
			openssl_sign($data, $sign, $res);
		}
		if(!$this->checkEmpty($this->rsaPrivateKeyFilePath)){
			openssl_free_key($res);
		}
		$sign = base64_encode($sign);
		return $sign;
	}
	
    /**     
     * 公钥验签     
	 * @param string $data  
	 * @param string $sign  
     * @return bool    
     */ 
	public function verify($data, $sign) {
		$res = $this->getPublicKey();
		if ("RSA2" == $this->signType) {
			$result = (bool)openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_SHA256);
		} else {
			$result = (bool)openssl_verify($data, base64_decode($sign), $res);
		}
		if(!$this->checkEmpty($this->rsaPublicKeyFilePath)) {
			openssl_free_key($res);
		}
		return $result;
	}

    /**     
     * 公钥加密     
     * @param string $data     
     * @return null|string     
     */    
    public function publicEncrypt($data)
    {
		if (!is_string($data)) {
            return null;
        }
		$res = $this->getPublicKey();
		if ("RSA2" == $this->signType) {
			$max_encrypt_block = 245;
		} else {
			$max_encrypt_block = 117;
		}
		$result = '';
		while($data){
			$input = substr($data, 0, $max_encrypt_block);
			$data = substr($data, $max_encrypt_block);
			openssl_public_encrypt($input, $encrypted, $res);
			$result .= $encrypted;
		}
		if(!$this->checkEmpty($this->rsaPublicKeyFilePath)) {
			openssl_free_key($res);
		}
		$result = base64_encode($result);
		return $result;
    }

    /**     
     * 私钥解密     
     * @param string $encrypted     
     * @return null|string     
     */    
    public function privDecrypt($encrypted)
    {
		if (!is_string($encrypted)) {
            return null;
        }
		$res = $this->getPrivateKey();
		if ("RSA2" == $this->signType) {
			$max_decrypt_block = 256;
		} else {
			$max_decrypt_block = 128;
		}
		$content = base64_decode($encrypted);
		$result  = '';
		for($i = 0; $i < strlen($content)/$max_decrypt_block; $i++  ) {
			$data = substr($content, $i * $max_decrypt_block, $max_decrypt_block);
			openssl_private_decrypt($data, $decrypt, $res);
			$result .= $decrypt;
		}
		if(!$this->checkEmpty($this->rsaPrivateKeyFilePath)){
			openssl_free_key($res);
		}
		return $result;
    }
	
}

$data = 'version=1.0&amp;service=orderPay&amp;mch_id=1000000000001';
$rsa = new Rsa();
//设置公钥
$rsa->rsaPublicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCIaDx0DMC0NxLssTPZg3VZOX8xnW1hu74AK4Kwzv+fpZT9JiliIU/gTBwTJQNCnR2LbFR0DOMRnWUafrbI0IcbA2VA3sO+z/WwfGYTy9/fZnNOXr91Jgg7Hln+7qM7aWy2MoGY4M1s8NvtHbtMc+FZFQwVzgjipGDReEaK3ahGCQIDAQAB';
//设置私钥
$rsa->rsaPrivateKey = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIhoPHQMwLQ3EuyxM9mDdVk5fzGdbWG7vgArgrDO/5+llP0mKWIhT+BMHBMlA0KdHYtsVHQM4xGdZRp+tsjQhxsDZUDew77P9bB8ZhPL399mc05ev3UmCDseWf7uoztpbLYygZjgzWzw2+0du0xz4VkVDBXOCOKkYNF4RordqEYJAgMBAAECgYAxXbfzL+JV1lCtBHyFTcQcZGiZ5iV951od6aFEp8VvW5eN/k+fyfqEbEcEzrJdW0sj+DKgRUGwUEwUyKMSDhW2r8mFr0wlWDvOJDpVl1VYjSxp2yhwjg0H1uTjGZhf9G1GcWwJHnD3U54ZrzgtqGiEvqK64qIl1koGXG5ww5mu4QJBAO4CZfw8lF+wD2+YlT1x4e4ku4NyI9awihadV4LN3NiHs1Ubm2RuW4ca/hcH5oToRrRmypmOT8bxyWMMkrj5CIUCQQCSt8itnsDfxyWdhEX1ecyn+25bkZQo1fn+ujFxc6ZmGI0vym22HZji/ghKUAug+i6Owtn896/O80m5V/ZZ5EC1AkEAiWAjA2Ln9Q8G6c+1HEEWOcFD5gvEec9t9L2eXCZ8eRJiRRZpK5+y/plq3Vo3CLGU1d2axOTqURcPuTbxnQhIjQJAUpQ80HQVTR7S7iigE719UmMzRzjWMnHVZuk3oQqd8sMI3IhMXf+kqMagter90JpgEBxeA4MJoirPKRH4Z7oQLQJAJm5Fol7pjrCAVin5P1yxpD5ZZqPU4vzyk4tOomFYShtjV+6GQrNQNS6jmCZ2ARz4k0b0LrMnk+Iv9JQXFsDKiw==';
//签名
$sign = $rsa->sign($data);
echo "\r\n------------------sign--------------------\r\n";
echo $sign;
//验签
$verify = $rsa->verify($data, $sign);
echo "\r\n------------------verify--------------------\r\n";
var_dump($verify);
//公钥加密
$result = $rsa->publicEncrypt($data);
echo "\r\n------------------publicEncrypt--------------------\r\n";
echo $result;
//私钥解密
$data2 = $rsa->privDecrypt($result);
echo "\r\n------------------privDecrypt--------------------\r\n";
echo $data2;
?>