提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.common; |
A |
2 |
|
|
3 |
import java.io.ByteArrayOutputStream; |
|
4 |
import java.security.Key; |
|
5 |
import java.security.KeyFactory; |
|
6 |
import java.security.KeyPair; |
|
7 |
import java.security.KeyPairGenerator; |
|
8 |
import java.security.PrivateKey; |
|
9 |
import java.security.PublicKey; |
|
10 |
import java.security.Signature; |
|
11 |
import java.security.interfaces.RSAPrivateKey; |
|
12 |
import java.security.interfaces.RSAPublicKey; |
|
13 |
import java.security.spec.PKCS8EncodedKeySpec; |
|
14 |
import java.security.spec.X509EncodedKeySpec; |
|
15 |
import java.util.HashMap; |
|
16 |
import java.util.Map; |
|
17 |
|
|
18 |
import javax.crypto.Cipher; |
|
19 |
|
|
20 |
import org.springframework.util.Base64Utils; |
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
/** |
|
25 |
* @author mengly |
|
26 |
* @version 创建时间:2015年6月9日 下午11:36:50 |
|
27 |
* 类说明:rsa加密解密 depends:spring |
|
28 |
*/ |
|
29 |
|
|
30 |
public class RSAMethod { |
|
31 |
/** *//** |
|
32 |
* 加密算法RSA |
|
33 |
*/ |
|
34 |
public static final String KEY_ALGORITHM = "RSA"; |
|
35 |
|
|
36 |
/** *//** |
|
37 |
* 签名算法 |
|
38 |
*/ |
|
39 |
public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; |
|
40 |
|
|
41 |
/** *//** |
|
42 |
* 获取公钥的key |
|
43 |
*/ |
|
44 |
private static final String PUBLIC_KEY = "RSAPublicKey"; |
|
45 |
|
|
46 |
/** *//** |
|
47 |
* 获取私钥的key |
|
48 |
*/ |
|
49 |
private static final String PRIVATE_KEY = "RSAPrivateKey"; |
|
50 |
|
|
51 |
/** *//** |
|
52 |
* RSA最大加密明文大小 |
|
53 |
*/ |
|
54 |
private static final int MAX_ENCRYPT_BLOCK = 117; |
|
55 |
|
|
56 |
/** *//** |
|
57 |
* RSA最大解密密文大小 |
|
58 |
*/ |
|
59 |
private static final int MAX_DECRYPT_BLOCK = 128; |
|
60 |
|
|
61 |
/** *//** |
|
62 |
* <p> |
|
63 |
* 生成密钥对(公钥和私钥) |
|
64 |
* </p> |
|
65 |
* |
|
66 |
* @return |
|
67 |
* @throws Exception |
|
68 |
*/ |
|
69 |
public static Map<String, Object> genKeyPair() throws Exception { |
|
70 |
Map<String, Object> keyMap = new HashMap<String, Object>(2); |
|
71 |
try { |
|
72 |
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); |
|
73 |
keyPairGen.initialize(1024); |
|
74 |
KeyPair keyPair = keyPairGen.generateKeyPair(); |
|
75 |
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); |
|
76 |
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); |
|
77 |
keyMap.put(PUBLIC_KEY, publicKey); |
|
78 |
keyMap.put(PRIVATE_KEY, privateKey); |
|
79 |
} catch (Exception e) { |
|
80 |
e.printStackTrace(); |
|
81 |
} |
|
82 |
|
|
83 |
return keyMap; |
|
84 |
} |
|
85 |
|
|
86 |
|
|
87 |
/** *//** |
|
88 |
* <p> |
|
89 |
* 用私钥对信息生成数字签名 |
|
90 |
* </p> |
|
91 |
* |
|
92 |
* @param data 已加密数据 |
|
93 |
* @param privateKey 私钥(BASE64编码) |
|
94 |
* |
|
95 |
* @return (BASE64编码) |
|
96 |
* @throws Exception |
|
97 |
*/ |
|
98 |
public static String sign(byte[] data, String privateKey) throws Exception { |
|
99 |
byte[] keyBytes = Base64Utils.decodeFromString(privateKey); |
|
100 |
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); |
|
101 |
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
|
102 |
PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); |
|
103 |
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); |
|
104 |
signature.initSign(privateK); |
|
105 |
signature.update(data); |
|
106 |
return Base64Utils.encodeToString(signature.sign()); |
|
107 |
} |
|
108 |
|
|
109 |
/** *//** |
|
110 |
* <p> |
|
111 |
* 校验数字签名 |
|
112 |
* </p> |
|
113 |
* |
|
114 |
* @param data 已加密数据 |
|
115 |
* @param publicKey 公钥(BASE64编码) |
|
116 |
* @param sign 数字签名 |
|
117 |
* |
|
118 |
* @return |
|
119 |
* @throws Exception |
|
120 |
* |
|
121 |
*/ |
|
122 |
public static boolean verify(byte[] data, String publicKey, String sign) |
|
123 |
throws Exception { |
|
124 |
byte[] keyBytes = Base64Utils.decodeFromString(publicKey); |
|
125 |
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); |
|
126 |
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
|
127 |
PublicKey publicK = keyFactory.generatePublic(keySpec); |
|
128 |
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); |
|
129 |
signature.initVerify(publicK); |
|
130 |
signature.update(data); |
|
131 |
return signature.verify(Base64Utils.decodeFromString(sign)); |
|
132 |
} |
|
133 |
/** *//** |
|
134 |
* <P> |
|
135 |
* 私钥解密 |
|
136 |
* </p> |
|
137 |
* |
|
138 |
* @param encryptedData 已加密数据 |
|
139 |
* @param privateKey 私钥(BASE64编码) |
|
140 |
* @return |
|
141 |
* @throws Exception |
|
142 |
*/ |
|
143 |
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) |
|
144 |
throws Exception { |
|
145 |
byte[] keyBytes = Base64Utils.decodeFromString(privateKey); |
|
146 |
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); |
|
147 |
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
|
148 |
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); |
|
149 |
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); |
|
150 |
cipher.init(Cipher.DECRYPT_MODE, privateK); |
|
151 |
int inputLen = encryptedData.length; |
|
152 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
153 |
int offSet = 0; |
|
154 |
byte[] cache; |
|
155 |
int i = 0; |
|
156 |
// 对数据分段解密 |
|
157 |
while (inputLen - offSet > 0) { |
|
158 |
if (inputLen - offSet > MAX_DECRYPT_BLOCK) { |
|
159 |
cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); |
|
160 |
} else { |
|
161 |
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); |
|
162 |
} |
|
163 |
out.write(cache, 0, cache.length); |
|
164 |
i++; |
|
165 |
offSet = i * MAX_DECRYPT_BLOCK; |
|
166 |
} |
|
167 |
byte[] decryptedData = out.toByteArray(); |
|
168 |
out.close(); |
|
169 |
return decryptedData; |
|
170 |
} |
|
171 |
|
|
172 |
/** *//** |
|
173 |
* <p> |
|
174 |
* 公钥解密 |
|
175 |
* </p> |
|
176 |
* |
|
177 |
* @param encryptedData 已加密数据 |
|
178 |
* @param publicKey 公钥(BASE64编码) |
|
179 |
* @return |
|
180 |
* @throws Exception |
|
181 |
*/ |
|
182 |
public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) |
|
183 |
throws Exception { |
|
184 |
byte[] keyBytes = Base64Utils.decodeFromString(publicKey); |
|
185 |
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); |
|
186 |
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
|
187 |
Key publicK = keyFactory.generatePublic(x509KeySpec); |
|
188 |
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); |
|
189 |
cipher.init(Cipher.DECRYPT_MODE, publicK); |
|
190 |
int inputLen = encryptedData.length; |
|
191 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
192 |
int offSet = 0; |
|
193 |
byte[] cache; |
|
194 |
int i = 0; |
|
195 |
// 对数据分段解密 |
|
196 |
while (inputLen - offSet > 0) { |
|
197 |
if (inputLen - offSet > MAX_DECRYPT_BLOCK) { |
|
198 |
cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); |
|
199 |
} else { |
|
200 |
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); |
|
201 |
} |
|
202 |
out.write(cache, 0, cache.length); |
|
203 |
i++; |
|
204 |
offSet = i * MAX_DECRYPT_BLOCK; |
|
205 |
} |
|
206 |
byte[] decryptedData = out.toByteArray(); |
|
207 |
out.close(); |
|
208 |
return decryptedData; |
|
209 |
} |
|
210 |
|
|
211 |
|
|
212 |
/** *//** |
|
213 |
* <p> |
|
214 |
* 公钥加密 |
|
215 |
* </p> |
|
216 |
* |
|
217 |
* @param data 源数据 |
|
218 |
* @param publicKey 公钥(BASE64编码) |
|
219 |
* @return |
|
220 |
* @throws Exception |
|
221 |
*/ |
|
222 |
public static byte[] encryptByPublicKey(byte[] data, String publicKey) |
|
223 |
throws Exception { |
|
224 |
byte[] keyBytes = Base64Utils.decodeFromString(publicKey); |
|
225 |
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); |
|
226 |
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
|
227 |
Key publicK = keyFactory.generatePublic(x509KeySpec); |
|
228 |
// 对数据加密 |
|
229 |
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); |
|
230 |
cipher.init(Cipher.ENCRYPT_MODE, publicK); |
|
231 |
int inputLen = data.length; |
|
232 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
233 |
int offSet = 0; |
|
234 |
byte[] cache; |
|
235 |
int i = 0; |
|
236 |
// 对数据分段加密 |
|
237 |
while (inputLen - offSet > 0) { |
|
238 |
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { |
|
239 |
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); |
|
240 |
} else { |
|
241 |
cache = cipher.doFinal(data, offSet, inputLen - offSet); |
|
242 |
} |
|
243 |
out.write(cache, 0, cache.length); |
|
244 |
i++; |
|
245 |
offSet = i * MAX_ENCRYPT_BLOCK; |
|
246 |
} |
|
247 |
byte[] encryptedData = out.toByteArray(); |
|
248 |
out.close(); |
|
249 |
return encryptedData; |
|
250 |
} |
|
251 |
|
|
252 |
/** *//** |
|
253 |
* <p> |
|
254 |
* 私钥加密 |
|
255 |
* </p> |
|
256 |
* |
|
257 |
* @param data 源数据 |
|
258 |
* @param privateKey 私钥(BASE64编码) |
|
259 |
* @return |
|
260 |
* @throws Exception |
|
261 |
*/ |
|
262 |
public static byte[] encryptByPrivateKey(byte[] data, String privateKey) |
|
263 |
throws Exception { |
|
264 |
byte[] keyBytes = Base64Utils.decodeFromString(privateKey); |
|
265 |
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); |
|
266 |
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
|
267 |
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); |
|
268 |
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); |
|
269 |
cipher.init(Cipher.ENCRYPT_MODE, privateK); |
|
270 |
int inputLen = data.length; |
|
271 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
272 |
int offSet = 0; |
|
273 |
byte[] cache; |
|
274 |
int i = 0; |
|
275 |
// 对数据分段加密 |
|
276 |
while (inputLen - offSet > 0) { |
|
277 |
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { |
|
278 |
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); |
|
279 |
} else { |
|
280 |
cache = cipher.doFinal(data, offSet, inputLen - offSet); |
|
281 |
} |
|
282 |
out.write(cache, 0, cache.length); |
|
283 |
i++; |
|
284 |
offSet = i * MAX_ENCRYPT_BLOCK; |
|
285 |
} |
|
286 |
byte[] encryptedData = out.toByteArray(); |
|
287 |
out.close(); |
|
288 |
return encryptedData; |
|
289 |
} |
|
290 |
|
|
291 |
/** |
|
292 |
* <p> |
|
293 |
* 获取私钥 |
|
294 |
* </p> |
|
295 |
* |
|
296 |
* @param keyMap 密钥对 |
|
297 |
* @return |
|
298 |
* @throws Exception |
|
299 |
*/ |
|
300 |
public static String getPrivateKey(Map<String, Object> keyMap) |
|
301 |
throws Exception { |
|
302 |
Key key = (Key) keyMap.get(PRIVATE_KEY); |
|
303 |
return Base64Utils.encodeToString(key.getEncoded()); |
|
304 |
} |
|
305 |
|
|
306 |
/** *//** |
|
307 |
* <p> |
|
308 |
* 获取公钥 |
|
309 |
* </p> |
|
310 |
* |
|
311 |
* @param keyMap 密钥对 |
|
312 |
* @return |
|
313 |
* @throws Exception |
|
314 |
*/ |
|
315 |
public static String getPublicKey(Map<String, Object> keyMap) |
|
316 |
throws Exception { |
|
317 |
Key key = (Key) keyMap.get(PUBLIC_KEY); |
|
318 |
return Base64Utils.encodeToString(key.getEncoded()); |
|
319 |
} |
|
320 |
} |