diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java b/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java index 9a381a6f..f74acce0 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java @@ -222,7 +222,7 @@ public class HighTestController { @ApiOperation(value = "测试") public ResponseData test3() { try { - return ResponseMsgUtil.success(HuiLianTongUnionCardConfig.getToken()); + return ResponseMsgUtil.success(HuiLianTongUnionCardConfig.queryBalance()); } catch (Exception e) { log.error("HighOrderController --> getOrderById() error!", e); return ResponseMsgUtil.exception(e); diff --git a/hai-service/src/main/java/com/hai/common/security/DESUtil.java b/hai-service/src/main/java/com/hai/common/security/DESUtil.java deleted file mode 100644 index 7d6df471..00000000 --- a/hai-service/src/main/java/com/hai/common/security/DESUtil.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.hai.common.security; - -import java.io.*; -import java.security.Key; -import java.security.SecureRandom; -import java.security.Security; - -import javax.crypto.*; -import javax.crypto.spec.DESKeySpec; -import javax.crypto.spec.IvParameterSpec; - -import java.util.Base64; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -public class DESUtil { - - /** - * 偏移变量,固定占8位字节 - */ - private final static String IV_PARAMETER = "12345678"; - /** - * 密钥算法 - */ - private static final String ALGORITHM = "DES"; - /** - * 加密/解密算法-工作模式-填充模式 - */ - private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding"; - /** - * 默认编码 - */ - private static final String CHARSET = "utf-8"; - - /** - * 生成key - * - * @param password - * @return - * @throws Exception - */ - private static Key generateKey(String password) throws Exception { - DESKeySpec dks = new DESKeySpec(password.getBytes(CHARSET)); - SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); - return keyFactory.generateSecret(dks); - } - - - /** - * DES加密字符串 - * - * @param password 加密密码,长度不能够小于8位 - * @param data 待加密字符串 - * @return 加密后内容 - */ - public static String encrypt(String password, String data) { - if (password== null || password.length() < 8) { - throw new RuntimeException("加密失败,key不能小于8位"); - } - if (data == null) - return null; - try { - Key secretKey = generateKey(password); - Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); - IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); - cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); - byte[] bytes = cipher.doFinal(data.getBytes(CHARSET)); - - //JDK1.8及以上可直接使用Base64,JDK1.7及以下可以使用BASE64Encoder - //Android平台可以使用android.util.Base64 - return new String(Base64.getEncoder().encode(bytes)); - - } catch (Exception e) { - e.printStackTrace(); - return data; - } - } - - /** - * DES解密字符串 - * - * @param password 解密密码,长度不能够小于8位 - * @param data 待解密字符串 - * @return 解密后内容 - */ - public static String decrypt(String password, String data) { - if (password== null || password.length() < 8) { - throw new RuntimeException("加密失败,key不能小于8位"); - } - if (data == null) - return null; - try { - Key secretKey = generateKey(password); - Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); - IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); - cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); - return new String(cipher.doFinal(Base64.getDecoder().decode(data.getBytes(CHARSET))), CHARSET); - } catch (Exception e) { - e.printStackTrace(); - return data; - } - } - - /** - * DES加密文件 - * - * @param srcFile 待加密的文件 - * @param destFile 加密后存放的文件路径 - * @return 加密后的文件路径 - */ -/* public static String encryptFile(String password, String srcFile, String destFile) { - - if (password== null || password.length() < 8) { - throw new RuntimeException("加密失败,key不能小于8位"); - } - try { - IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); - Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); - cipher.init(Cipher.ENCRYPT_MODE, generateKey(key), iv); - InputStream is = new FileInputStream(srcFile); - OutputStream out = new FileOutputStream(destFile); - CipherInputStream cis = new CipherInputStream(is, cipher); - byte[] buffer = new byte[1024]; - int r; - while ((r = cis.read(buffer)) > 0) { - out.write(buffer, 0, r); - } - cis.close(); - is.close(); - out.close(); - return destFile; - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - }*/ - - /** - * DES解密文件 - * - * @param srcFile 已加密的文件 - * @param destFile 解密后存放的文件路径 - * @return 解密后的文件路径 - */ -/* public static String decryptFile(String password, String srcFile, String destFile) { - if (password== null || password.length() < 8) { - throw new RuntimeException("加密失败,key不能小于8位"); - } - try { - File file = new File(destFile); - if (!file.exists()) { - file.getParentFile().mkdirs(); - file.createNewFile(); - } - IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); - Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); - cipher.init(Cipher.DECRYPT_MODE, generateKey(key), iv); - InputStream is = new FileInputStream(srcFile); - OutputStream out = new FileOutputStream(destFile); - CipherOutputStream cos = new CipherOutputStream(out, cipher); - byte[] buffer = new byte[1024]; - int r; - while ((r = is.read(buffer)) >= 0) { - cos.write(buffer, 0, r); - } - cos.close(); - is.close(); - out.close(); - return destFile; - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - }*/ -} diff --git a/hai-service/src/main/java/com/hai/common/security/DesUtil.java b/hai-service/src/main/java/com/hai/common/security/DesUtil.java new file mode 100644 index 00000000..a7662e44 --- /dev/null +++ b/hai-service/src/main/java/com/hai/common/security/DesUtil.java @@ -0,0 +1,277 @@ +package com.hai.common.security; + +import java.nio.charset.Charset; +import java.security.Key; +import java.security.SecureRandom; +import java.security.spec.AlgorithmParameterSpec; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; +import javax.crypto.spec.DESedeKeySpec; +import javax.crypto.spec.IvParameterSpec; + +import org.apache.commons.codec.binary.Base64; +//import org.apache.commons.lang3.RandomStringUtils; + + +import com.thoughtworks.xstream.core.util.Base64Encoder; + +public class DesUtil { + + public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding"; + + /** + * DES算法,加密 + * + * @param data + * 待加密字符串 + * @param key + * 加密私钥,长度不能够小于8位 + * @return 加密后的字节数组,一般结合Base64编码使用 + * @throws CryptException + * 异常 + */ + public static byte[] encode(String key, String data) throws Exception { + return encode(key, data.getBytes()); + } + + /** + * DES算法,加密 + * + * @param data + * 待加密字符串 + * @param key + * 加密私钥,长度不能够小于8位 + * @return 加密后的字节数组,一般结合Base64编码使用 + * @throws CryptException + * 异常 + */ + public static byte[] encode(String key, byte[] data) throws Exception { + try { + DESKeySpec dks = new DESKeySpec(key.getBytes()); + + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); + // key的长度不能够小于8位字节 + Key secretKey = keyFactory.generateSecret(dks); + Cipher cipher = Cipher.getInstance(ALGORITHM_DES); + IvParameterSpec iv = new IvParameterSpec(key.getBytes()); + AlgorithmParameterSpec paramSpec = iv; + cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); + + byte[] bytes = cipher.doFinal(data); + return bytes; + // return byte2HexStr(bytes); + // return byte2hex(new String(bytes)); + // return new String(new BASE64Encoder().encode(bytes)); + // return new String(bytes); + } catch (Exception e) { + throw new Exception(e); + } + } + + /** + * DES算法,解密 + * + * @param data + * 待解密字符串 + * @param key + * 解密私钥,长度不能够小于8位 + * @return 解密后的字节数组 + * @throws Exception + * 异常 + */ + public static byte[] decode(String key, byte[] data) throws Exception { + try { + // SecureRandom sr = new SecureRandom(); + DESKeySpec dks = new DESKeySpec(key.getBytes()); + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); + // key的长度不能够小于8位字节 + Key secretKey = keyFactory.generateSecret(dks); + Cipher cipher = Cipher.getInstance(ALGORITHM_DES); + IvParameterSpec iv = new IvParameterSpec(key.getBytes()); + AlgorithmParameterSpec paramSpec = iv; + cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); + return cipher.doFinal(data); + } catch (Exception e) { + throw new Exception(e); + } + } + + /** + * 获取编码后的值 + * + * @param key + * @param data + * @return + * @throws Exception + */ + public static String decode(String key, String data) { + byte[] datas; + String value = null; + try { + + datas = decode(key, new Base64Encoder().decode(data)); + + value = new String(datas); + } catch (Exception e) { + value = ""; + } + return value; + } + + public static String byte2HexStr(byte[] b) { + String hs = ""; + String stmp = ""; + for (int n = 0; n < b.length; n++) { + stmp = (Integer.toHexString(b[n] & 0XFF)); + if (stmp.length() == 1) + hs = hs + "0" + stmp; + else + hs = hs + stmp; + // if (n8800030132003900701123120457349857394500000188000301650001000402016-07-28 13:20:50150"; + // String s = HexUtil.byte2HexStr(DesUtil.encode(key, data)); + // System.err.println(s); + // System.err.println(DesUtil.encode(key, data.getBytes()).length); + // String s2 = new String(DesUtil.decode(key, HexUtil.hexStr2Bytes(s))); + // System.err.println(s2); + + // String s = encode("123", Charset.forName("GBK"), "d0fb65e5"); + // System.out.println(s); + // + // String e = decode("d0fb65e5", "258946d6143e30f9", + // Charset.forName("UTF-8")); + // System.out.println(e); + + /*String s = encode("中国", Charset.forName("UTF-8"),"12345678"); + System.out.println(s);*/ + String data = "T3xbPEKEXV9+CbBw8D1B+N2jk8xwa55s0Bde48c49YDwYfnUdBVz6Kj4HS2oCA1TTiqJkCUIYa5ckMhJeByBCAMsqu21LmFjb/hdW0y1Tt0Wk5PqmO8FAg=="; +/* { + "success": true, + "message": "ok", + "cards": [{ + "cardNo": "8800030115015107746" + }, { + "cardNo": "8800030115015119428" + }, { + "cardNo": "8800030128003170055" + }, { + "cardNo": "8800030132003656709" + }, { + "cardNo": "8800030132004014510" + }, { + "cardNo": "8800031104000000248" + }] + }*/ + + String a = decode("F8E91A3C", data,Charset.forName("UTF-8")); + System.out.println(a); + System.out.println("完成"); + } +} diff --git a/hai-service/src/main/java/com/hai/config/HuiLianTongUnionCardConfig.java b/hai-service/src/main/java/com/hai/config/HuiLianTongUnionCardConfig.java index b5940b62..9fb0bf81 100644 --- a/hai-service/src/main/java/com/hai/config/HuiLianTongUnionCardConfig.java +++ b/hai-service/src/main/java/com/hai/config/HuiLianTongUnionCardConfig.java @@ -2,16 +2,14 @@ package com.hai.config; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.hai.common.security.DESUtil; +import com.hai.common.pay.util.MD5Util; +import com.hai.common.security.DesUtil; import com.hai.common.utils.HttpsUtils; -import com.hai.common.utils.MD5Util; + import org.apache.commons.collections4.MapUtils; import java.nio.charset.Charset; -import java.util.Base64; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; +import java.util.*; /** * 汇联通会员卡业务接口 @@ -23,28 +21,80 @@ public class HuiLianTongUnionCardConfig { * @return * @throws Exception */ - public static JSONObject getToken() throws Exception { - Map map = new HashMap<>(); + public static JSONObject queryCardByMobile() throws Exception { + // 签名码 + String signCode = "F8E91A3C"; + + Map map = new LinkedHashMap<>(); map.put("accessCode", "6FCAE1470CEF465988351BB65ABAA8AE"); map.put("requestId", new Date().getTime()); map.put("method", "qgk/queryCardByMobile"); - String signCode = "F8E91A3C"; + // 业务数据 Map dataMap = new HashMap<>(); - dataMap.put("userMobile", "17726395120"); + dataMap.put("userMobile", "15185028745"); + + // 转换成json String dataJson = JSONObject.toJSONString(dataMap); + System.out.println(dataJson); + + // DES加密 + map.put("data", DesUtil.encode(dataJson, Charset.forName("UTF-8"),signCode)); + System.out.println(MapUtils.getString(map,"data")); + + // 数据签名字符串 + String str = (MapUtils.getString(map,"accessCode")+signCode); + str +=(MapUtils.getString(map,"requestId")+signCode) ; + str += (MapUtils.getString(map,"method") + signCode); + str += (MapUtils.getString(map,"data") + signCode); + System.out.println(str); + + // MD5加密 + map.put("sign", MD5Util.MD5Encode(str, "UTF-8").toUpperCase()); + System.out.println(MapUtils.getString(map, "sign")); + return HttpsUtils.doPost("http://hltgz.com:4010/api/v2/execute.json", JSON.toJSONString(map)); + // return null; + } + /** + * 获取token + * @return + * @throws Exception + */ + public static JSONObject queryBalance() throws Exception { + // 签名码 + String signCode = "F8E91A3C"; + + Map map = new LinkedHashMap<>(); + map.put("accessCode", "6FCAE1470CEF465988351BB65ABAA8AE"); + map.put("requestId", new Date().getTime()); + map.put("method", "qtk/queryBalance"); + + // 业务数据 + Map dataMap = new HashMap<>(); + dataMap.put("cardType", "ghk"); + dataMap.put("cardNo", "8800030115015107746"); + + // 转换成json + String dataJson = JSONObject.toJSONString(dataMap); System.out.println(dataJson); - map.put("data", DESUtil.encrypt(signCode,dataJson)); + // DES加密 + map.put("data", DesUtil.encode(dataJson, Charset.forName("UTF-8"),signCode)); System.out.println(MapUtils.getString(map,"data")); - String str = (MapUtils.getString(map,"accessCode") + signCode) + (MapUtils.getString(map,"requestId") + signCode) + (MapUtils.getString(map,"method") + signCode) + (MapUtils.getString(map,"data") + signCode); + // 数据签名字符串 + String str = (MapUtils.getString(map,"accessCode")+signCode); + str +=(MapUtils.getString(map,"requestId")+signCode) ; + str += (MapUtils.getString(map,"method") + signCode); + str += (MapUtils.getString(map,"data") + signCode); System.out.println(str); - map.put("sign", com.hai.common.pay.util.MD5Util.MD5Encode(str, "UTF-8").toUpperCase()); + + // MD5加密 + map.put("sign", MD5Util.MD5Encode(str, "UTF-8").toUpperCase()); System.out.println(MapUtils.getString(map, "sign")); return HttpsUtils.doPost("http://hltgz.com:4010/api/v2/execute.json", JSON.toJSONString(map)); - // return null; + // return null; }