chenjiahe
2022-03-31 f96f3900e2ec516e10fa9143f60e7e64e500d2a0
提交 | 用户 | age
cf178e 1 package com.hx.util.rsa;
C 2
f96f39 3 import com.hx.util.StringUtils;
C 4
cf178e 5 import java.security.KeyFactory;
C 6 import java.security.PrivateKey;
7 import java.security.PublicKey;
8 import java.security.spec.PKCS8EncodedKeySpec;
9 import java.security.spec.X509EncodedKeySpec;
f96f39 10 import java.util.*;
cf178e 11
C 12
13 /**
14  * RSA签名验签类
15  */
16 public class RSASignature{
17
18     /**
19      * 签名算法
20      */
21     public static final String SIGN_ALGORITHMS = "SHA1WithRSA";
22
23     /**
24      * RSA签名
25      * @param content 待签名数据
26      * @param privateKey 商户私钥
27      * @param encode 字符集编码
28      * @return 签名值
29      */
f96f39 30     public static String createSign(String content, String privateKey, String encode) {
cf178e 31         try {
C 32             PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec( Base64.decode(privateKey) );
33
34             KeyFactory keyf = KeyFactory.getInstance("RSA");
35             PrivateKey priKey = keyf.generatePrivate(priPKCS8);
36
37             java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
38
39             signature.initSign(priKey);
40             signature.update( content.getBytes(encode));
41
42             byte[] signed = signature.sign();
43
44             return Base64.encode(signed);
45         }
46         catch (Exception e)
47         {
48             e.printStackTrace();
49         }
50
51         return null;
52     }
53
f96f39 54     public static String createSign(String content, String privateKey) {
cf178e 55         try{
C 56             PKCS8EncodedKeySpec priPKCS8     = new PKCS8EncodedKeySpec( Base64.decode(privateKey) );
57             KeyFactory keyf = KeyFactory.getInstance("RSA");
58             PrivateKey priKey = keyf.generatePrivate(priPKCS8);
59             java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
60             signature.initSign(priKey);
61             signature.update( content.getBytes());
62             byte[] signed = signature.sign();
63             return Base64.encode(signed);
64         } catch (Exception e) {
65             e.printStackTrace();
66         }
67         return null;
68     }
69
f96f39 70
C 71     /**
72      * RSA验签名检查
73      * @param params 所有参数
74      * @param publicKey 分配给开发商公钥
75      * @param encode 字符集编码
76      * @return 布尔值
77      */
78     public static boolean doCheck(Map<String,String> params, String publicKey, String encode) {
79         String sign = params.get("sign");
80         if(sign == null){
81             return false;
82         }
83         String content = getSignCheckContent(params);
84         return doCheck( content, sign, publicKey, encode);
85     }
86
87     public static String getSignCheckContent(Map<String, String> params) {
88         if (params == null) {
89             return null;
90         } else {
91             params.remove("sign");
92             StringBuilder content = new StringBuilder();
93             List<String> keys = new ArrayList(params.keySet());
94             Collections.sort(keys);
95
96             for(int i = 0; i < keys.size(); ++i) {
97                 String key = (String)keys.get(i);
98                 String value = (String)params.get(key);
99                 if(StringUtils.isEmpty(value)){
100                     continue;
101                 }
102                 content.append(i == 0 ? "" : "&").append(key).append("=").append(value);
103             }
104             return content.toString();
105         }
106     }
107
cf178e 108     /**
C 109      * RSA验签名检查
110      * @param content 待签名数据
111      * @param sign 签名值
112      * @param publicKey 分配给开发商公钥
113      * @param encode 字符集编码
114      * @return 布尔值
115      */
116     public static boolean doCheck(String content, String sign, String publicKey,String encode) {
117         try {
118             KeyFactory keyFactory = KeyFactory.getInstance("RSA");
119             byte[] encodedKey = Base64.decode(publicKey);
120             PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
121
122
123             java.security.Signature signature = java.security.Signature
124                     .getInstance(SIGN_ALGORITHMS);
125
126             signature.initVerify(pubKey);
127             signature.update( content.getBytes(encode) );
128
129             boolean bverify = signature.verify( Base64.decode(sign) );
130             return bverify;
131
132         } catch (Exception e) {
133             e.printStackTrace();
134         }
135
136         return false;
137     }
138
139     public static boolean doCheck(String content, String sign, String publicKey) {
140         try {
141             KeyFactory keyFactory = KeyFactory.getInstance("RSA");
142             byte[] encodedKey = Base64.decode(publicKey);
143             PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
144
145
146             java.security.Signature signature = java.security.Signature
147                     .getInstance(SIGN_ALGORITHMS);
148
149             signature.initVerify(pubKey);
150             signature.update( content.getBytes() );
151
152             boolean bverify = signature.verify( Base64.decode(sign) );
153             return bverify;
154
155         } catch (Exception e) {
156             e.printStackTrace();
157         }
158
159         return false;
160     }
161
162 }