Bladeren bron

华泰对接新建实体(加解密逻辑)

li.pengfei 2 jaren geleden
bovenliggende
commit
bb82463761

+ 16 - 15
src/main/java/com/ydtech/modules/order/components/huatai/HuaTaiRequestApiComponents.java

@@ -7,12 +7,17 @@ import com.ydtech.constants.InsuranceEnum;
 import com.ydtech.exception.SystemException;
 import com.ydtech.modules.order.entity.api.huatai.HuaTaiBaseRequest;
 import com.ydtech.modules.order.entity.api.huatai.HuaTaiBaseResponse;
+import com.ydtech.modules.order.entity.api.huatai.HuaTaiEncryptReq;
+import com.ydtech.modules.order.entity.api.huatai.HuaTaiEncryptRes;
 import com.ydtech.modules.order.entity.api.huatai.constants.HuaTaiTransCode;
 import com.ydtech.modules.order.entity.api.huatai.request.HuaTaiQuotedPriceRequest;
 import com.ydtech.modules.order.entity.api.huatai.response.HuaTaiQuotedPriceResponse;
 import com.ydtech.modules.order.entity.api.huatai.request.*;
 import com.ydtech.modules.order.entity.api.huatai.response.*;
+import com.ydtech.modules.order.utils.AESUtilsWithDataBase;
 import com.ydtech.modules.order.utils.InsuranceLog;
+import com.ydtech.utils.Base64Utils;
+import com.ydtech.utils.RSA.RSAUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.http.HttpEntity;
@@ -64,36 +69,34 @@ public class HuaTaiRequestApiComponents {
         String busReqString = JSON.toJSONString(q, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
         InsuranceLog.infoLog(InsuranceEnum.HTIC.getPinyin(), "\n\t---------> URl: {} \n---------> 请求参数:{}", url, busReqString);
         // ----加密starting------------
-       /* //1.先获取16位随机字符串 Key
+        //1.先获取16位随机字符串 Key
         String key=generateRadomStr(8);
         //2.使用Base64加密请求报文
-        String base64ReqJson=Base64Utils.getBase64Encode(busReqString);
+        String base64ReqJson= Base64Utils.getBase64Encode(busReqString);
         //3. 使用AES算法 秘钥Key  对 base64ReqJson加密
-         String busiContent=AESUtilsWithDataBase.encryptECBPK5Hex(base64ReqJson, key);
+         String busiContent= AESUtilsWithDataBase.encryptECBPK5Base64(base64ReqJson, key);
         //4.对Key 使用RSA算法进行加密
         String accessToken="";
         try{
-            accessToken=RSAUtils.rsaEncrypt(key,properties.getHuaTaiPublicKey(),"UTF-8");
+            accessToken= RSAUtils.rsaEncrypt(key, properties.getHuaTaiPublicKey(),"utf-8");
         }catch (Exception e){
             throw new SystemException("RSA加密获取accessToken失败!");
         }
         HuaTaiEncryptReq encryptReq=new HuaTaiEncryptReq();
         encryptReq.setInsuredCode(properties.getInsuredCode());
-        encryptReq.setSourceCode("016");
+        encryptReq.setSourceCode(properties.getSourceCode());
         encryptReq.setBusiContent(busiContent);
         encryptReq.setAccessToken(accessToken);
         String jsonString = JSON.toJSONString(encryptReq, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
         InsuranceLog.infoLog(InsuranceEnum.HTIC.getPinyin(), "\n\t---------> 加密后的参数:{}", jsonString);
-        HttpEntity<String> httpEntity = new HttpEntity<>(jsonString, httpHeaders);*/
+        HttpEntity<String> httpEntity = new HttpEntity<>(jsonString, httpHeaders);
         //----加密endding------------
-        //不加密
-        HttpEntity<String> httpEntity = new HttpEntity<>(busReqString, httpHeaders);
         ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class);
         String   body = response.getBody();
 
         InsuranceLog.infoLog(InsuranceEnum.HTIC.getPinyin(), "\n\t---------> 解密前的参数:{}", body);
         // ------解密starting---------
-        /*HuaTaiEncryptRes encryptRes=JSON.parseObject(body,HuaTaiEncryptRes.class);
+        HuaTaiEncryptRes encryptRes=JSON.parseObject(body,HuaTaiEncryptRes.class);
         //1.通过自己的私钥对key进行解密
         String resAccessToken="";
         try{
@@ -102,14 +105,12 @@ public class HuaTaiRequestApiComponents {
             throw new SystemException("RSA解密获取accessToken失败!");
         }
         //2.用key解密出出参报文
-        String aseDecryptStr=AESUtilsWithDataBase.decryptECBPK5Hex(encryptRes.getBusiContent(),resAccessToken);
+        String aseDecryptStr=AESUtilsWithDataBase.decryptECBPK5Base64(encryptRes.getBusiContent(),resAccessToken);
         //3.使用Base64解密最终返回报文
-        String text = Base64Utils.getBase64Encode(aseDecryptStr);
-        S t= JSON.parseObject(text, tClass);*/
+        String text = Base64Utils.getBase64Decode(aseDecryptStr);
         // ------解密ending---------
-        InsuranceLog.infoLog(InsuranceEnum.HTIC.getPinyin(), "\n\t---------> {}:{}", "响应参数", body);
-        //不加密
-        S t= JSON.parseObject(body, tClass);
+        InsuranceLog.infoLog(InsuranceEnum.HTIC.getPinyin(), "\n\t---------> {}:{}", "响应参数", text);
+        S t= JSON.parseObject(text, tClass);
         if (ObjectUtils.isEmpty(t)) {
             throw new SystemException("请求失败");
         }

+ 62 - 0
src/main/java/com/ydtech/modules/order/utils/AESUtilsWithDataBase.java

@@ -2,7 +2,10 @@ package com.ydtech.modules.order.utils;
 
 import com.alibaba.fastjson.JSON;
 import com.ydtech.constants.InsuranceEnum;
+import com.ydtech.utils.Base64Utils;
+import com.ydtech.utils.RSA.RSAUtils;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.binary.Base64;
 
 import javax.crypto.Cipher;
 import javax.crypto.spec.SecretKeySpec;
@@ -114,4 +117,63 @@ public class AESUtilsWithDataBase {
         return JSON.parseObject(text, c);
     }
 
+    /**
+     * AES 加密操作  ECBPK5-Base64
+     *
+     * @param content
+     * @return
+     */
+    public static String encryptECBPK5Base64(String content, String sessionKey) {
+        byte[] result = null;
+        if (content == null || "".equals(content)) {
+            return content;
+        }
+        try {
+            byte[] byteContent = content.getBytes(CODE_TYPE);
+            //使用加密秘钥
+            SecretKeySpec skeySpec = new SecretKeySpec(sessionKey.getBytes(CODE_TYPE), AES);
+            /*
+             * 新建一个密码编译器的实例,由三部分构成,用"/"分隔,分别代表如下
+             * 1. 加密的类型(如AES,DES,RC2等)
+             * 2. 模式(AES中包含ECB,CBC,CFB,CTR,CTS等)
+             * 3. 补码方式(包含nopadding/PKCS5Padding等等)
+             * 依据这三个参数可以创建很多种加密方式
+             */
+            synchronized (Cipher.class) {
+                Cipher cipher = Cipher.getInstance(ECB_PKCS5_PADDING);//"AES/ECB/PKCS5Padding";
+                cipher.init(Cipher.ENCRYPT_MODE, skeySpec);// 初始化为加密模式的密码器
+                result = cipher.doFinal(byteContent);// 加密
+            }
+        } catch (Exception ex) {
+            ex.printStackTrace();
+        }
+        return Base64.encodeBase64String(result);//通过base64转码返回
+    }
+
+    /**
+     * AES 解密操作  ECBPK5-Base64
+     *
+     * @param content
+     * @return
+     */
+    public static String decryptECBPK5Base64(String content, String sessionKey) {
+        String returnStr = content;
+        byte[] result;
+        if (content == null || "".equals(content)) {
+            return content;
+        }
+        try {
+            //使用加密秘钥
+            SecretKeySpec skeySpec = new SecretKeySpec(sessionKey.getBytes(CODE_TYPE), AES);
+            synchronized (Cipher.class) {
+                //实例化
+                Cipher cipher = Cipher.getInstance(ECB_PKCS5_PADDING);
+                cipher.init(Cipher.DECRYPT_MODE, skeySpec);// 初始化为加密模式的密码器
+                result = cipher.doFinal(Base64.decodeBase64(content));
+            }
+            returnStr = new String(result, CODE_TYPE);
+        } catch (Exception ex) {
+        }
+        return returnStr;
+    }
 }