chenjiahe
2022-03-31 cf178eec8bb8e0ebe0763295d2a47ff4826bcc7f
提交 | 用户 | age
cf178e 1 package com.hx.util.rsa;
C 2
3 import com.sun.org.apache.xml.internal.security.utils.Base64;
4
5 import java.security.KeyFactory;
6 import java.security.PrivateKey;
7 import java.security.PublicKey;
8 import java.security.spec.PKCS8EncodedKeySpec;
9 import java.security.spec.X509EncodedKeySpec;
10
11
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      */
30     public static String sign(String content, String privateKey, String encode) {
31         try {
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
54     public static String sign(String content, String privateKey) {
55         try{
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
124 }