概述
信息安全基本概念:
- DH(Diffie–Hellman key exchange,迪菲-赫尔曼密钥交换)
DH
是一种安全协议,,一种确保共享KEY安全穿越不安全网络的方法,它是OAKLEY的一个组成部分。
这个机制的巧妙在于需要安全通信的双方可以用这个方法确定对称密钥。然后可以用这个密钥进行加密和解密。但是注意,这个密钥交换协议/算法只能用于密钥的交换,而不能进行消息的加密和解密。双方确定要用的密钥后,要使用其他对称密钥操作加密算法实际加密和解密消息。
Oakley算法是对Diffie-Hellman密钥交换算法的优化,它保留了后者的优点,同时克服了其弱点. Oakley算法具有五个重要特征: 它采用称为cookie程序的机制来对抗阻塞攻击. 它使得双方能够协商一个全局参数集合. 它使用了现时来保证抵抗重演攻击. 它能够交换Diffie-Hellman公开密钥. 它对Diffie-Hellman交换进行鉴别以对抗中间人的攻击.
流程分析
1.甲方构建密钥对儿,将公钥公布给乙方,将私钥保留;双方约定数据加密算法;乙方通过甲方公钥构建密钥对儿,将公钥公布给甲方,将私钥保留。
2.甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给乙方加密后的数据;乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。
3.乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给甲方加密后的数据;甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。
代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234package com.jd.order.util.encryption; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; import javax.crypto.KeyAgreement; import javax.crypto.SecretKey; import javax.crypto.interfaces.DHPrivateKey; import javax.crypto.interfaces.DHPublicKey; import javax.crypto.spec.DHParameterSpec; import org.apache.commons.codec.binary.Base64; /** * DH密码交换协议 * * @author 木子旭 * @since 2017年3月17日上午9:18:14 * @version %I%,%G% */ public class DHCoder { public static final String ALGORITHM = "DH"; /** * 默认密钥字节数 * * <pre> * DH * Default Keysize 1024 * Keysize must be a multiple of 64, ranging from 512 to 1024 (inclusive). * </pre> */ private static final int KEY_SIZE = 1024; /** * DH加密下需要一种对称加密算法对数据加密,这里我们使用DES,也可以使用其他对称加密算法。 */ public static final String SECRET_ALGORITHM = "DES"; private static final String PUBLIC_KEY = "DHPublicKey"; private static final String PRIVATE_KEY = "DHPrivateKey"; /** * 初始化甲方密钥 * * @return * @throws Exception */ public static Map<String, Object> initKey() throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance(ALGORITHM); keyPairGenerator.initialize(KEY_SIZE); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 甲方公钥 DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic(); // 甲方私钥 DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate(); Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } /** * 初始化乙方密钥 * * @param key * 甲方公钥 * @return * @throws Exception */ public static Map<String, Object> initKey(String key) throws Exception { // 解析甲方公钥 byte[] keyBytes = decryptBASE64(key); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); // 由甲方公钥构建乙方密钥 DHParameterSpec dhParamSpec = ((DHPublicKey) pubKey).getParams(); KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance(keyFactory.getAlgorithm()); keyPairGenerator.initialize(dhParamSpec); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 乙方公钥 DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic(); // 乙方私钥 DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate(); Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } /** * 加密<br> * * @param data * 待加密数据 * @param publicKey * 甲方公钥 * @param privateKey * 乙方私钥 * @return * @throws Exception */ public static byte[] encrypt(byte[] data, String publicKey, String privateKey) throws Exception { // 生成本地密钥 SecretKey secretKey = getSecretKey(publicKey, privateKey); // 数据加密 Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, secretKey); return cipher.doFinal(data); } /** * 解密<br> * * @param data * 待解密数据 * @param publicKey * 乙方公钥 * @param privateKey * 乙方私钥 * @return * @throws Exception */ public static byte[] decrypt(byte[] data, String publicKey, String privateKey) throws Exception { // 生成本地密钥 SecretKey secretKey = getSecretKey(publicKey, privateKey); // 数据解密 Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, secretKey); return cipher.doFinal(data); } /** * 构建密钥 * * @param publicKey * 公钥 * @param privateKey * 私钥 * @return * @throws Exception */ private static SecretKey getSecretKey(String publicKey, String privateKey) throws Exception { // 初始化公钥 byte[] pubKeyBytes = decryptBASE64(publicKey); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKeyBytes); PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); // 初始化私钥 byte[] priKeyBytes = decryptBASE64(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKeyBytes); Key priKey = keyFactory.generatePrivate(pkcs8KeySpec); KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory .getAlgorithm()); keyAgree.init(priKey); keyAgree.doPhase(pubKey, true); // 生成本地密钥 SecretKey secretKey = keyAgree.generateSecret(SECRET_ALGORITHM); return secretKey; } /** * 取得私钥 * * @param keyMap * @return * @throws Exception */ public static String getPrivateKey(Map<String, Object> keyMap) throws Exception { Key key = (Key) keyMap.get(PRIVATE_KEY); return encryptBASE64(key.getEncoded()); } /** * 取得公钥 * * @param keyMap * @return * @throws Exception */ public static String getPublicKey(Map<String, Object> keyMap) throws Exception { Key key = (Key) keyMap.get(PUBLIC_KEY); return encryptBASE64(key.getEncoded()); } public static byte[] decryptBASE64(String data) { return Base64.decodeBase64(data); } public static String encryptBASE64(byte[] data) { return new String(Base64.encodeBase64(data)); } }
测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63package com.jd.order.util.encryption; import static org.junit.Assert.assertEquals; import java.util.Map; import org.junit.Test; public class DHCoderTest { @Test public void test() throws Exception { // 生成甲方密钥对儿 Map<String, Object> aKeyMap = DHCoder.initKey(); String aPublicKey = DHCoder.getPublicKey(aKeyMap); String aPrivateKey = DHCoder.getPrivateKey(aKeyMap); System.err.println("甲方公钥:r" + aPublicKey); System.err.println("甲方私钥:r" + aPrivateKey); // 由甲方公钥产生本地密钥对儿 Map<String, Object> bKeyMap = DHCoder.initKey(aPublicKey); String bPublicKey = DHCoder.getPublicKey(bKeyMap); String bPrivateKey = DHCoder.getPrivateKey(bKeyMap); System.err.println("乙方公钥:r" + bPublicKey); System.err.println("乙方私钥:r" + bPrivateKey); System.err.println("乙方构建加密,甲方解密 "); String aInput = "abc "; System.err.println("原文: " + aInput); // 乙方构建密钥消息,使用甲方公钥,乙方私钥构建密文 byte[] aCode = DHCoder.encrypt(aInput.getBytes(), aPublicKey, bPrivateKey); // 甲方解密乙方加密消息,使用乙方公钥,甲方私钥解密 byte[] aDecode = DHCoder.decrypt(aCode, bPublicKey, aPrivateKey); String aOutput = (new String(aDecode)); System.err.println("解密: " + aOutput); assertEquals(aInput, aOutput); System.err.println(" ===============反过来加密解密================== "); System.err.println("甲方构建加密,乙方解密 "); String bInput = "def "; System.err.println("原文: " + bInput); // 甲方构建密钥消息,由乙方公钥,甲方私钥构建密文 byte[] bCode = DHCoder.encrypt(bInput.getBytes(), bPublicKey, aPrivateKey); // 乙方解密甲方加密消息,使用甲方公钥,乙方私钥解密 byte[] bDecode = DHCoder.decrypt(bCode, aPublicKey, bPrivateKey); String bOutput = (new String(bDecode)); System.err.println("解密: " + bOutput); assertEquals(bInput, bOutput); } }
结果输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15甲方公钥: MIIBpjCCARsGCSqGSIb3DQEDATCCAQwCgYEA/X9TgR11EilS30qcLuzk5/YRt1I870QAwx4/gLZRJmlFXUAiUftZPY1Y+r/F9bow9subVWzXgTuAHTRv8mZgt2uZUKWkn5/oBHsQIsJPu6nX/rfGG/g7V+fGqKYVDwT7g/bTxR7DAjVUE1oWkTL2dfOuK2HXKu/yIgMZndFIAccCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuW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ae1UlZAFMO/7PSSoCAgIAA4GEAAKBgA5xV90r65kHFOCgKqE+6XCLx+Zxu+g4sg+A3bReyp+IgsIOQQhMXS1Lh3cCQMEMni1jqQ0c5hT3rBV0SIDaVD/57A62pevjdt1oKq4AQBabXNxXj9alENMozud1PVbEgq8+n89mt3s2QMOKpOCFMfjTIEi3GJRMa+MI7B28S1gS 乙方私钥: MIIBZwIBADCCARsGCSqGSIb3DQEDATCCAQwCgYEA/X9TgR11EilS30qcLuzk5/YRt1I870QAwx4/gLZRJmlFXUAiUftZPY1Y+r/F9bow9subVWzXgTuAHTRv8mZgt2uZUKWkn5/oBHsQIsJPu6nX/rfGG/g7V+fGqKYVDwT7g/bTxR7DAjVUE1oWkTL2dfOuK2HXKu/yIgMZndFIAccCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoCAgIABEMCQQCp+LEg3WYOYkFP8Cyl05O5y69ilJ99KO8NpdIjnqoD0uK9gl6GzWr2HVtXHs6KDRSJQFcKaOvldhfp9rVPQdiA 乙方构建加密,甲方解密 原文: abc 解密: abc ===============反过来加密解密================== 甲方构建加密,乙方解密 原文: def 解密: def
参考文章
百度百科,维基百科
http://snowolf.iteye.com/blog/382422,等
最后
以上就是留胡子火最近收集整理的关于java-信息安全(八)-迪菲-赫尔曼(DH)密钥交换概述的全部内容,更多相关java-信息安全(八)-迪菲-赫尔曼(DH)密钥交换概述内容请搜索靠谱客的其他文章。
发表评论 取消回复