提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.common; |
A |
2 |
|
|
3 |
import java.security.KeyFactory; |
|
4 |
import java.security.KeyPair; |
|
5 |
import java.security.KeyPairGenerator; |
|
6 |
import java.security.PrivateKey; |
|
7 |
import java.security.PublicKey; |
|
8 |
import java.security.SecureRandom; |
|
9 |
import java.security.Signature; |
|
10 |
import java.security.interfaces.DSAPrivateKey; |
|
11 |
import java.security.interfaces.DSAPublicKey; |
|
12 |
import java.security.spec.PKCS8EncodedKeySpec; |
|
13 |
import java.security.spec.X509EncodedKeySpec; |
|
14 |
import java.util.HashMap; |
|
15 |
import java.util.Map; |
|
16 |
|
|
17 |
import org.springframework.util.Base64Utils; |
|
18 |
|
|
19 |
/** |
|
20 |
* @author mengly |
|
21 |
* @version 创建时间:2015年10月27日 下午6:53:16 |
|
22 |
* 类说明 :签名 |
|
23 |
*/ |
|
24 |
|
|
25 |
public class DSAMethod { |
|
26 |
/** |
|
27 |
* 用私钥对信息生成数字签名 |
|
28 |
* |
|
29 |
* @param data |
|
30 |
* 加密数据 |
|
31 |
* @param privateKey |
|
32 |
* 私钥 |
|
33 |
* |
|
34 |
* @return |
|
35 |
* @throws Exception |
|
36 |
*/ |
|
37 |
public static String sign(byte[] data, String privateKey) throws Exception { |
|
38 |
// 解密由base64编码的私钥 |
|
39 |
byte[] keyBytes =Base64Utils.decodeFromString(privateKey); |
|
40 |
|
|
41 |
// 构造PKCS8EncodedKeySpec对象 |
|
42 |
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); |
|
43 |
|
|
44 |
// KEY_ALGORITHM 指定的加密算法 |
|
45 |
KeyFactory keyFactory = KeyFactory.getInstance("DSA"); |
|
46 |
|
|
47 |
// 取私钥匙对象 |
|
48 |
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); |
|
49 |
|
|
50 |
// 用私钥对信息生成数字签名 |
|
51 |
Signature signature = Signature.getInstance(keyFactory.getAlgorithm()); |
|
52 |
signature.initSign(priKey); |
|
53 |
signature.update(data); |
|
54 |
|
|
55 |
return Base64Utils.encodeToString(signature.sign()); |
|
56 |
} |
|
57 |
/** |
|
58 |
* 用私钥对信息生成数字签名 |
|
59 |
* |
|
60 |
* @param data |
|
61 |
* 加密数据 |
|
62 |
* @param privateKey |
|
63 |
* 私钥 |
|
64 |
* |
|
65 |
* @return |
|
66 |
* @throws Exception |
|
67 |
*/ |
|
68 |
public static String sign(String content, String privateKey) throws Exception { |
|
69 |
// 解密由base64编码的私钥 |
|
70 |
byte[] data=content.getBytes("utf-8"); |
|
71 |
byte[] keyBytes =Base64Utils.decodeFromString(privateKey); |
|
72 |
|
|
73 |
// 构造PKCS8EncodedKeySpec对象 |
|
74 |
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); |
|
75 |
|
|
76 |
// KEY_ALGORITHM 指定的加密算法 |
|
77 |
KeyFactory keyFactory = KeyFactory.getInstance("DSA"); |
|
78 |
|
|
79 |
// 取私钥匙对象 |
|
80 |
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); |
|
81 |
|
|
82 |
// 用私钥对信息生成数字签名 |
|
83 |
Signature signature = Signature.getInstance(keyFactory.getAlgorithm()); |
|
84 |
signature.initSign(priKey); |
|
85 |
signature.update(data); |
|
86 |
|
|
87 |
return Base64Utils.encodeToString(signature.sign()); |
|
88 |
} |
|
89 |
public static boolean verify(String content, String publicKey, String sign) |
|
90 |
throws Exception { |
|
91 |
byte[] data=content.getBytes("utf-8"); |
|
92 |
// 解密由base64编码的公钥 |
|
93 |
byte[] keyBytes = Base64Utils.decodeFromString(publicKey); |
|
94 |
|
|
95 |
// 构造X509EncodedKeySpec对象 |
|
96 |
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); |
|
97 |
|
|
98 |
// ALGORITHM 指定的加密算法 |
|
99 |
KeyFactory keyFactory = KeyFactory.getInstance("DSA"); |
|
100 |
|
|
101 |
// 取公钥匙对象 |
|
102 |
PublicKey pubKey = keyFactory.generatePublic(keySpec); |
|
103 |
|
|
104 |
Signature signature = Signature.getInstance(keyFactory.getAlgorithm()); |
|
105 |
signature.initVerify(pubKey); |
|
106 |
signature.update(data); |
|
107 |
|
|
108 |
// 验证签名是否正常 |
|
109 |
return signature.verify(Base64Utils.decodeFromString(sign)); |
|
110 |
} |
|
111 |
|
|
112 |
|
|
113 |
/** |
|
114 |
* 生成密钥 |
|
115 |
* |
|
116 |
* @param seed |
|
117 |
* 种子 |
|
118 |
* @return 密钥对象 |
|
119 |
* @throws Exception |
|
120 |
*/ |
|
121 |
public static Map<String, Object> initKey(String seed) throws Exception { |
|
122 |
KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA"); |
|
123 |
// 初始化随机产生器 |
|
124 |
SecureRandom secureRandom = new SecureRandom(); |
|
125 |
secureRandom.setSeed(seed.getBytes()); |
|
126 |
keygen.initialize(1024, secureRandom); |
|
127 |
|
|
128 |
KeyPair keys = keygen.genKeyPair(); |
|
129 |
|
|
130 |
DSAPublicKey publicKey = (DSAPublicKey) keys.getPublic(); |
|
131 |
DSAPrivateKey privateKey = (DSAPrivateKey) keys.getPrivate(); |
|
132 |
|
|
133 |
Map<String, Object> map = new HashMap<String, Object>(2); |
|
134 |
map.put("publicKey", publicKey); |
|
135 |
map.put("privateKey", privateKey); |
|
136 |
|
|
137 |
return map; |
|
138 |
} |
|
139 |
} |