吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package org.sxkj.common.utils.SM2;
 
import org.bouncycastle.crypto.InvalidCipherTextException;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Map;
public class Utils {
 
    /**
     * get key pair
     */
    public static Map<String, String> createKeyPair() throws NoSuchAlgorithmException {
        return SecretCommon.createKeyPair();
    }
 
    /**
     * encrypt
     *
     * @param plainText 需加密的明文字符串
     * @param publicKey 公钥
     */
    public static String encrypt(String plainText, String publicKey) throws IOException, InvalidCipherTextException {
        publicKey=ensurePrefix(publicKey);
        return encrypt(plainText, publicKey, ModeTypeConstant.BASE);
    }
 
    /**
     * encrypt
     *
     * @param plainText 需加密的明文字符串
     * @param publicKey 公钥
     * @param modeType  base:标准;bc:BC模式
     */
    public static String encrypt(String plainText, String publicKey, String modeType) throws IOException, InvalidCipherTextException {
        return SecretCommon.encrypt(plainText, publicKey, ModeTypeConstant.getMode(modeType));
    }
 
    public static String encrypts(byte[] plainText, String publicKey, String modeType) throws IOException, InvalidCipherTextException {
        return SecretCommon.encrypts(plainText, publicKey, ModeTypeConstant.getMode(modeType));
    }
    public static byte[] decrypts(byte[] plainText, String privateKey, String modeType) throws InvalidCipherTextException, UnsupportedEncodingException {
        return SecretCommon.decrypts(plainText, privateKey, ModeTypeConstant.getMode(modeType));
    }
 
    /**
     * decrypt
     *
     * @param cipherText 需加密的字符串
     * @param privateKey 私钥
     */
    public static byte[] decrypt(String cipherText, String privateKey) throws InvalidCipherTextException, UnsupportedEncodingException {
        cipherText = ensurePrefix(cipherText);
        return decrypt(cipherText, privateKey, ModeTypeConstant.BASE);
    }
 
    /**
     * decrypt
     *
     * @param cipherText 需加密的字符串
     * @param privateKey 私钥
     * @param modeType   base:标准;bc:BC模式
     */
    public static byte[] decrypt(String cipherText, String privateKey, String modeType) throws InvalidCipherTextException, UnsupportedEncodingException {
        return SecretCommon.decrypt(cipherText, privateKey, ModeTypeConstant.getMode(modeType));
    }
 
    public static String ensurePrefix(String input) {
        // 检查字符串是否以 "04" 开头
        if (!input.startsWith("04")) {
            return "04" + input; // 添加前缀
        }
        return input; // 原样返回
    }
 
    // 将 Hex 格式的字符串转换为字节数组
    public static byte[] hexToBytes(String hex) {
        int length = hex.length();
        byte[] data = new byte[length / 2];
        for (int i = 0; i < length; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i + 1), 16));
        }
        return data;
    }
 
    // 将 DER 格式的字节数组转换为 Base64 编码的字符串
    public static String convertDerToBase64(byte[] derKey) {
        return Base64.getEncoder().encodeToString(derKey);
    }
 
    // 将 Base64 编码的字符串转换为 Hex 格式的字符串
    public static byte[] convertBase64ToBinary(String base64Key) throws Exception {
        // 将 Base64 解码为 DER 编码的字节数组
        byte[] derBytes = Base64.getDecoder().decode(base64Key);
 
        // 直接返回解码后的字节数组
        return derBytes;
    }
 
    // 将字节数组转换为 Hex 格式的字符串
    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString().toUpperCase(); // Convert to uppercase for consistency
    }
    public static String hexToBase64(String hexString) {
        // 将 Hex 字符串转换为字节数组
        byte[] bytes = hexStringToByteArray(hexString);
 
        // 将字节数组转换为 Base64 字符串
        return Base64.getEncoder().encodeToString(bytes);
    }
 
    private static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i+1), 16));
        }
        return data;
    }
}