parent
42bc1db51d
commit
f5955cea14
File diff suppressed because one or more lines are too long
@ -0,0 +1,126 @@ |
||||
package com.hai.common.utils; |
||||
|
||||
import javax.crypto.Cipher; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.util.Calendar; |
||||
|
||||
/** |
||||
* @className: AESTool |
||||
* @author: HuRui |
||||
* @date: 2022/9/27 |
||||
**/ |
||||
public class AESTool { |
||||
|
||||
public static String Encrypt(String sSrc, String sKey) |
||||
{ |
||||
try |
||||
{ |
||||
return toHex(encryptBytes(sSrc.getBytes("utf-8"), sKey)); |
||||
} catch (Exception ex) { |
||||
throw new RuntimeException(ex.toString()); |
||||
} |
||||
} |
||||
|
||||
public static byte[] encrypt(byte[] sSrc, String sKey) { |
||||
try { |
||||
return encryptBytes(sSrc, sKey); |
||||
} catch (Exception ex) { |
||||
throw new RuntimeException(ex.toString()); |
||||
} |
||||
} |
||||
|
||||
public static byte[] encryptBytes(byte[] sSrc, String sKey) throws Exception |
||||
{ |
||||
if (sKey == null) { |
||||
throw new Exception("Key为空null"); |
||||
} |
||||
|
||||
if (sKey.length() != 16) { |
||||
throw new Exception("Key长度不是16位"); |
||||
} |
||||
byte[] raw = sKey.getBytes("utf-8"); |
||||
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); |
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); |
||||
cipher.init(1, skeySpec); |
||||
byte[] encrypted = cipher.doFinal(sSrc); |
||||
return encrypted; |
||||
} |
||||
|
||||
public static String Decrypt(String sSrc, String sKey) |
||||
{ |
||||
try { |
||||
return new String(decryptBytes(toBytes(sSrc), sKey), "utf-8"); |
||||
} catch (Exception ex) { |
||||
throw new RuntimeException(ex.toString()); |
||||
} |
||||
} |
||||
|
||||
public static byte[] decrypt(byte[] sSrc, String sKey) { |
||||
try { |
||||
return decryptBytes(sSrc, sKey); |
||||
} catch (Exception ex) { |
||||
throw new RuntimeException(ex.toString()); |
||||
} |
||||
} |
||||
|
||||
|
||||
public static byte[] decryptBytes(byte[] sSrc, String sKey) |
||||
throws Exception |
||||
{ |
||||
if (sKey == null) { |
||||
throw new Exception("Key为空null"); |
||||
} |
||||
|
||||
if (sKey.length() != 16) { |
||||
throw new Exception("Key长度不是16位"); |
||||
} |
||||
byte[] raw = sKey.getBytes("utf-8"); |
||||
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); |
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); |
||||
cipher.init(2, skeySpec); |
||||
byte[] original = cipher.doFinal(sSrc); |
||||
return original; |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
public static String toHex(byte[] buf) |
||||
{ |
||||
if (buf == null) |
||||
return ""; |
||||
StringBuffer result = new StringBuffer(2 * buf.length); |
||||
for (int i = 0; i < buf.length; i++) { |
||||
appendHex(result, buf[i]); |
||||
} |
||||
return result.toString(); |
||||
} |
||||
|
||||
private static void appendHex(StringBuffer sb, byte b) { |
||||
sb.append("0123456789ABCDEF".charAt(b >> 4 & 0xF)).append("0123456789ABCDEF".charAt(b & 0xF)); |
||||
} |
||||
|
||||
|
||||
|
||||
public static byte[] toBytes(String hexString) |
||||
{ |
||||
int len = hexString.length() / 2; |
||||
byte[] result = new byte[len]; |
||||
for (int i = 0; i < len; i++) |
||||
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), |
||||
16).byteValue(); |
||||
return result; |
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
cal.add(5, 10); |
||||
String str = "1,13439676580," + cal.getTimeInMillis(); |
||||
String key = "0123456789ABCDEF"; |
||||
String token = Encrypt(str, key); |
||||
System.out.println(token); |
||||
System.out.println(Decrypt(token, key)); |
||||
} |
||||
} |
@ -0,0 +1,134 @@ |
||||
package com.hai.config; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hai.common.exception.ErrorCode; |
||||
import com.hai.common.exception.ErrorHelp; |
||||
import com.hai.common.exception.SysCode; |
||||
import com.hai.common.utils.AESTool; |
||||
import com.hai.common.utils.HttpsUtils; |
||||
import com.hai.common.utils.MD5Util; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.*; |
||||
|
||||
/** |
||||
* 重庆中石油 |
||||
* @className: CqPetroChina |
||||
* @author: HuRui |
||||
* @date: 2022/9/26 |
||||
**/ |
||||
public class ChongQingCNPCService { |
||||
|
||||
private static Logger log = LoggerFactory.getLogger(ChongQingCNPCService.class); |
||||
|
||||
/** |
||||
* 发放电子券 |
||||
* @param requestCode |
||||
* @param tradeId |
||||
* @param ticketSum |
||||
* @param phone |
||||
*/ |
||||
public static JSONObject sendCNPCTicket(String requestCode, String tradeId, Integer ticketSum, String phone) throws Exception { |
||||
Map<String, Object> map = new HashMap<>(); |
||||
map.put("requestCode", requestCode); |
||||
map.put("tradeId", tradeId); |
||||
map.put("ticketSum", ticketSum); |
||||
map.put("phone", phone); |
||||
return request("sendCNPCTicket", map); |
||||
} |
||||
|
||||
/** |
||||
* 获取中石油跨界券核销码 |
||||
* @param couponNo |
||||
* @param tradeId |
||||
* @return |
||||
*/ |
||||
public static JSONObject getCNPCCheckCode(String couponNo, String tradeId) throws Exception { |
||||
Map<String, Object> map = new HashMap<>(); |
||||
map.put("couponNo", couponNo); |
||||
map.put("tradeId", tradeId); |
||||
return request("getCNPCCheckCode", map); |
||||
} |
||||
|
||||
/** |
||||
* 获取电子券详情 |
||||
* @param couponNo |
||||
* @return |
||||
*/ |
||||
public static JSONObject queryCoupon(String couponNo) throws Exception { |
||||
Map<String, Object> map = new HashMap<>(); |
||||
map.put("couponNo", couponNo); |
||||
map.put("random", String.valueOf(new Random().nextInt(899999) + 100000)); |
||||
return request("queryCoupon", map); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 请求 |
||||
* @param actionType 接口类型 |
||||
* @param param 参数 |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static JSONObject request(String actionType, Map<String,Object> param) throws Exception { |
||||
String verifyCode = AESTool.Encrypt(JSONObject.toJSONString(param), CommonSysConst.getSysConfig().getChongQingCnpcMerKey()); |
||||
|
||||
JSONObject header = new JSONObject(); |
||||
header.put("strVendorCode", CommonSysConst.getSysConfig().getChongQingCnpcMerNo()); |
||||
header.put("strActionType", actionType); |
||||
header.put("verifyCode", MD5Util.encode((verifyCode + CommonSysConst.getSysConfig().getChongQingCnpcMerKey()).getBytes()).toLowerCase()); |
||||
|
||||
JSONObject msg = new JSONObject(); |
||||
msg.put("head", header); |
||||
msg.put("body", verifyCode); |
||||
|
||||
Map<String, Object> reqParam = new HashMap<>(); |
||||
reqParam.put("sendMessage", msg); |
||||
|
||||
log.info("============start============"); |
||||
log.info("请求接口:" + actionType); |
||||
log.info("参数:" + JSONObject.toJSONString(param)); |
||||
log.info("请求参数:" + JSONObject.toJSONString(reqParam)); |
||||
JSONObject response = HttpsUtils.doPost(CommonSysConst.getSysConfig().getChongQingCnpcUrl(), reqParam, new HashMap<>()); |
||||
log.info("响应参数:" + response.toJSONString()); |
||||
log.info("============end============"); |
||||
|
||||
JSONObject postMessage = response.getJSONObject("postMessage"); |
||||
if (postMessage == null) { |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请求失败!"); |
||||
} |
||||
// 解密body
|
||||
JSONObject body = decryptBody(response.getJSONObject("postMessage").getString("body")); |
||||
if (!body.getInteger("status").equals(1)) { |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, body.getString("info")); |
||||
} |
||||
return body; |
||||
} |
||||
|
||||
/** |
||||
* 解密body |
||||
* @param bodyStr body加密字符串 |
||||
* @return |
||||
*/ |
||||
public static JSONObject decryptBody(String bodyStr) { |
||||
// 解密body
|
||||
String decryptBody = AESTool.Decrypt(bodyStr, CommonSysConst.getSysConfig().getChongQingCnpcMerKey()); |
||||
return JSONObject.parseObject(decryptBody, JSONObject.class); |
||||
} |
||||
|
||||
/** |
||||
* 解密电子券码 |
||||
* @param couponCode 券码加密字符串 |
||||
* @return 核销码 |
||||
*/ |
||||
public static String decryptCouponCode(String couponCode) { |
||||
return AESTool.Decrypt(couponCode, CommonSysConst.getSysConfig().getChongQingCnpcCouponSignKey()); |
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
//System.out.println(AESTool.Decrypt("A9D356D4614F874586EAF8678C8C3E7D8CD0EEF031ADA44DDAA3B342CE4BBE44F115AABE27324A6A1F049619139A1A889FCA0FB48EB6E1EA4B1B50F041961B2D686FE8B9696C02DF95BA99B342747D67ECC7847646C87993CF924F33C1308829","n2j30jxhl3rhuoci"));
|
||||
System.out.println(AESTool.Decrypt("12AEA963896A18F78D0AD9A1103BD0C07E5F6ED88477C49F59192296FC59A07CB046D702CFC72522258A3097138B88F6FBDBEA1130F0D2D72F8C7351A6BA6A17DEAF3605450202D746BFCD6B35373A7BCF10101966AB79E118BDE0155B982299A76614DDCB885158A18C1AE672DE3CB912AEA963896A18F78D0AD9A1103BD0C07E5F6ED88477C49F59192296FC59A07CB046D702CFC72522258A3097138B88F6FBDBEA1130F0D2D72F8C7351A6BA6A17DEAF3605450202D746BFCD6B35373A7BCF10101966AB79E118BDE0155B982299A76614DDCB885158A18C1AE672DE3CB9","n2j30jxhl3rhuoci")); |
||||
//System.out.println(AESTool.Decrypt("3549AD565800370205265D7518D26E5F40C90A61FDEB5DD73966126A5D25F5E9","5ojldakiz343a6yk"));
|
||||
} |
||||
} |
Loading…
Reference in new issue