Pu Zhibing
5 天以前 072d5dbd7642935056857268af4ab187035d7eec
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.stylefeng.guns.modular.system.util.zhenglian;
 
import com.stylefeng.guns.modular.system.util.SpringContextsUtil;
import com.zlpay.assist.sign.sm2.SM2Cert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.cert.X509Certificate;
import java.util.Base64;
 
/**
 * @author zhibing.pu
 * @Date 2025/7/28 14:22
 */
@Slf4j
public class ZhengLianUtil {
    
    private static ZhengLianConfig zhengLianConfig = SpringContextsUtil.getBean(ZhengLianConfig.class).getZhengLianConfig();
    
    /**
     * @Description: 从公钥证书获取公钥串
     * @return
     * @return String
     * @throws
     * @author: tsl
     * @date: 2019年4月19日 下午9:07:56
     */
    public static String getPublicKey() {
        String publicKey = "";
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(zhengLianConfig.getCer()));
            X509Certificate publicCert = SM2Cert.getPublicCert(inputStream);
            publicKey = Base64.getEncoder().encodeToString(publicCert.getEncoded());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return publicKey;
    }
    
    /**
     * @Description: 从私钥证书获取私钥传
     * @return
     * @return String
     * @throws
     * @author: tsl
     * @date: 2019年4月19日 下午9:08:00
     */
    public static String getPrivateKey() {
        String privateKey = "";
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(zhengLianConfig.getSm2()));
            byte[] priBytes = SM2Cert.getPrivateCert(inputStream);
            privateKey = SM2Cert.getPrivateKey(zhengLianConfig.getPassword(), priBytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return privateKey;
    }
    /**
     * @Description: 生成对称加密秘钥
     * @return String
     * @throws
     * @author: syuf
     * @date: 2018年11月8日 下午5:50:05
     */
    public static String generateKey(int length) {
        return RandomStringUtils.randomAlphanumeric(length);
    }
    /**
     * @Description: 签名
     * @param data
     * @return String
     * @throws
     * @author: syuf
     * @date: 2018年11月15日 下午2:19:01
     */
    public static String sign(String data) {
        try {
            // 获取私钥
            String privateKey = getPrivateKey();
            return com.zlpay.assist.sign.sm2.SM2Util.sign(privateKey, zhengLianConfig.getSignNo(), data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}