zhouxiang
2022-04-25 8ab2ad5580212b91df848e4c127f2a682485fde3
提交 | 用户 | 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
70     /**
71      * RSA验签名检查
72      * @param content 待签名数据
73      * @param sign 签名值
74      * @param publicKey 分配给开发商公钥
75      * @param encode 字符集编码
76      * @return 布尔值
77      */
78     public static boolean doCheck(String content, String sign, String publicKey,String encode) {
79         try {
80             KeyFactory keyFactory = KeyFactory.getInstance("RSA");
81             byte[] encodedKey = Base64.decode(publicKey);
82             PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
83
84
85             java.security.Signature signature = java.security.Signature
86                     .getInstance(SIGN_ALGORITHMS);
87
88             signature.initVerify(pubKey);
89             signature.update( content.getBytes(encode) );
90
91             boolean bverify = signature.verify( Base64.decode(sign) );
92             return bverify;
93
94         } catch (Exception e) {
95             e.printStackTrace();
96         }
97
98         return false;
99     }
100
101     public static boolean doCheck(String content, String sign, String publicKey) {
102         try {
103             KeyFactory keyFactory = KeyFactory.getInstance("RSA");
104             byte[] encodedKey = Base64.decode(publicKey);
105             PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
106
107
108             java.security.Signature signature = java.security.Signature
109                     .getInstance(SIGN_ALGORITHMS);
110
111             signature.initVerify(pubKey);
112             signature.update( content.getBytes() );
113
114             boolean bverify = signature.verify( Base64.decode(sign) );
115             return bverify;
116
117         } catch (Exception e) {
118             e.printStackTrace();
119         }
120
121         return false;
122     }
123
5d99e5 124     /**
C 125      * ASCII码递增排序(剔除空值)
126      * @param params
127      * @return
128      */
129     public static String getSignCheckContent(Map<String, String> params) {
130         if (params == null) {
131             return null;
132         } else {
133             StringBuilder content = new StringBuilder();
134             List<String> keys = new ArrayList(params.keySet());
135             Collections.sort(keys);
136
137             for(int i = 0; i < keys.size(); ++i) {
138                 String key = (String)keys.get(i);
591b96 139                 Object value = params.get(key);
497008 140                 if(value == null){
5d99e5 141                     continue;
C 142                 }
143                 content.append(i == 0 ? "" : "&").append(key).append("=").append(value);
144             }
145             return content.toString();
146         }
147     }
148
cf178e 149 }