彭雪彬
2025-07-14 c1d20b425b10e8ba59f102dd1ab413055883eed0
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
package com.oying.modules.hwc.utils;
 
import org.apache.commons.codec.binary.Base64;
 
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
 
 
/**
 * @author zeming.fan@swiftpass.cn
 *
 */
public class RSAUtil {
 
    public enum SignatureSuite {
        //SHA1("SHA1WithRSA"), MD5("MD5WithRSA");
        SHA1("SHA1WithRSA"), SHA256("SHA256WithRSA");
        private final String suite;
 
        SignatureSuite(String suite) {
            this.suite = suite;
        }
 
        public String val() {
            return suite;
        }
    }
 
 
    private static KeyFactory getKeyFactory() {
        try {
            return KeyFactory.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            // 应该不会出现
            throw new RuntimeException("初始化RSA KeyFactory失败");
        }
    }
 
    public static byte[] sign(SignatureSuite suite, byte[] msgBuf, String privateKeyStr) {
        Signature signature = null;
        try {
            signature = Signature.getInstance(suite.val());
        } catch (Exception e) {
            // 上线运行时套件一定存在
            // 异常不往外抛
        }
 
        try {
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyStr));
            PrivateKey privateKey = getKeyFactory().generatePrivate(keySpec);
            if (signature != null) {
                signature.initSign(privateKey);
            }
        } catch(Exception e) {
            //logger.warn("解析私钥失败:{}", e.getMessage());
            throw new RuntimeException("INVALID_PRIKEY");
        }
        try {
            if (signature != null) {
                signature.update(msgBuf);
            }
            if (signature != null) {
                return signature.sign();
            }
        } catch (SignatureException e) {
            // 一般不会出现
            throw new RuntimeException(e.getMessage());
        }
        return msgBuf;
    }
 
    public static boolean verifySign(SignatureSuite suite, byte[] msgBuf, byte[] sign, String publicKeyStr) {
        Signature signature = null;
        try {
            signature = Signature.getInstance(suite.val());
        } catch (Exception e) {
            // 上线运行时套件一定存在
            // 异常不往外抛
        }
 
        try {
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyStr));
            PublicKey publicKey = getKeyFactory().generatePublic(keySpec);
            if (signature != null) {
                signature.initVerify(publicKey);
            }
        } catch(Exception e) {
            throw new RuntimeException("INVALID_PUBKEY");
        }
        try {
            if (signature != null) {
                signature.update(msgBuf);
            }
            if (signature != null) {
                return signature.verify(sign);
            }
        } catch (SignatureException e) {
            // 一般不会出现
            throw new RuntimeException("签名格式不合法");
        }
        return false;
    }
}