|
@@ -0,0 +1,637 @@
|
|
|
+package com.qunzhixinxi.hnqz.admin.util;
|
|
|
+
|
|
|
+import cn.hutool.crypto.asymmetric.KeyType;
|
|
|
+import cn.hutool.crypto.asymmetric.RSA;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.DataOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.KeyFactory;
|
|
|
+import java.security.PrivateKey;
|
|
|
+import java.security.PublicKey;
|
|
|
+import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
+import java.security.spec.X509EncodedKeySpec;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.SortedMap;
|
|
|
+import java.util.TreeMap;
|
|
|
+
|
|
|
+public class SaasApiDemoV2 {
|
|
|
+
|
|
|
+ public static String version = "1";
|
|
|
+
|
|
|
+ public static String merId = "7000010115";
|
|
|
+
|
|
|
+ public static String tenantCode = "0505";
|
|
|
+
|
|
|
+ public static String levyCode = "1000046215";
|
|
|
+
|
|
|
+ public static String bankCode = "6000030915";
|
|
|
+
|
|
|
+ public static String localhost ="http://8.130.66.247:20000/carybusiness";
|
|
|
+
|
|
|
+ // 商户私钥
|
|
|
+ public static String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALJMzIKlbgil/c1l4N48zJsD+gMdAhtvhwSN9e+RSudM7d3SSFTnBahEkystVl39XN1yfa5eVxbJX2lRzKd8OOxop4vi6eI0xsCSPCcezLI0M13GnQ/C1z9ZXSz0P3998PH3C3EaYjHiG6gzA37SF0jOkalaML/y9aHXI6iwQbx3AgMBAAECgYB5MoMBe3nkHxD6gXntFt7tsDJN03YPOCxLCERYaCbGOyClW78XF0JdMt/mxZajpQlu7dhIopAISTG14Yh/gI0f4uOqaoIBTrDzwb9pxYqgHF6nf/EqW9WV66PyCfZi5Yz58V4Iq93i/qOWCDDjJ+WXwaYazyOeRMWWNsmDjnvZgQJBAOCP0YXdrkKsi8j4SY9nYmCONE5ckkuKMFMDKzxy+S2TZ25EjavVYWRVMr0zA3r9V3J/fgqeO7DXPMPpY7co2RECQQDLQvsgc/b7TpQGyBYUDFhEvtyQrsQNOAn71ggqi3fVNGvbXgkYf0JLTh72qNtfgpQ+9Z4NT7uhYBiNFI7lvv0HAkBAsS3rIrHRA2RBwUVvypjMuCrZaFG88VJ6l68c+wJN3uktGxK1LgOgo+q6Frgp9lH66j+lT3BiwZ7/0Xyoh1wBAkB/gUH1JqmdFbrH0kX/R4srfKrx25/CRxAEOUwgHCMMzHiIQKHmCxZ0FMpZoq0SaeBC6mK+KPEjlxtvjKDjP/6LAkApF71n67aigGM1Vkcw+1xeIIQoMVDdBiaYSXljxRQlXiIQkJSNZq/UCN1zHSJq6RRK51U+coTXyz813tSVv2KE";
|
|
|
+
|
|
|
+ // 运营公钥
|
|
|
+ public static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbfWSbWP0tXtPSxgXX4kcdG5G8hPFdrTMEu4xVDDJYRAQ8W2wHl6Ts1Q+JYXaVbvgEqbqrRY6ezWL2TJiA7iobb8dFI53ED+eSVHJg1Sc5HO/P/n9tlMWpR+t2QsuB9BRc6k7u9M9w9yeIZ0meq/jjjVt1Ut9ZV/hpifO+BVeO0QIDAQAB";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * soho签约
|
|
|
+ **/
|
|
|
+
|
|
|
+
|
|
|
+// public static void main(String[] args) throws Exception {
|
|
|
+// // sohoSign();
|
|
|
+//// sohoQuery();
|
|
|
+// //sohoIdCardUpload();
|
|
|
+// //rescindSoho();
|
|
|
+// //downloadContract();
|
|
|
+// //batchSoho();
|
|
|
+// //accountBalance();
|
|
|
+// //calculation();
|
|
|
+// //accTransDetail();
|
|
|
+// //levyInfo();
|
|
|
+// //merchantInfo();
|
|
|
+// //accNoInfo();
|
|
|
+// //openMerchant();
|
|
|
+// //selectMerchant();
|
|
|
+// //supplementInfo();
|
|
|
+// //selectProjectMcc();
|
|
|
+// //createProject();
|
|
|
+// //accReviewRecord();
|
|
|
+// //payment();
|
|
|
+// //queryBatch();
|
|
|
+// //downloadReceipt();
|
|
|
+// //downloadReceiptByDate();
|
|
|
+// //queryMerplatOrderPage();
|
|
|
+// //invoiceType();
|
|
|
+// //invoiceMerSumAmt();
|
|
|
+// //invoiceOpen();
|
|
|
+// //invoicePic();
|
|
|
+// //invoiceInfo();
|
|
|
+// //openApiHelp();
|
|
|
+// }
|
|
|
+
|
|
|
+ public static String ALGORITHM_DES = "DES";
|
|
|
+ public static String PKCS5 = "DES/ECB/PKCS5Padding";
|
|
|
+ public static String SIGN_ALGORITHMS = "SHA1WithRSA";// 摘要加密算饭
|
|
|
+ public static String CHAR_SET = "UTF-8";
|
|
|
+
|
|
|
+
|
|
|
+ public static boolean verify(String content, String sign, String public_key)
|
|
|
+ throws Exception {
|
|
|
+ KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
|
|
+ byte[] encodedKey = decode(public_key);
|
|
|
+ PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(
|
|
|
+ encodedKey));
|
|
|
+
|
|
|
+ java.security.Signature signature = java.security.Signature
|
|
|
+ .getInstance(SIGN_ALGORITHMS);
|
|
|
+
|
|
|
+ signature.initVerify(pubKey);
|
|
|
+ signature.update(content.getBytes(CHAR_SET));
|
|
|
+
|
|
|
+ boolean bverify = signature.verify(decode(sign));
|
|
|
+ return bverify;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String createSign(SortedMap<String, String> parameters) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ Set es = parameters.entrySet();
|
|
|
+ Iterator it = es.iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ Map.Entry entry = (Map.Entry) it.next();
|
|
|
+ String k = (String) entry.getKey();
|
|
|
+ Object v = entry.getValue();
|
|
|
+ if ("" != v && "null" != v && null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k) && !"null".equals(v)) {
|
|
|
+ sb.append(k + "=" + v + "&");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // signature 不用添加到SortMap里面去,单独处理,编码方式采用UTF-8
|
|
|
+ return sb.toString().substring(0, sb.length() - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String sendPost(String curl, String param) {
|
|
|
+ String result = "";// 返回的结果
|
|
|
+ BufferedReader in = null;// 读取响应输入流
|
|
|
+ try {
|
|
|
+ //创建连接
|
|
|
+ URL url = new URL(curl);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url
|
|
|
+ .openConnection();
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
|
+ connection.setRequestProperty("Content-Type",
|
|
|
+ "application/json");
|
|
|
+ connection.setRequestProperty("token", "Bearer gfdsgfsgfdsgsrkljnfak[po[eghpaj");
|
|
|
+
|
|
|
+ connection.connect();
|
|
|
+
|
|
|
+ //POST请求
|
|
|
+ DataOutputStream out = new DataOutputStream(
|
|
|
+ connection.getOutputStream());
|
|
|
+
|
|
|
+ out.writeBytes(param);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ //读取响应
|
|
|
+ // 定义BufferedReader输入流来读取URL的响应,并设置编码方式
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection
|
|
|
+ .getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ String line;
|
|
|
+ // 读取返回的内容
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ result += line;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ System.out.println("Http请求方法内部问题");
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return
|
|
|
+ * @Author cuiLei
|
|
|
+ * @Description 将Map以key=value&格式拼接
|
|
|
+ * @Date 14:47 2021/4/7
|
|
|
+ **/
|
|
|
+ public static String createLinkString(Map<String, Object> params, boolean encode) {
|
|
|
+ List<String> keys = new ArrayList<String>(params.keySet());
|
|
|
+ Collections.sort(keys);//不按首字母排序, 需要按首字母排序请打开
|
|
|
+ StringBuilder prestrSB = new StringBuilder();
|
|
|
+ for (int i = 0; i < keys.size(); i++) {
|
|
|
+ String key = keys.get(i);
|
|
|
+ Object value = params.get(key);
|
|
|
+ if (StringUtils.isEmpty(value) || "null".equals(value) || "sign".equals(value) || "key".equals(value)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (encode) {
|
|
|
+ try {
|
|
|
+ value = URLEncoder.encode(value.toString(), "GBK");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
|
|
|
+ prestrSB.append(key).append("=").append(value);
|
|
|
+ } else {
|
|
|
+ prestrSB.append(key).append("=").append(value).append("&");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return prestrSB.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String sign(String content, String privateKey)
|
|
|
+ throws Exception {
|
|
|
+ byte[] str = decode(privateKey);
|
|
|
+ PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(str);
|
|
|
+ KeyFactory keyf = KeyFactory.getInstance("RSA");
|
|
|
+ PrivateKey priKey = keyf.generatePrivate(priPKCS8);
|
|
|
+
|
|
|
+ java.security.Signature signature = java.security.Signature
|
|
|
+ .getInstance(SIGN_ALGORITHMS);
|
|
|
+
|
|
|
+ signature.initSign(priKey);
|
|
|
+ signature.update(content.getBytes(CHAR_SET));
|
|
|
+
|
|
|
+ byte[] signed = signature.sign();
|
|
|
+
|
|
|
+ return encode(signed);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ static private final int BASELENGTH = 128;
|
|
|
+ static private final int LOOKUPLENGTH = 64;
|
|
|
+ static private final int TWENTYFOURBITGROUP = 24;
|
|
|
+ static private final int EIGHTBIT = 8;
|
|
|
+ static private final int SIXTEENBIT = 16;
|
|
|
+ static private final int FOURBYTE = 4;
|
|
|
+ static private final int SIGN = -128;
|
|
|
+ static private final char PAD = '=';
|
|
|
+ static private final boolean fDebug = false;
|
|
|
+ static final private byte[] base64Alphabet = new byte[BASELENGTH];
|
|
|
+ static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH];
|
|
|
+
|
|
|
+ static {
|
|
|
+ for (int i = 0; i < BASELENGTH; ++i) {
|
|
|
+ base64Alphabet[i] = -1;
|
|
|
+ }
|
|
|
+ for (int i = 'Z'; i >= 'A'; i--) {
|
|
|
+ base64Alphabet[i] = (byte) (i - 'A');
|
|
|
+ }
|
|
|
+ for (int i = 'z'; i >= 'a'; i--) {
|
|
|
+ base64Alphabet[i] = (byte) (i - 'a' + 26);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = '9'; i >= '0'; i--) {
|
|
|
+ base64Alphabet[i] = (byte) (i - '0' + 52);
|
|
|
+ }
|
|
|
+
|
|
|
+ base64Alphabet['+'] = 62;
|
|
|
+ base64Alphabet['/'] = 63;
|
|
|
+
|
|
|
+ for (int i = 0; i <= 25; i++) {
|
|
|
+ lookUpBase64Alphabet[i] = (char) ('A' + i);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 26, j = 0; i <= 51; i++, j++) {
|
|
|
+ lookUpBase64Alphabet[i] = (char) ('a' + j);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 52, j = 0; i <= 61; i++, j++) {
|
|
|
+ lookUpBase64Alphabet[i] = (char) ('0' + j);
|
|
|
+ }
|
|
|
+ lookUpBase64Alphabet[62] = (char) '+';
|
|
|
+ lookUpBase64Alphabet[63] = (char) '/';
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isWhiteSpace(char octect) {
|
|
|
+ return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isPad(char octect) {
|
|
|
+ return (octect == PAD);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isData(char octect) {
|
|
|
+ return (octect < BASELENGTH && base64Alphabet[octect] != -1);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * remove WhiteSpace from MIME containing encoded Base64 data.
|
|
|
+ *
|
|
|
+ * @param data the byte array of base64 data (with WS)
|
|
|
+ * @return the new length
|
|
|
+ */
|
|
|
+ private static int removeWhiteSpace(char[] data) {
|
|
|
+ if (data == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // count characters that's not whitespace
|
|
|
+ int newSize = 0;
|
|
|
+ int len = data.length;
|
|
|
+ for (int i = 0; i < len; i++) {
|
|
|
+ if (!isWhiteSpace(data[i])) {
|
|
|
+ data[newSize++] = data[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return newSize;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String encode(byte[] binaryData) {
|
|
|
+
|
|
|
+ if (binaryData == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ int lengthDataBits = binaryData.length * EIGHTBIT;
|
|
|
+ if (lengthDataBits == 0) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
|
|
|
+ int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
|
|
|
+ int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets;
|
|
|
+ char encodedData[] = null;
|
|
|
+
|
|
|
+ encodedData = new char[numberQuartet * 4];
|
|
|
+
|
|
|
+ byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
|
|
|
+
|
|
|
+ int encodedIndex = 0;
|
|
|
+ int dataIndex = 0;
|
|
|
+ if (fDebug) {
|
|
|
+ System.out.println("number of triplets = " + numberTriplets);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < numberTriplets; i++) {
|
|
|
+ b1 = binaryData[dataIndex++];
|
|
|
+ b2 = binaryData[dataIndex++];
|
|
|
+ b3 = binaryData[dataIndex++];
|
|
|
+
|
|
|
+ if (fDebug) {
|
|
|
+ System.out.println("b1= " + b1 + ", b2= " + b2 + ", b3= " + b3);
|
|
|
+ }
|
|
|
+
|
|
|
+ l = (byte) (b2 & 0x0f);
|
|
|
+ k = (byte) (b1 & 0x03);
|
|
|
+
|
|
|
+ byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
|
|
|
+ byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
|
|
|
+ byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);
|
|
|
+
|
|
|
+ if (fDebug) {
|
|
|
+ System.out.println("val2 = " + val2);
|
|
|
+ System.out.println("k4 = " + (k << 4));
|
|
|
+ System.out.println("vak = " + (val2 | (k << 4)));
|
|
|
+ }
|
|
|
+
|
|
|
+ encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
|
|
|
+ encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
|
|
|
+ encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3];
|
|
|
+ encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f];
|
|
|
+ }
|
|
|
+
|
|
|
+ // form integral number of 6-bit groups
|
|
|
+ if (fewerThan24bits == EIGHTBIT) {
|
|
|
+ b1 = binaryData[dataIndex];
|
|
|
+ k = (byte) (b1 & 0x03);
|
|
|
+ if (fDebug) {
|
|
|
+ System.out.println("b1=" + b1);
|
|
|
+ System.out.println("b1<<2 = " + (b1 >> 2));
|
|
|
+ }
|
|
|
+ byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
|
|
|
+ encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
|
|
|
+ encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4];
|
|
|
+ encodedData[encodedIndex++] = PAD;
|
|
|
+ encodedData[encodedIndex++] = PAD;
|
|
|
+ } else if (fewerThan24bits == SIXTEENBIT) {
|
|
|
+ b1 = binaryData[dataIndex];
|
|
|
+ b2 = binaryData[dataIndex + 1];
|
|
|
+ l = (byte) (b2 & 0x0f);
|
|
|
+ k = (byte) (b1 & 0x03);
|
|
|
+
|
|
|
+ byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
|
|
|
+ byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
|
|
|
+
|
|
|
+ encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
|
|
|
+ encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
|
|
|
+ encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2];
|
|
|
+ encodedData[encodedIndex++] = PAD;
|
|
|
+ }
|
|
|
+
|
|
|
+ return new String(encodedData);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Decodes Base64 data into octects
|
|
|
+ *
|
|
|
+ * @param encoded string containing Base64 data
|
|
|
+ * @return Array containind decoded data.
|
|
|
+ */
|
|
|
+ public static byte[] decode(String encoded) {
|
|
|
+
|
|
|
+ if (encoded == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ char[] base64Data = encoded.toCharArray();
|
|
|
+ // remove white spaces
|
|
|
+ int len = removeWhiteSpace(base64Data);
|
|
|
+
|
|
|
+ if (len % FOURBYTE != 0) {
|
|
|
+ return null;//should be divisible by four
|
|
|
+ }
|
|
|
+
|
|
|
+ int numberQuadruple = (len / FOURBYTE);
|
|
|
+
|
|
|
+ if (numberQuadruple == 0) {
|
|
|
+ return new byte[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ byte decodedData[] = null;
|
|
|
+ byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
|
|
|
+ char d1 = 0, d2 = 0, d3 = 0, d4 = 0;
|
|
|
+
|
|
|
+ int i = 0;
|
|
|
+ int encodedIndex = 0;
|
|
|
+ int dataIndex = 0;
|
|
|
+ decodedData = new byte[(numberQuadruple) * 3];
|
|
|
+
|
|
|
+ for (; i < numberQuadruple - 1; i++) {
|
|
|
+
|
|
|
+ if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))
|
|
|
+ || !isData((d3 = base64Data[dataIndex++]))
|
|
|
+ || !isData((d4 = base64Data[dataIndex++]))) {
|
|
|
+ return null;
|
|
|
+ }//if found "no data" just return null
|
|
|
+
|
|
|
+ b1 = base64Alphabet[d1];
|
|
|
+ b2 = base64Alphabet[d2];
|
|
|
+ b3 = base64Alphabet[d3];
|
|
|
+ b4 = base64Alphabet[d4];
|
|
|
+
|
|
|
+ decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
|
|
|
+ decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
|
|
|
+ decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))) {
|
|
|
+ return null;//if found "no data" just return null
|
|
|
+ }
|
|
|
+
|
|
|
+ b1 = base64Alphabet[d1];
|
|
|
+ b2 = base64Alphabet[d2];
|
|
|
+
|
|
|
+ d3 = base64Data[dataIndex++];
|
|
|
+ d4 = base64Data[dataIndex++];
|
|
|
+ if (!isData((d3)) || !isData((d4))) {//Check if they are PAD characters
|
|
|
+ if (isPad(d3) && isPad(d4)) {
|
|
|
+ if ((b2 & 0xf) != 0)//last 4 bits should be zero
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ byte[] tmp = new byte[i * 3 + 1];
|
|
|
+ System.arraycopy(decodedData, 0, tmp, 0, i * 3);
|
|
|
+ tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
|
|
|
+ return tmp;
|
|
|
+ } else if (!isPad(d3) && isPad(d4)) {
|
|
|
+ b3 = base64Alphabet[d3];
|
|
|
+ if ((b3 & 0x3) != 0)//last 2 bits should be zero
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ byte[] tmp = new byte[i * 3 + 2];
|
|
|
+ System.arraycopy(decodedData, 0, tmp, 0, i * 3);
|
|
|
+ tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
|
|
|
+ tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
|
|
|
+ return tmp;
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else { //No PAD e.g 3cQl
|
|
|
+ b3 = base64Alphabet[d3];
|
|
|
+ b4 = base64Alphabet[d4];
|
|
|
+ decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
|
|
|
+ decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
|
|
|
+ decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return decodedData;
|
|
|
+ }
|
|
|
+
|
|
|
+ private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
|
|
|
+ "a", "b", "c", "d", "e", "f"};
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ static String formatDate(Date date, String format) {
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ String format1 = simpleDateFormat.format(date);
|
|
|
+ return format1;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String RSAPriKeyEncry(String priKey, String openText) {
|
|
|
+ RSA rsa = new RSA(priKey, null);
|
|
|
+ byte[] encrypt = rsa.encrypt(openText, KeyType.PrivateKey);
|
|
|
+ String ciphertext = encode(encrypt);
|
|
|
+ return ciphertext;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String sendPostHaveHead(String curl, String param, String sign) {
|
|
|
+ String result = "";// 返回的结果
|
|
|
+ BufferedReader in = null;// 读取响应输入流
|
|
|
+ try {
|
|
|
+ //创建连接
|
|
|
+ URL url = new URL(curl);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url
|
|
|
+ .openConnection();
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
|
+ connection.setRequestProperty("Content-Type",
|
|
|
+ "application/json");
|
|
|
+ connection.setRequestProperty("sign", sign);
|
|
|
+
|
|
|
+ connection.connect();
|
|
|
+
|
|
|
+ //POST请求
|
|
|
+ DataOutputStream out = new DataOutputStream(
|
|
|
+ connection.getOutputStream());
|
|
|
+
|
|
|
+ out.writeBytes(param);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ //读取响应
|
|
|
+ // 定义BufferedReader输入流来读取URL的响应,并设置编码方式
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection
|
|
|
+ .getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ String line;
|
|
|
+ // 读取返回的内容
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ result += line;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ System.out.println("Http请求方法内部问题");
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //解密验签工具
|
|
|
+ public static <T> T unPackMessage(JSONObject job, Class<T> clazz, String publicKey) throws Exception {
|
|
|
+ //验签
|
|
|
+ String sign = job.get("sign").toString();
|
|
|
+ job.put("sign",null);
|
|
|
+ String signString = v2SortKey(job);
|
|
|
+ System.out.println("验签串:"+signString);
|
|
|
+ if(!StringUtils.isEmpty(sign)){
|
|
|
+ boolean verify = verify(signString, sign, publicKey);
|
|
|
+ System.out.println("验签结果:"+verify);
|
|
|
+ if (!verify) {
|
|
|
+ throw new RuntimeException("验签失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //解密
|
|
|
+ String strData = job.get("data").toString();
|
|
|
+ if (job.getBoolean("encryptedState")) {
|
|
|
+ String s = RSAPubKDeCode(publicKey, strData);
|
|
|
+ return JSONObject.parseObject(s, clazz);
|
|
|
+ } else {
|
|
|
+ return JSONObject.parseObject(strData, clazz);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //解密验签工具
|
|
|
+ public static <T> T unPackMessage(JSONObject job, Class<T> clazz) throws Exception {
|
|
|
+ return unPackMessage(job, clazz, publicKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对象转map
|
|
|
+ *
|
|
|
+ * @param object
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<String, Object> objectToMap(Object object) {
|
|
|
+ Map<String, Object> dataMap = new HashMap<>();
|
|
|
+ Class<?> clazz = object.getClass();
|
|
|
+ for (Field field : clazz.getDeclaredFields()) {
|
|
|
+ try {
|
|
|
+ field.setAccessible(true);
|
|
|
+ dataMap.put(field.getName(), field.get(object));
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dataMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String v2SortKey(Map<String, Object> parameters) {
|
|
|
+ SortedMap<String, String> sortMap = new TreeMap<String, String>();
|
|
|
+ Set<String> keySet = parameters.keySet();
|
|
|
+ for (String key : keySet) {
|
|
|
+ sortMap.put(key, String.valueOf(parameters.get(key)));
|
|
|
+ }
|
|
|
+ return createSign(sortMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * RSA公钥解密
|
|
|
+ */
|
|
|
+ public static String RSAPubKDeCode(String pubKey, String cipherText) {
|
|
|
+ RSA rsa = new RSA(null, pubKey);
|
|
|
+ byte[] decode = cn.hutool.core.codec.Base64.decode(cipherText);
|
|
|
+ byte[] decrypt = rsa.decrypt(decode, KeyType.PublicKey);
|
|
|
+ String openText = new String(decrypt);
|
|
|
+ return openText;
|
|
|
+ }
|
|
|
+}
|