Common.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. date_default_timezone_set("Asia/Shanghai");
  3. //--------------------------------------------1、基础参数配置------------------------------------------------
  4. $config = include('../config/Basics.php');
  5. // 获取公私钥匙
  6. $priKey = loadPk12Cert($config['privateKeyPath'], $config['privateKeyPwd']);
  7. //$priKey_2 = loadPk12Cert(PRI_KEY_PATH_2, CERT_PWD);
  8. $pubKey = loadX509Cert($config['publicKeyPath']);
  9. //--------------------------------------------end基础参数配置------------------------------------------------
  10. /**
  11. * 获取公钥
  12. * @param $path
  13. * @return mixed
  14. * @throws Exception
  15. */
  16. function loadX509Cert($path)
  17. {
  18. try {
  19. $file = file_get_contents($path);
  20. if (!$file) {
  21. throw new \Exception('loadx509Cert::file_get_contents ERROR');
  22. }
  23. $cert = chunk_split(base64_encode($file), 64, "\n");
  24. $cert = "-----BEGIN CERTIFICATE-----\n" . $cert . "-----END CERTIFICATE-----\n";
  25. $res = openssl_pkey_get_public($cert);
  26. $detail = openssl_pkey_get_details($res);
  27. openssl_free_key($res);
  28. if (!$detail) {
  29. throw new \Exception('loadX509Cert::openssl_pkey_get_details ERROR');
  30. }
  31. return $detail['key'];
  32. } catch (\Exception $e) {
  33. throw $e;
  34. }
  35. }
  36. /**
  37. * 获取私钥
  38. * @param $path
  39. * @param $pwd
  40. * @return mixed
  41. * @throws Exception
  42. */
  43. function loadPk12Cert($path, $pwd)
  44. {
  45. try {
  46. $file = file_get_contents($path);
  47. if (!$file) {
  48. throw new \Exception('loadPk12Cert::file
  49. _get_contents');
  50. }
  51. if (!openssl_pkcs12_read($file, $cert, $pwd)) {
  52. throw new \Exception('loadPk12Cert::openssl_pkcs12_read ERROR');
  53. }
  54. return $cert['pkey'];
  55. } catch (\Exception $e) {
  56. throw $e;
  57. }
  58. }
  59. /**
  60. * 私钥签名
  61. * @param $plainText
  62. * @param $path
  63. * @return string
  64. * @throws Exception
  65. */
  66. function sign($plainText, $path)
  67. {
  68. //$plainText = json_encode($plainText);
  69. try {
  70. $resource = openssl_pkey_get_private($path);
  71. $result = openssl_sign($plainText, $sign, $resource);
  72. openssl_free_key($resource);
  73. if (!$result) {
  74. throw new \Exception('签名出错' . $plainText);
  75. }
  76. return base64_encode($sign);
  77. } catch (\Exception $e) {
  78. throw $e;
  79. }
  80. }
  81. /**
  82. * 秘钥加密
  83. * Author: Tao.
  84. *
  85. * @param string $data 之前生成好的需加密内容
  86. * @param $key 私钥证书位置(.pfx文件)
  87. * @param string $pwd 证书密码
  88. *
  89. * @return string
  90. */
  91. function SHA1withRSA($data, $key,$pwd)
  92. {
  93. openssl_pkcs12_read(file_get_contents($key), $certs, $pwd);
  94. if (!$certs) return;
  95. $signature = '';
  96. openssl_sign($data, $signature, $certs['pkey']);
  97. return bin2hex($signature);
  98. }
  99. /**
  100. * 公钥验签
  101. * @param $plainText
  102. * @param $sign
  103. * @param $path
  104. * @return int
  105. * @throws Exception
  106. */
  107. function verify($plainText, $sign, $path)
  108. {
  109. $resource = openssl_pkey_get_public($path);
  110. $result = openssl_verify($plainText, base64_decode($sign), $resource);
  111. openssl_free_key($resource);
  112. if (!$result) {
  113. throw new \Exception('签名验证未通过,plainText:' . $plainText . '。sign:' . $sign, '02002');
  114. }
  115. return $result;
  116. }
  117. /**
  118. * 公钥加密AESKey
  119. * @param $plainText
  120. * @param $puk
  121. * @return string
  122. * @throws Exception
  123. */
  124. function RSAEncryptByPub($plainText, $puk)
  125. {
  126. if (!openssl_public_encrypt($plainText, $cipherText, $puk, OPENSSL_PKCS1_PADDING)) {
  127. throw new \Exception('AESKey 加密错误');
  128. }
  129. return base64_encode($cipherText);
  130. }
  131. /**
  132. * 私钥解密AESKey
  133. * @param $cipherText
  134. * @param $prk
  135. * @return string
  136. * @throws Exception
  137. */
  138. function RSADecryptByPri($cipherText, $prk)
  139. {
  140. if (!openssl_private_decrypt(base64_decode($cipherText), $plainText, $prk, OPENSSL_PKCS1_PADDING)) {
  141. throw new \Exception('AESKey 解密错误');
  142. }
  143. return (string)$plainText;
  144. }
  145. /**
  146. * AES加密
  147. * @param $plainText
  148. * @param $key
  149. * @return string
  150. * @throws \Exception
  151. */
  152. function AESEncrypt($plainText, $key)
  153. {
  154. ksort($plainText);
  155. $plainText = json_encode($plainText);
  156. $ivlen = openssl_cipher_iv_length($cipher="AES-128-ECB");
  157. $iv = openssl_random_pseudo_bytes($ivlen);
  158. $result = openssl_encrypt($plainText, 'AES-128-ECB', $key,OPENSSL_RAW_DATA,$iv);
  159. //var_dump($iv);
  160. if (!$result) {
  161. throw new \Exception('报文加密错误');
  162. }
  163. return base64_encode($result);
  164. }
  165. /**
  166. * AES解密
  167. * @param $cipherText
  168. * @param $key
  169. * @return string
  170. * @throws \Exception
  171. */
  172. function AESDecrypt($cipherText, $key)
  173. {
  174. $result = openssl_decrypt(base64_decode($cipherText), 'AES-128-ECB', $key, 1);
  175. if (!$result) {
  176. throw new \Exception('报文解密错误', 2003);
  177. }
  178. return $result;
  179. }
  180. /**
  181. * 生成AESKey
  182. * @param $size
  183. * @return string
  184. */
  185. function aes_generate($size)
  186. {
  187. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  188. $arr = array();
  189. for ($i = 0; $i < $size; $i++) {
  190. $arr[] = $str[mt_rand(0, 61)];
  191. }
  192. return implode('', $arr);
  193. }
  194. /**
  195. * 发送请求
  196. * @param $url
  197. * @param $param
  198. * @return bool|mixed
  199. * @throws Exception
  200. */
  201. function http_post_json($url, $param)
  202. {
  203. if (empty($url) || empty($param)) {
  204. return false;
  205. }
  206. $param = json_encode($param);
  207. try {
  208. $ch = curl_init();//初始化curl
  209. curl_setopt($ch, CURLOPT_URL, $url);
  210. curl_setopt($ch, CURLOPT_POST, 1);
  211. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  212. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  213. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  214. //正式环境时解开注释
  215. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  216. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  217. $data = curl_exec($ch);//运行curl
  218. curl_close($ch);
  219. if (!$data) {
  220. throw new \Exception('请求出错');
  221. }
  222. return $data;
  223. } catch (\Exception $e) {
  224. throw $e;
  225. }
  226. }
  227. // 表单请求接口
  228. function form($apiName,$data)
  229. {
  230. $config = include('../config/Basics.php');
  231. $apiMap = include('../helper/Map.php');
  232. if (!isset($apiMap[$apiName])) {
  233. throw new \Exception('接口名错误');
  234. }
  235. //提交数据
  236. $postData = $data;
  237. $url = $config['apiUrl'] . $apiMap[$apiName]['url'];
  238. $form = '<form action="' . $url . '" method="post">';
  239. foreach ($postData as $k => $v) {
  240. $form .= "{$k} <p><input type='text' name='{$k}' value='{$v}'></p>";
  241. }
  242. $form .= '<input type="submit" value="提交"></form>';
  243. return ['form'=>$form,'postData'=>$postData];
  244. }
  245. function base64EncodeImage ($image_file) {
  246. //$base64_image = "";
  247. // $image_info = getimagesize($image_file);
  248. $image_data = file_get_contents($image_file);
  249. //$image_data = fread(fopen($image_file, "r"), filesize($image_file));
  250. // $base64_image = "data:" . $image_info["mime"] . ";base64," . chunk_split(base64_encode($image_data));
  251. // $base64_image = "data:" . $image_info["mime"] . ";base64," . base64_encode($image_data);
  252. return base64_encode($image_data);
  253. // return $base64_image;
  254. }
  255. // function base64EncodeImage ($image_file) {
  256. // //$file:图片地址
  257. // //Filetype: JPEG,PNG,GIF
  258. // $file = $image_file;
  259. // if($fp = fopen($file,"rb", 0))
  260. // {
  261. // $gambar = fread($fp,filesize($file));
  262. // fclose($fp);
  263. // // $base64 = chunk_split(base64_encode($gambar));
  264. // $base64 = base64_encode($gambar);
  265. // return $base64;
  266. // // 输出
  267. // // $encode = '<img src="data:image/jpg/png/gif;base64,' . $base64 .'" >';
  268. // // echo $encode;
  269. // }
  270. // }