Yspay.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. namespace Shop\Src;
  3. use Dever;
  4. use Main\Lib\Core;
  5. class Yspay
  6. {
  7. public function t1()
  8. {
  9. $content = '0c62d6b7f1177314c9ee257915b20298fbf63e53e23a11ab728569370a1037132156a34e0c14d4201f08064e35be918c1e8b8dd2611bca1b6f1251acb399e2a84b644ab4def69d92
  10. 1b781970c0bea960ddbb3169d58ef8c7094a35d03ed110b7d75d5a9881aae448ea95f3355c28941fda2e43b3efbc21918faffefe4392a04b293ec8d6d709f5802aca91cdf7a597d837bcb8ad7464cba2434d65ffb6c63475405381cc56e83842c8ae462ab70653e40ea482b2980f683b9f263018eca
  11. bfdc6069eaeb89ec0c4295f5230c13820a578f13ef0086d15a720ea2712ff2040f3ae09a8d3b8f125ff0c174da1b9b90abe18445c283c138126cbf1749097bcac6e197cc872983cd8cb32636e00b335325c60dd4480b7da012e8b1d5dfba885dad768f3f74597a1743a02fa21baddc177a00b4fb9cc
  12. 0fac04c11ce8404d92fced77eeb1808028e9bf24ff586f455cf0093829b592518797254d43357332eeca7bbf0b141f313fc72e503bd20dc2bfb1de2e6a22786c62e33b78a3308c3464089b51bdbc97368ed8a5c51d10a2bfeb1d88015abf9e042c72b74d167738359d278100f6df644d564174c1d92
  13. 051e206c4a80bbdf93ae102c02781089d1d526f8f6391590f9f4c8128ceb6a402c046746e8f1e341b72bbb5f3a818aa15726bd2930a190fef9d5b625f03020103c00526aa0e2d678f1601d20553d601dac686106c311586218bd446e23f55e19e6f5c322765ec9b88747a080cb8b9460dd91db4d47c
  14. 7b9a8e4873f4d319b56f0855a8209d3e2cb7edd84a043446dbc05dcf0fd2186d10653750b6db0e0f2c09dc583b3321021efffad62e6cfe5df4cca03429de98aed68bc15c054b99c08ad6c5a0a9ef1a8ae03caf052a7beda023aba98db8a796f889b180597aa899681f54a4b89498525ba24ce69d2d2
  15. 565175ab789410007fed227715c60f71304fd5dab209310f10a34ce1484dd76e4547fd2ff21f2a286e7f5c0b60a15274c1aaae47180a4b67dd7bedda2a04d6eb1cbec3331572bb1b311677189a337e1dd680f29ac3671ad4fea70c3d341999ba25acabecb03822c68d7f3a9bd7b09a4ce809f48f119
  16. 6cf39a10221c930b87bc43987b18bef819bf08e72a45b2969a54b27693f5d70260bb9adb93345454107c8c194e71c8612fdc13a663ff13184cb53833c21320cd0e66c61d54499b2b2ec3c188cb87bc2b61b50cda9d43b7d7bec1bef549c8dfa12e43eb9fb1004c1f43b988ac2e38d603df559fc467c
  17. 11398886f730494e7696645c99321fe4cb844b41267930da4c7b4622b6f260dc72b2eac1d7c4f6cfd41e6fd79cd50d7e4bfd6832e312e632847e8690288e4c51cddacea2521377bcab13209031442627d3ae55e5ff9c9ba2c19fd84814ec5ea221eed4d03c4703fa9d39f4c8c53202b858fbc4691c2
  18. 16fff191633c098221f22d21';
  19. $class = new Chinaums();
  20. $content = $class->privateDecrypt($content);
  21. print_r($content);die;
  22. }
  23. public function query()
  24. {
  25. # 查看当前余额
  26. $sid = Dever::input('sid');
  27. $cash = $this->query_action($sid);
  28. return $cash;
  29. }
  30. public function query_action($sid)
  31. {
  32. $class = new Chinaums();
  33. $data = $class->query($sid);
  34. return $data;
  35. }
  36. public function record()
  37. {
  38. # 查看提现记录
  39. $sid = Dever::input('sid');
  40. $start = Dever::input('start');
  41. $end = Dever::input('end');
  42. $log = $this->record_action($sid, $start, $end);
  43. return $log;
  44. }
  45. public function record_action($sid, $start, $end)
  46. {
  47. $class = new Chinaums();
  48. $log = $class->record($sid, $start, $end);
  49. return $log;
  50. }
  51. public function act()
  52. {
  53. $order = Dever::order();
  54. $result = $this->action('89810007372107C', $order, -1, 2, 127);
  55. return $result;
  56. }
  57. public function action($mid, $order, $order_id, $type, $cash, $order_info = array())
  58. {
  59. $class = new Chinaums();
  60. $notify = Dever::url('yspay.notify?order=' . $order, 'shop');
  61. $result = $class->act($mid, $order, $order_id, $type, $cash, $notify, $order_info);
  62. return $result;
  63. }
  64. public function notify()
  65. {
  66. $msg = Dever::input();
  67. Dever::log($msg, 'yspay');
  68. $order = $msg['order'];
  69. if (!$order) {
  70. echo 'FAILED';die;
  71. }
  72. if (strstr($order, 'SD')) {
  73. $log = Dever::db('shop/yspay_tixian_log')->one(array('order_num' => $order));
  74. if ($log) {
  75. Dever::db('shop/yspay_tixian_log')->update(array('where_id' => $log['id'], 'status' => 2, 'response' => Dever::json_encode($content)));
  76. $account = Dever::db('shop/yspay_account')->one(array('shop_id' => $log['shop_id']));
  77. if ($account) {
  78. Dever::db('shop/yspay_account')->dec(array('where_id' => $account['id'], 'set_cash' => $log['cash']));
  79. }
  80. }
  81. echo 'SUCCESS';die;
  82. }
  83. $where['order_num'] = $order;
  84. $order = Dever::db('shop/sell_order')->find($where);
  85. $class = new Chinaums();
  86. if ($order && $order['withdraw'] == 1) {
  87. $content = $msg['content'];
  88. $content = $class->privateDecrypt($content);
  89. $log = Dever::db('shop/yspay_tixian_log')->one(array('order_num' => $order, 'shop_id' => $order['shop_id']));
  90. if ($content && isset($content['responseCode']) && ($content['responseCode'] == '000000' || $content['responseCode'] == '000001')) {
  91. Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'withdraw' => 2));
  92. if ($log) {
  93. Dever::db('shop/yspay_tixian_log')->update(array('where_id' => $log['id'], 'status' => 2, 'response' => Dever::json_encode($content)));
  94. $account = Dever::db('shop/yspay_account')->one(array('shop_id' => $log['shop_id']));
  95. if ($account) {
  96. Dever::db('shop/yspay_account')->dec(array('where_id' => $account['id'], 'set_cash' => $log['cash']));
  97. }
  98. }
  99. echo 'SUCCESS';die;
  100. } else {
  101. Dever::load('shop/lib/cron')->yspayOne($order);
  102. }
  103. }
  104. echo 'FAILED';die;
  105. }
  106. }
  107. /**
  108. * 银联商务代付
  109. * Class Chinaums
  110. */
  111. class Chinaums
  112. {
  113. //测试地址
  114. protected $test_host = 'https://mobl-test.chinaums.com/';
  115. //正式地址
  116. protected $host = 'https://managefree.chinaums.com/';
  117. //系统编号(sysId)
  118. protected $sys_id = '10119';
  119. //商户号
  120. protected $mch_id = '89810007372106N';
  121. //外部平台私钥
  122. protected $private_key = 'MIIEowIBAAKCAQEApzpOtvWGclmdw+/aEzAxwyTgUwnC9qjQEhL+qfMWDBIbFR8N4q6GQ8fRKZ9Zyvm0voFhgpH44Y6sh2U7rF9HQOuw2puywvU8kk5JAUxpxykbH7QqW/ijH1gBG3Ck/Ip/5I4co+q9K/CjQRmId5nSJ8ffnaiAOogNZycrP+AgEEU8vL1eNtnagVxT3EC1LEh0wBwecT9Tkyi0tMD0sMeIUl9POrnh7YADJtUzGf+TTNrqK6DhoT9mE3Plw0pqxKSk4/tHNx1ZIXf4I/UeYSEAreXHWVfLQ/if1C9HRYhEGB57mmqn0JdmsSRD7DX7eU7Oja+RqtWfQ/KH+2fnjq9uSwIDAQABAoIBABACUvrM8nwL/EB/u77OhFUMOLV6X8HBU3oHe33Fmby6FwGkoW2sC4p9nJaBfNUjppv+Qtl/I/Zj9DxaAqZz0Jx0c8/Zy/lCBPR0riSDrjS0yLX2R3/ag7NCd4iq9gB/OdMlj7RZipakSNy4lwnsHeRVDCFJukzgLXhMlaVdzXQBJTdERP3G8A9vwxuvbOE3q3kHCTWepVYd3KdEQ5rDPrYh/HwfVB2tWSne7Z/NurutW5qLA6aiJGcDoF7H41Fit8zw1QDnwJNvsUeR/POFFD0zog7+ZpMX3OiJwFKBZfTNvzJtH3mkqnN62JJwpJNqTTMWdfSziFlQNJ6GrCqhaxECgYEA1STJz0GAC7tho/l6O13BYlnyjS15bSCrr23XXUY3Sa7s6JiNFAUkIFq57XzEEdZhqOmgQlJQmGKJmmRVSshnSH7HohRdjisnjAbK1O0qABe3tP3IIuKCrPY+q1zb+PpYkFMiWUr3+1JYKU8o5tQCWQhRLVPN8Gbl6K0ffr3sJzcCgYEAyNoS4oWNeKnAJyK7rkDq7iMkf4eDMEUl1VPunZOTsZDkycf3bIKJJK+KW/HNpCt5M+DdhgZPtbuvpPVZFsscbM/q2q4vIBxp2riS94o+Aoijc8U5FzkhE+a5+qTJpykMBOVXaHENVvZzeh9TysoRROiI3c4IibKsP2NdwxnRU40CgYACozjoGqxl+MSljdHIaEmbWPeabaSiCBR30Eko+1R10fdWjYS5abLfv5+rY67ddaSxvWphR7l9mUKQqYswqLg2enMn+06JEtmjbvYVmnW5mJGKaNWQMPNjBo0R+80xEJAGATFcn4PgFU3H/RXJfXgl9+dhQzc6MVvI/FN2cw04EwKBgQCp3uaB9vpDxy3g65tYMUeAyPAhe/cUaCcrWaVCWIJdd3JIxnsYpv5erKiUQtnts2b63X/9znCz/1z6YxdKE9aioOa8tPzeZ6mPdvO1sJHpiq7TR0aVwZecQVOiDKUQX8LukRlwF810c1QPQVeY93XLle/LyGmoLBlKMzwlujw0MQKBgDZT/egpnQM3x4TR9ERrk0o5GOLHNFiNkSfkzzZ3D2nRGT+6GRlmb40LtPzBy5+vAgLqBlfEEfToaMGZ+Vrl2CePBBoR/j/q8nlziqZ3H5b8plqHSbX85zONT6tF+YZ2nRMxpI2gQtecRzoozm+ilELCNSd6V7pAzpl14KRU2V5l';
  123. //外部平台PKCS1私钥(银联商务提供的PKCS8格式需转为PKCS1格式)
  124. protected $private_key_pkcs1 = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCnOk629YZyWZ3D79oTMDHDJOBTCcL2qNASEv6p8xYMEhsVHw3iroZDx9Epn1nK+bS+gWGCkfjhjqyHZTusX0dA67Dam7LC9TySTkkBTGnHKRsftCpb+KMfWAEbcKT8in/kjhyj6r0r8KNBGYh3mdInx9+dqIA6iA1nJys/4CAQRTy8vV422dqBXFPcQLUsSHTAHB5xP1OTKLS0wPSwx4hSX086ueHtgAMm1TMZ/5NM2uoroOGhP2YTc+XDSmrEpKTj+0c3HVkhd/gj9R5hIQCt5cdZV8tD+J/UL0dFiEQYHnuaaqfQl2axJEPsNft5Ts6Nr5Gq1Z9D8of7Z+eOr25LAgMBAAECggEAEAJS+szyfAv8QH+7vs6EVQw4tXpfwcFTegd7fcWZvLoXAaShbawLin2cloF81SOmm/5C2X8j9mP0PFoCpnPQnHRzz9nL+UIE9HSuJIOuNLTItfZHf9qDs0J3iKr2AH850yWPtFmKlqRI3LiXCewd5FUMIUm6TOAteEyVpV3NdAElN0RE/cbwD2/DG69s4TereQcJNZ6lVh3cp0RDmsM+tiH8fB9UHa1ZKd7tn826u61bmosDpqIkZwOgXsfjUWK3zPDVAOfAk2+xR5H884UUPTOiDv5mkxfc6InAUoFl9M2/Mm0feaSqc3rYknCkk2pNMxZ19LOIWVA0noasKqFrEQKBgQDVJMnPQYALu2Gj+Xo7XcFiWfKNLXltIKuvbdddRjdJruzomI0UBSQgWrntfMQR1mGo6aBCUlCYYomaZFVKyGdIfseiFF2OKyeMBsrU7SoAF7e0/cgi4oKs9j6rXNv4+liQUyJZSvf7UlgpTyjm1AJZCFEtU83wZuXorR9+vewnNwKBgQDI2hLihY14qcAnIruuQOruIyR/h4MwRSXVU+6dk5OxkOTJx/dsgokkr4pb8c2kK3kz4N2GBk+1u6+k9VkWyxxsz+rari8gHGnauJL3ij4CiKNzxTkXOSET5rn6pMmnKQwE5VdocQ1W9nN6H1PKyhFE6IjdzgiJsqw/Y13DGdFTjQKBgAKjOOgarGX4xKWN0choSZtY95ptpKIIFHfQSSj7VHXR91aNhLlpst+/n6tjrt11pLG9amFHuX2ZQpCpizCouDZ6cyf7TokS2aNu9hWadbmYkYpo1ZAw82MGjRH7zTEQkAYBMVyfg+AVTcf9Fcl9eCX352FDNzoxW8j8U3ZzDTgTAoGBAKne5oH2+kPHLeDrm1gxR4DI8CF79xRoJytZpUJYgl13ckjGexim/l6sqJRC2e2zZvrdf/3OcLP/XPpjF0oT1qKg5ry0/N5nqY9287WwkemKrtNHRpXBl5xBU6IMpRBfwu6RGXAXzXRzVA9BV5j3dcuV78vIaagsGUozPCW6PDQxAoGANlP96CmdAzfHhNH0RGuTSjkY4sc0WI2RJ+TPNncPadEZP7oZGWZvjQu0/MHLn68CAuoGV8QR9OhowZn5WuXYJ48EGhH+P+ryeXOKpncflvymWodJtfznM41Pq0X5hnadEzGkjaBC15xHOijOb6KUQsI1J3pXukDOmXXgpFTZXmU=';
  125. //银商系统公钥
  126. protected $public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhTzN8wxdXYr+/+t8iX0mlol8wFy+0pGtxnyB6PjyrIRjGHrZJA7grZPvy7MOK6PqihqTZh6SO7tX9StxVmTQo+PuibAY/p2i08UxkIBUcucHVvHN8gkM3GBFBqD0uJgUc03cerEI7gNeaKYfCVs0/hMo6CKbdXVKqaE987kXYK209uFcte+Y8YofQ2AYamImpzSf7pXomAu+8D98HIOs5bKITHTk30vPdNhrQdpDRO1TuX0x8bwhCykcwAEdB/Z9YGXkLU5S3QJY2qvCZJY08YdNqIpbDqifSRvOg0sqeKFXPfI2M0GsOh6PArTEmh0QHxDht9xjPPt11aVgh8kgkQIDAQAB';
  127. private function host()
  128. {
  129. return $this->host;
  130. }
  131. # 查询余额
  132. public function query($mid)
  133. {
  134. $url = $this->host() . 'uisouterfront/qrywithdrawbalance/process';
  135. $mid = $mid ? $mid : $this->mch_id;
  136. //整理内容信息
  137. $content = [
  138. 'sysId' => $this->sys_id,
  139. 'mchntNo' => $mid,
  140. 'timestamp' => date('YmdHis'),
  141. ];
  142. //设置签名并加密
  143. $body['content'] = $this->createSignature($content);
  144. $result = Dever::curl($url, $body, 'post');
  145. $decryptData = $this->privateDecrypt($result);
  146. if (isset($decryptData['tzWithdrawAmtPublic'])) {
  147. return $decryptData['tzWithdrawAmtPublic'];
  148. } elseif (isset($decryptData['t0WithdrawAmtPublic'])) {
  149. return $decryptData['t0WithdrawAmtPublic'];
  150. }
  151. return 0;
  152. }
  153. # 查询提现记录
  154. public function record($mid, $start, $end, $page = '1')
  155. {
  156. $url = $this->host() . 'uisouterfront/withdraw/qryWithDrawLog';
  157. $mid = $mid ? $mid : $this->mch_id;
  158. //整理内容信息
  159. $content = [
  160. 'sysId' => $this->sys_id,
  161. 'mchntNo' => $mid,
  162. 'timestamp' => date('YmdHis'),
  163. 'page' => (string) $page,
  164. 'beginDate' => $start,
  165. 'endDate' => $end,
  166. ];
  167. //设置签名并加密
  168. $body['content'] = $this->createSignature($content);
  169. $result = Dever::curl($url, $body, 'post');
  170. $result = Dever::json_decode($result);
  171. $decryptData = $this->privateDecrypt($result['result']);
  172. if (isset($decryptData['recordList']) && $decryptData['recordList']) {
  173. $data = Dever::json_decode($decryptData['recordList']);
  174. if ($page == 1 && isset($decryptData['totalPage']) && $decryptData['totalPage'] > 1) {
  175. for ($i = 2; $i <= $decryptData['totalPage']; $i++) {
  176. $temp = $this->record($mid, $start, $end, $i);
  177. if ($temp) {
  178. $data = array_merge($data, $temp);
  179. }
  180. }
  181. }
  182. return $data;
  183. }
  184. return array();
  185. }
  186. # 提现
  187. public function act($mid, $order, $order_id, $type = 2, $cash = 1, $notify = '', $order_info = array())
  188. {
  189. $url = $this->host() . 'uisouterfront/withdrawback/process';
  190. $mid = $mid ? $mid : $this->mch_id;
  191. $type = (string) $type;
  192. $cash = (string) $cash;
  193. //整理内容信息
  194. $content = [
  195. 'sysId' => $this->sys_id,
  196. 'mchntNo' => $mid,
  197. 'timestamp' => date('YmdHis'),
  198. 'sysOrderId' => $order,
  199. 'responseUrl' => $notify,
  200. 'withdrawType' => $type,
  201. 'withdrawAmt' => $cash,
  202. ];
  203. Dever::log($content, 'yspay_act');
  204. $data['shop_id'] = $order_info['shop_id'];
  205. $data['mid'] = $mid;
  206. $data['order_num'] = $order;
  207. $data['type'] = $type;
  208. $data['cash'] = $cash;
  209. $data['tdate'] = time();
  210. $data['status'] = 1;
  211. $data['desc'] = $order_info['desc'];
  212. $data['mtype'] = $order_info['mtype'];
  213. Dever::db('shop/yspay_tixian_log')->insert($data);
  214. //设置签名并加密
  215. $body['content'] = $this->createSignature($content);
  216. $result = Dever::curl($url, $body, 'post');
  217. $decryptData = $this->privateDecrypt($result);
  218. if (isset($decryptData['responseCode']) && ($decryptData['responseCode'] == '000000' || $decryptData['responseCode'] == '000001')) {
  219. return 'ok';
  220. } else {
  221. if ($decryptData['responseDesc'] == '订单号已存在' && $order_id > 0) {
  222. Dever::db('shop/sell_order')->update(array('where_id' => $order_id, 'withdraw' => 2));
  223. }
  224. return $decryptData['responseDesc'];
  225. }
  226. }
  227. /**
  228. * 私钥解密
  229. * @Author Abnermouke <abnermouke@gmail.com>
  230. * @Originate in Abnermouke's MBP
  231. * @Time 2021-08-26 16:36:58
  232. * @param $encryptData
  233. * @return string
  234. */
  235. public function privateDecrypt($encryptData)
  236. {
  237. $content = pack('H*', $encryptData);
  238. $result = '';
  239. foreach (str_split($content, 256) as $block) {
  240. openssl_private_decrypt($block, $dataDecrypt, $this->formatRsaKey($this->private_key_pkcs1), OPENSSL_PKCS1_PADDING);
  241. $result .= $dataDecrypt;
  242. }
  243. $result = Dever::json_decode($result);
  244. return $result;
  245. }
  246. /**
  247. * 创建签名
  248. * @Author Abnermouke <abnermouke@gmail.com>
  249. * @Originate in Abnermouke's MBP
  250. * @Time 2021-08-26 16:34:49
  251. * @param $body
  252. * @return false|string
  253. */
  254. private function createSignature($body)
  255. {
  256. //重新排序信息
  257. ksort($body);
  258. //整理加密字符串
  259. $signature_string = '';
  260. //循环数据
  261. foreach ($body as $key => $value) {
  262. //整合字符串
  263. $signature_string .= $key.'='.$value.'&';
  264. }
  265. //整理加密字符串
  266. $signature_string = rtrim($signature_string, '&');
  267. //echo $signature_string;
  268. //获取加密数组
  269. $array_json = $this->rsaEncrypt($signature_string);
  270. //设置签名
  271. $body['sign'] = bin2hex($array_json);
  272. //print_r($body);
  273. //json序列化
  274. $json_data = json_encode($body);
  275. //加密RSA
  276. $json_rsa = $this->publicRsa($json_data);
  277. //返回结果
  278. return $json_rsa;
  279. }
  280. /**
  281. * 私钥加密
  282. * @Author Abnermouke <abnermouke@gmail.com>
  283. * @Originate in Abnermouke's MBP
  284. * @Time 2021-08-26 16:35:08
  285. * @param string $str
  286. * @return false
  287. */
  288. private function rsaEncrypt($str=''){
  289. $charSet = mb_detect_encoding($str, ["UTF-8", "GB2312", "GBK"]);
  290. $str = mb_convert_encoding($str, "UTF-8", $charSet);
  291. $pri_key = $this->private_key;
  292. $pri_key = $this->formatRsaKey($pri_key, 'RSA PRIVATE');
  293. $pi_key = openssl_pkey_get_private($pri_key);
  294. if(!$pi_key) return false;
  295. $algo = "SHA256";
  296. openssl_sign($str, $binary_signature, $pi_key, $algo);
  297. return $binary_signature;
  298. }
  299. /**
  300. * 公钥加密
  301. * @Author Abnermouke <abnermouke@gmail.com>
  302. * @Originate in Abnermouke's MBP
  303. * @Time 2021-08-26 16:35:14
  304. * @param string $str
  305. * @return false|string
  306. */
  307. private function publicRsa($str='')
  308. {
  309. $charSet = mb_detect_encoding($str, ["UTF-8", "GB2312", "GBK"]);
  310. $str = mb_convert_encoding($str, "UTF-8", $charSet);
  311. $pub_key = $this->public_key;
  312. $pub_key = $this->formatRsaKey($pub_key, 'PUBLIC');
  313. $pu_key = openssl_pkey_get_public($pub_key);
  314. if(!$pu_key) return false;
  315. $result = '';
  316. foreach (str_split($str,245) as $chunk) {
  317. if(openssl_public_encrypt($chunk,$encryptData, $pub_key)){
  318. $result .= $encryptData;
  319. }
  320. }
  321. $result = bin2hex($result);
  322. openssl_free_key($pu_key);
  323. return $result;
  324. }
  325. /**
  326. * 格式化密钥
  327. * @Author Abnermouke <abnermouke@gmail.com>
  328. * @Originate in Abnermouke's MBP
  329. * @Time 2021-08-26 16:36:36
  330. * @param $key
  331. * @param string $alias
  332. * @param int $length
  333. * @return string
  334. */
  335. private function formatRsaKey($key, $alias = 'RSA PRIVATE', $length = 64)
  336. {
  337. //拆分格式
  338. $key = chunk_split($key, (int)$length, "\n");
  339. //生成pem
  340. $pem = "-----BEGIN $alias KEY-----\n".$key."-----END $alias KEY-----";
  341. //返回pem
  342. return $pem;
  343. }
  344. }