安全算法demo

一些常用的安全加密解码代码

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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package com.bogle.nio;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
* Created by lenovo on 2017/4/4.
*/
public class Digest {


//--------------------------------------------------------------------------------数值摘要----------------------------------------------------------------------

/**
* md5加密
*
* @param content
* @return
* @throws Exception
*/
public static byte[] md5(String content) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(content.getBytes("utf8"));
return bytes;
}


/**
* sha1加密
*
* @param content
* @return
* @throws Exception
*/
public static byte[] sha1(String content) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] bytes = md.digest(content.getBytes("utf8"));
return bytes;
}

/**
* bytes 转十六进制
*
* @param bytes
* @return
*/
public static String bytes2Hex(byte[] bytes) {
StringBuilder hex = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
boolean negative = false;//是否为负数
if (b < 0) negative = true;
int inte = Math.abs(b);
if (negative) inte = inte | 0x80;//负数会转成整数(最高位的符号变成数值计算),在转十六进制
String temp = Integer.toHexString(inte & 0xFF);
if (temp.length() == 1) {
hex.append(0);
}
hex.append(temp.toLowerCase());
}
return hex.toString();
}

/**
* 十六进制转bytes
*
* @param hex
* @return
*/
public static byte[] hex2Bytes(String hex) {
byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < hex.length(); i = i + 2) {
String subStr = hex.substring(i, i + 2);
boolean negative = false; //是否为负数
int inte = Integer.parseInt(subStr, 16);
if (inte > 127) negative = true;
if (inte == 128) {
inte = -128;
} else if (negative) {
inte = 0 - (inte & 0x7F);
}
byte b = (byte) inte;
bytes[i / 2] = b;
}
return bytes;
}

/**
* byte转base64
*
* @param bytes
* @return
*/
public static String byte2base64(byte[] bytes) {
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(bytes);
}

/**
* base64转bytes
*
* @param base64
* @return
* @throws IOException
*/
public static byte[] base642byte(String base64) throws IOException {
BASE64Decoder base64Decoder = new BASE64Decoder();
return base64Decoder.decodeBuffer(base64);
}

//--------------------------------------------------------------------------------对称加密----------------------------------------------------------------------

//////////////////////////////////////////////////////////DES加密解密/////////////////////////////////////

/**
* 生产DES密钥
*
* @return
* @throws Exception
*/
public static String getKeyDES() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56);
SecretKey key = keyGen.generateKey();
String base64Str = byte2base64(key.getEncoded());
return base64Str;
}

public static void main(String[] args) throws Exception {
System.out.println(getKeyDES());
}

public static SecretKey loadKeyDES(String base64Key) throws Exception {
byte[] bytes = base642byte(base64Key);
SecretKey key = new SecretKeySpec(bytes, "DES");
return key;
}

/**
* DES加密
*
* @param source
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptDES(byte[] source, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bytes = cipher.doFinal(source);
return bytes;
}

/**
* DES解密
*
* @param source
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptDES(byte[] source, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] bytes = cipher.doFinal(source);
return bytes;
}

//////////////////////////////////////////////////////////AES加密解密/////////////////////////////////////

/**
* 生成AES密钥
*
* @return
* @throws Exception
*/
public static String genKeyAES() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey key = keyGen.generateKey();
String base64Str = byte2base64(key.getEncoded());
return base64Str;
}

public static SecretKey loadKeyAES(String base64Key) throws Exception {
byte[] bytes = base642byte(base64Key);
SecretKey key = new SecretKeySpec(bytes, "AES");
return key;
}

/**
* AES加密
*
* @param source
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptAES(byte[] source, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bytes = cipher.doFinal(source);
return bytes;
}

/**
* AES解密
*
* @param source
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptAES(byte[] source, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] bytes = cipher.doFinal(source);
return bytes;
}

//--------------------------------------------------------------------------------非对称加密----------------------------------------------------------------------

/**
* 生成公钥和私钥
*
* @return
* @throws Exception
*/
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}

public static String getPublicKey(KeyPair keyPair) {
PublicKey publicKey = keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return byte2base64(bytes);
}

public static String getPrivateKey(KeyPair keyPair) {
PublicKey publicKey = keyPair.getPrivate()
byte[] bytes = publicKey.getEncoded();
return byte2base64(bytes);
}

/**
* 将String类型的密钥转换为PublicKey
*
* @param pubStr
* @return
* @throws Exception
*/
public static PublicKey string2PublicKey(String pubStr) throws Exception {
byte[] keyBytes = base642byte(pubStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}

/**
* 将String类型的密钥转换为privateKey
*
* @param priStr
* @return
* @throws Exception
*/
public static PrivateKey string2PrivateKey(String priStr) throws Exception {
byte[] keyBytes = base642byte(priStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}

/**
* 使用公钥加密
*
* @param content
* @param publicKey
* @return
* @throws Exception
*/
public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}

/**
* 使用私钥解密
*
* @param content
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}


//--------------------------------------------------------------------------------MD5withRSA----------------------------------------------------------------------

/**
* MD5withRSA算法实现
* 签名
*
* @param content
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] sign(byte[] content, PrivateKey privateKey) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(content);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] encryptBytes = cipher.doFinal(bytes);
return encryptBytes;
}

/**
* 验证签名是否正确
* @param content 内容
* @param sign 签名
* @param publicKey 公钥
* @return
* @throws Exception
*/
public static boolean verify(byte[] content, byte[] sign, PublicKey publicKey) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(content);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE,publicKey);
byte[] decryptBytes = cipher.doFinal(sign);
return byte2base64(decryptBytes).equals(byte2base64(bytes));
}

//基于Signature API的使用
public static byte[] sign(byte[] content, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance("MD5withRSA");
signature.initSign(privateKey);
signature.update(content);
return signature.sign();
}

public static boolean verify(byte[] content, byte[] sign, PublicKey publicKey) throws Exception {
Signature signature = Signature.getInstance("MD5withRSA");
signature.initVerify(publicKey);
signature.update(content);
return signature.verify(sign);
}

//--------------------------------------------------------------------------------SH1withRSA----------------------------------------------------------------------

/**
* sh1签名
* @param content
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] sign(byte[] content, PrivateKey privateKey) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] bytes = md.digest(content);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] encryptBytes = cipher.doFinal(bytes);
return encryptBytes;
}

/**
* 验证签名是否正确
* @param content 内容
* @param sign 签名
* @param publicKey 公钥
* @return
* @throws Exception
*/
public static boolean verify(byte[] content, byte[] sign, PublicKey publicKey) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] bytes = md.digest(content);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE,publicKey);
byte[] decryptBytes = cipher.doFinal(sign);
return byte2base64(decryptBytes).equals(byte2base64(bytes));
}

//基于Signature API的使用
public static byte[] sign(byte[] content, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initSign(privateKey);
signature.update(content);
return signature.sign();
}

public static boolean verify(byte[] content, byte[] sign, PublicKey publicKey) throws Exception {
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initVerify(publicKey);
signature.update(content);
return signature.verify(sign);
}
}