java110
2023-05-23 4b47d63f3a39c845eebd44a8e2be7d48173f073f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.java110.job.adapt.returnMoney.bbg;
 
import com.java110.core.factory.CommunitySettingFactory;
import com.java110.job.adapt.returnMoney.bbg.lib.GmUtil;
import com.java110.job.adapt.returnMoney.bbg.lib.HttpRequestUtil;
import com.java110.job.adapt.returnMoney.bbg.lib.JsonUtil;
 
import java.util.HashMap;
import java.util.Map;
 
public class EncryptDecryptFactory {
 
 
    public static String execute(String communityId, String url, Map<String, Object> params) {
        String decrypt = "";
        try {
            String mchtNo_SM4 = CommunitySettingFactory.getValue(communityId, "mchtNo_SM4");
            String publicKey_SM4 = CommunitySettingFactory.getValue(communityId, "publicKey_SM4");
            // 格式为json
            String json = JsonUtil.mapToJson(params);
            System.out.println("加密前:" + json);
            // 报文加密
            String secretKey = GmUtil.generateSm4Key();
            String encrypt = GmUtil.encryptSm4(json, secretKey);
            System.out.println("加密后:" + encrypt);
 
            Map<String, Object> signParams = new HashMap<>();
            signParams.put("mcht_no", mchtNo_SM4);// 收款商户编号
            signParams.put("sign_type", "SM4");
            signParams.put("message_key", GmUtil.encryptSm2(secretKey, publicKey_SM4));// 密钥加密
            signParams.put("enc_data", encrypt);// 加密后请求参数
 
            String requestParams = JsonUtil.mapToJson(signParams);
            System.out.println("最终请求参数:" + requestParams);
            System.err.println("");
            String returnResult = HttpRequestUtil.httpPost(url, requestParams);
            System.out.println("支付结果返回值(原文):" + returnResult);
            if (returnResult == null) {
                System.err.println("通道响应异常");
                throw new IllegalArgumentException("通道响应异常");
 
            }
            // 开始解密
            Map<String, Object> responseParams = JsonUtil.jsonToMap(returnResult);
            if (!responseParams.containsKey("enc_data")) {
                System.err.println("交易失败-->" + responseParams.get("return_code") + ":" + responseParams.get("return_message"));
                throw new IllegalArgumentException("交易失败-->" + responseParams.get("return_code") + ":" + responseParams.get("return_message"));
            }
            String decryptStr = (String) responseParams.get("enc_data");
 
            decrypt = GmUtil.decryptSm4(decryptStr, secretKey);
            if (decrypt == null) {
                System.err.println("解密失败");
                throw new IllegalArgumentException("解密失败");
            }
            System.out.println("支付结果返回值(解密后):" + decrypt);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException(e.getMessage());
        }
        return decrypt;
    }
}