dev-discount
= 3 years ago
commit 338ae27e4f
  1. 2
      hai-cweb/src/main/java/com/cweb/controller/HighTestController.java
  2. 173
      hai-service/src/main/java/com/hai/common/security/DESUtil.java
  3. 278
      hai-service/src/main/java/com/hai/common/security/DesUtil.java
  4. 117
      hai-service/src/main/java/com/hai/config/HuiLianTongUnionCardConfig.java

@ -222,7 +222,7 @@ public class HighTestController {
@ApiOperation(value = "测试") @ApiOperation(value = "测试")
public ResponseData test3() { public ResponseData test3() {
try { try {
return ResponseMsgUtil.success(HuiLianTongUnionCardConfig.getToken()); return ResponseMsgUtil.success(HuiLianTongUnionCardConfig.queryCardInfo());
} catch (Exception e) { } catch (Exception e) {
log.error("HighOrderController --> getOrderById() error!", e); log.error("HighOrderController --> getOrderById() error!", e);
return ResponseMsgUtil.exception(e); return ResponseMsgUtil.exception(e);

@ -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;
}*/
}

@ -0,0 +1,278 @@
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 (n<b.length-1) hs=hs+":";
}
return hs;
}
public static String triDesEncrypt(String input, String desKey, String desIv) {
Cipher cipher = null;
try {
SecureRandom sr = new SecureRandom();
DESedeKeySpec dks = new DESedeKeySpec(desKey.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance("DESede");
Key key = keyFactory.generateSecret(dks);
IvParameterSpec iv = new IvParameterSpec(desIv.getBytes("UTF-8"));
cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(1, key, iv, sr);
byte[] array = cipher.doFinal(input.getBytes("UTF-8"));
return Base64.encodeBase64String(array);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String triDesDecrypt(String input, String desKey, String desIv) {
Cipher cipher = null;
try {
SecureRandom sr = new SecureRandom();
DESedeKeySpec dks = new DESedeKeySpec(desKey.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(dks);
IvParameterSpec iv = new IvParameterSpec(desIv.getBytes("UTF-8"));
cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(2, key, iv, sr);
byte[] decoded = Base64.decodeBase64(input);
byte[] array = cipher.doFinal(decoded);
return new String(array, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] encode(String key, String data, Charset charset)
throws Exception {
return encode(key, data.getBytes(charset));
}
public static String decode(String key, String data, Charset charset) {
byte[] datas;
String value = null;
try {
datas = decode(key, new Base64Encoder().decode(data));
value = new String(datas, charset);
} catch (Exception e) {
value = "";
}
return value;
}
/**
* des加密返回string类型指定编码集
*
* @time 2018-11-6 16:08:27
* @author Lxm
* @param data
* @param charset
* @param key
* @return
* @throws Exception
*/
public static String encode(String data, Charset charset, String key)
throws Exception {
byte[] bt = encode(key, data.getBytes(charset));
// return new String(bt, charset);
String rs = new Base64Encoder().encode(bt);
return rs;
}
/* public static String quickDecode(String key, String data) throws Exception {
return quickDecode(key, data, "UTF-8");
}
public static String quickDecode(String key, String data, String charSet)
throws Exception {
return new String(DesUtil.decode(key, HexUtil.hexStr2Bytes(data)),
charSet);
}*/
public static void main(String[] args) throws Exception {
// String ke = RandomStringUtils
// .random(8,
// "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*");
// System.out.println(ke);
// String key = "abcdefgh";
// String data =
// "<?xml version=\"1.0\"?><RequestData><cardNo>8800030132003900701</cardNo><jourId>1231204573498573945</jourId><mercCode>000001</mercCode><terminalId>8800030165000100040</terminalId><terminalTime>2016-07-28 13:20:50</terminalTime><tranAmt>150</tranAmt></RequestData>";
// 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==";
String data = "T3xbPEKEXV9+CbBw8D1B+N2jk8xwa55s0Bde48c49YBr4/b4yBwN2FIVZZn+Xg9KQTDoTCLu3YtByaWh7zPmdcpBr9FGARduhPrwSnYTFJ0VVVSK/UzPWdHN2YYd4yHGQRJ2HEr/1egt2JUHpWr0JA==";
/* {
"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("完成");
}
}

@ -2,16 +2,14 @@ package com.hai.config;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; 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.HttpsUtils;
import com.hai.common.utils.MD5Util;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.MapUtils;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Base64; import java.util.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/** /**
* 汇联通会员卡业务接口 * 汇联通会员卡业务接口
@ -23,29 +21,122 @@ public class HuiLianTongUnionCardConfig {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static JSONObject getToken() throws Exception { public static JSONObject queryCardByMobile() throws Exception {
Map<String,Object> map = new HashMap<>(); // 签名码
String signCode = "F8E91A3C";
Map<String,Object> map = new LinkedHashMap<>();
map.put("accessCode", "6FCAE1470CEF465988351BB65ABAA8AE"); map.put("accessCode", "6FCAE1470CEF465988351BB65ABAA8AE");
map.put("requestId", new Date().getTime()); map.put("requestId", new Date().getTime());
map.put("method", "qgk/queryCardByMobile"); map.put("method", "qgk/queryCardByMobile");
// 业务数据
Map<String,Object> dataMap = new HashMap<>();
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));
}
/**
* 获取token
* @return
* @throws Exception
*/
public static JSONObject queryBalance() throws Exception {
// 签名码
String signCode = "F8E91A3C"; String signCode = "F8E91A3C";
Map<String,Object> map = new LinkedHashMap<>();
map.put("accessCode", "6FCAE1470CEF465988351BB65ABAA8AE");
map.put("requestId", new Date().getTime());
map.put("method", "qtk/queryBalance");
// 业务数据
Map<String,Object> dataMap = new HashMap<>(); Map<String,Object> dataMap = new HashMap<>();
dataMap.put("userMobile", "17726395120"); dataMap.put("cardType", "ghk");
dataMap.put("cardNo", "8800030115015107746");
// 转换成json
String dataJson = JSONObject.toJSONString(dataMap); 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 queryCardInfo() throws Exception {
// 签名码
String signCode = "F8E91A3C";
Map<String,Object> map = new LinkedHashMap<>();
map.put("accessCode", "6FCAE1470CEF465988351BB65ABAA8AE");
map.put("requestId", new Date().getTime());
map.put("method", "qgk/queryCardInfo");
// 业务数据
Map<String,Object> dataMap = new HashMap<>();
dataMap.put("cardNo", "8800030115015107746");
// 转换成json
String dataJson = JSONObject.toJSONString(dataMap);
System.out.println(dataJson); 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")); 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); 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")); System.out.println(MapUtils.getString(map, "sign"));
return HttpsUtils.doPost("http://hltgz.com:4010/api/v2/execute.json", JSON.toJSONString(map)); return HttpsUtils.doPost("http://hltgz.com:4010/api/v2/execute.json", JSON.toJSONString(map));
// return null; // return null;
} }
} }

Loading…
Cancel
Save