123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- date_default_timezone_set("Asia/Shanghai");
- //--------------------------------------------1、基础参数配置------------------------------------------------
- $config = include('../config/Basics.php');
- // 获取公私钥匙
- $priKey = loadPk12Cert($config['privateKeyPath'], $config['privateKeyPwd']);
- //$priKey_2 = loadPk12Cert(PRI_KEY_PATH_2, CERT_PWD);
- $pubKey = loadX509Cert($config['publicKeyPath']);
- //--------------------------------------------end基础参数配置------------------------------------------------
- /**
- * 获取公钥
- * @param $path
- * @return mixed
- * @throws Exception
- */
- function loadX509Cert($path)
- {
- try {
- $file = file_get_contents($path);
- if (!$file) {
- throw new \Exception('loadx509Cert::file_get_contents ERROR');
- }
- $cert = chunk_split(base64_encode($file), 64, "\n");
- $cert = "-----BEGIN CERTIFICATE-----\n" . $cert . "-----END CERTIFICATE-----\n";
- $res = openssl_pkey_get_public($cert);
- $detail = openssl_pkey_get_details($res);
- openssl_free_key($res);
- if (!$detail) {
- throw new \Exception('loadX509Cert::openssl_pkey_get_details ERROR');
- }
- return $detail['key'];
- } catch (\Exception $e) {
- throw $e;
- }
- }
- /**
- * 获取私钥
- * @param $path
- * @param $pwd
- * @return mixed
- * @throws Exception
- */
- function loadPk12Cert($path, $pwd)
- {
- try {
- $file = file_get_contents($path);
- if (!$file) {
- throw new \Exception('loadPk12Cert::file
- _get_contents');
- }
- if (!openssl_pkcs12_read($file, $cert, $pwd)) {
- throw new \Exception('loadPk12Cert::openssl_pkcs12_read ERROR');
- }
- return $cert['pkey'];
- } catch (\Exception $e) {
- throw $e;
- }
- }
- /**
- * 私钥签名
- * @param $plainText
- * @param $path
- * @return string
- * @throws Exception
- */
- function sign($plainText, $path)
- {
- //$plainText = json_encode($plainText);
- try {
- $resource = openssl_pkey_get_private($path);
- $result = openssl_sign($plainText, $sign, $resource);
- openssl_free_key($resource);
- if (!$result) {
- throw new \Exception('签名出错' . $plainText);
- }
- return base64_encode($sign);
- } catch (\Exception $e) {
- throw $e;
- }
- }
- /**
- * 秘钥加密
- * Author: Tao.
- *
- * @param string $data 之前生成好的需加密内容
- * @param $key 私钥证书位置(.pfx文件)
- * @param string $pwd 证书密码
- *
- * @return string
- */
- function SHA1withRSA($data, $key,$pwd)
- {
- openssl_pkcs12_read(file_get_contents($key), $certs, $pwd);
- if (!$certs) return;
- $signature = '';
- openssl_sign($data, $signature, $certs['pkey']);
- return bin2hex($signature);
- }
- /**
- * 公钥验签
- * @param $plainText
- * @param $sign
- * @param $path
- * @return int
- * @throws Exception
- */
- function verify($plainText, $sign, $path)
- {
- $resource = openssl_pkey_get_public($path);
- $result = openssl_verify($plainText, base64_decode($sign), $resource);
- openssl_free_key($resource);
- if (!$result) {
- throw new \Exception('签名验证未通过,plainText:' . $plainText . '。sign:' . $sign, '02002');
- }
- return $result;
- }
- /**
- * 公钥加密AESKey
- * @param $plainText
- * @param $puk
- * @return string
- * @throws Exception
- */
- function RSAEncryptByPub($plainText, $puk)
- {
- if (!openssl_public_encrypt($plainText, $cipherText, $puk, OPENSSL_PKCS1_PADDING)) {
- throw new \Exception('AESKey 加密错误');
- }
- return base64_encode($cipherText);
- }
- /**
- * 私钥解密AESKey
- * @param $cipherText
- * @param $prk
- * @return string
- * @throws Exception
- */
- function RSADecryptByPri($cipherText, $prk)
- {
- if (!openssl_private_decrypt(base64_decode($cipherText), $plainText, $prk, OPENSSL_PKCS1_PADDING)) {
- throw new \Exception('AESKey 解密错误');
- }
- return (string)$plainText;
- }
- /**
- * AES加密
- * @param $plainText
- * @param $key
- * @return string
- * @throws \Exception
- */
- function AESEncrypt($plainText, $key)
- {
- ksort($plainText);
- $plainText = json_encode($plainText);
- $ivlen = openssl_cipher_iv_length($cipher="AES-128-ECB");
- $iv = openssl_random_pseudo_bytes($ivlen);
- $result = openssl_encrypt($plainText, 'AES-128-ECB', $key,OPENSSL_RAW_DATA,$iv);
- //var_dump($iv);
- if (!$result) {
- throw new \Exception('报文加密错误');
- }
- return base64_encode($result);
- }
- /**
- * AES解密
- * @param $cipherText
- * @param $key
- * @return string
- * @throws \Exception
- */
- function AESDecrypt($cipherText, $key)
- {
- $result = openssl_decrypt(base64_decode($cipherText), 'AES-128-ECB', $key, 1);
- if (!$result) {
- throw new \Exception('报文解密错误', 2003);
- }
- return $result;
- }
- /**
- * 生成AESKey
- * @param $size
- * @return string
- */
- function aes_generate($size)
- {
- $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- $arr = array();
- for ($i = 0; $i < $size; $i++) {
- $arr[] = $str[mt_rand(0, 61)];
- }
- return implode('', $arr);
- }
- /**
- * 发送请求
- * @param $url
- * @param $param
- * @return bool|mixed
- * @throws Exception
- */
- function http_post_json($url, $param)
- {
- if (empty($url) || empty($param)) {
- return false;
- }
- $param = json_encode($param);
- try {
- $ch = curl_init();//初始化curl
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //正式环境时解开注释
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $data = curl_exec($ch);//运行curl
- curl_close($ch);
- if (!$data) {
- throw new \Exception('请求出错');
- }
- return $data;
- } catch (\Exception $e) {
- throw $e;
- }
- }
- // 表单请求接口
- function form($apiName,$data)
- {
- $config = include('../config/Basics.php');
- $apiMap = include('../helper/Map.php');
-
- if (!isset($apiMap[$apiName])) {
- throw new \Exception('接口名错误');
- }
- //提交数据
- $postData = $data;
- $url = $config['apiUrl'] . $apiMap[$apiName]['url'];
- $form = '<form action="' . $url . '" method="post">';
- foreach ($postData as $k => $v) {
- $form .= "{$k} <p><input type='text' name='{$k}' value='{$v}'></p>";
- }
- $form .= '<input type="submit" value="提交"></form>';
- return ['form'=>$form,'postData'=>$postData];
- }
- function base64EncodeImage ($image_file) {
- //$base64_image = "";
- // $image_info = getimagesize($image_file);
- $image_data = file_get_contents($image_file);
- //$image_data = fread(fopen($image_file, "r"), filesize($image_file));
- // $base64_image = "data:" . $image_info["mime"] . ";base64," . chunk_split(base64_encode($image_data));
- // $base64_image = "data:" . $image_info["mime"] . ";base64," . base64_encode($image_data);
- return base64_encode($image_data);
- // return $base64_image;
- }
- // function base64EncodeImage ($image_file) {
- // //$file:图片地址
- // //Filetype: JPEG,PNG,GIF
- // $file = $image_file;
- // if($fp = fopen($file,"rb", 0))
- // {
- // $gambar = fread($fp,filesize($file));
- // fclose($fp);
-
- // // $base64 = chunk_split(base64_encode($gambar));
- // $base64 = base64_encode($gambar);
- // return $base64;
- // // 输出
- // // $encode = '<img src="data:image/jpg/png/gif;base64,' . $base64 .'" >';
- // // echo $encode;
- // }
- // }
-
|