commit | author | age
|
d6404a
|
1 |
package com.javen.common; |
Z |
2 |
|
|
3 |
import org.apache.commons.codec.binary.Base64; |
|
4 |
import org.apache.commons.codec.digest.DigestUtils; |
|
5 |
|
|
6 |
import javax.crypto.BadPaddingException; |
|
7 |
import javax.crypto.Cipher; |
|
8 |
import javax.crypto.SecretKey; |
|
9 |
import javax.crypto.spec.SecretKeySpec; |
|
10 |
import java.security.MessageDigest; |
|
11 |
import java.security.NoSuchAlgorithmException; |
|
12 |
import java.security.SecureRandom; |
|
13 |
import java.security.Security; |
|
14 |
import java.util.Arrays; |
|
15 |
import java.util.Objects; |
|
16 |
|
|
17 |
/** |
|
18 |
* 编码加密工具类 |
|
19 |
*/ |
|
20 |
public class Des1 { |
|
21 |
|
|
22 |
private Des1() { |
|
23 |
} |
|
24 |
|
|
25 |
/** Base64 编码 */ |
|
26 |
private static final Base64 B64 = new Base64(); |
|
27 |
/** 安全的随机数源 */ |
|
28 |
private static final SecureRandom RANDOM = new SecureRandom(); |
|
29 |
/** SHA-1加密 */ |
|
30 |
private static MessageDigest SHA_1 = null; |
|
31 |
|
|
32 |
static { |
|
33 |
init(); |
|
34 |
} |
|
35 |
|
|
36 |
/** 初始化 */ |
|
37 |
private static void init() { |
|
38 |
try { |
|
39 |
SHA_1 = MessageDigest.getInstance("SHA-1"); |
|
40 |
} catch (NoSuchAlgorithmException e) { |
|
41 |
throw new IllegalStateException(e); |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* SHA-1加密 |
|
47 |
* |
|
48 |
* @param str |
|
49 |
* 明文 |
|
50 |
* @return 密文 |
|
51 |
*/ |
|
52 |
public static String sha1(String str) { |
|
53 |
return new String(B64.encode(SHA_1.digest(str.getBytes()))); |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* SHA-1加密(Url安全) |
|
58 |
* |
|
59 |
* @param str |
|
60 |
* 明文 |
|
61 |
* @return 密文 |
|
62 |
*/ |
|
63 |
public static String sha1Url(String str) { |
|
64 |
return new String(Base64.encodeBase64URLSafeString(SHA_1.digest(str.getBytes()))); |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* Base64编码 |
|
69 |
* |
|
70 |
* @param bs |
|
71 |
* byte数组 |
|
72 |
* @return 编码后的byte数组 |
|
73 |
*/ |
|
74 |
public static byte[] b64Encode(byte[] bs) { |
|
75 |
return B64.encode(bs); |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* Base64编码字符串 |
|
80 |
* |
|
81 |
* @param str |
|
82 |
* 需要编码的字符串 |
|
83 |
* @return 编码后的字符串 |
|
84 |
*/ |
|
85 |
public static String b64Encode(String str) { |
|
86 |
if (null != str) { |
|
87 |
return new String(B64.encode(str.getBytes())); |
|
88 |
} |
|
89 |
return null; |
|
90 |
} |
|
91 |
|
|
92 |
/** |
|
93 |
* Base64编码字符串(Url安全) |
|
94 |
* |
|
95 |
* @param str |
|
96 |
* 需要编码的字符串 |
|
97 |
* @return 编码后的字符串 |
|
98 |
*/ |
|
99 |
public static String b64Url(String str) { |
|
100 |
if (null != str) { |
|
101 |
return Base64.encodeBase64URLSafeString(str.getBytes()); |
|
102 |
} |
|
103 |
return null; |
|
104 |
} |
|
105 |
|
|
106 |
/** |
|
107 |
* Base64解码 |
|
108 |
* |
|
109 |
* @param bs |
|
110 |
* byte数组 |
|
111 |
* @return 解码后的byte数组 |
|
112 |
*/ |
|
113 |
public static byte[] b64Decode(byte[] bs) { |
|
114 |
return B64.decode(bs); |
|
115 |
} |
|
116 |
|
|
117 |
/** |
|
118 |
* Base64解码字符串 |
|
119 |
* |
|
120 |
* @param str |
|
121 |
* 需要解码的字符串 |
|
122 |
* @return 解码后的字符串 |
|
123 |
*/ |
|
124 |
public static String b64Decode(String str) { |
|
125 |
if (null != str) { |
|
126 |
byte[] bs = B64.decode(str.getBytes()); |
|
127 |
if (null != bs) { |
|
128 |
return new String(bs); |
|
129 |
} |
|
130 |
} |
|
131 |
return null; |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* 生成32位MD5密文 |
|
136 |
* |
|
137 |
* <pre> |
|
138 |
* org.apache.commons.codec.digest.DigestUtils |
|
139 |
* </pre> |
|
140 |
* |
|
141 |
* @param str |
|
142 |
* 明文 |
|
143 |
* @return 密文 |
|
144 |
*/ |
|
145 |
public static String md5(String str) { |
|
146 |
if (null != str) { |
|
147 |
return DigestUtils.md5Hex(str); |
|
148 |
} |
|
149 |
return null; |
|
150 |
} |
|
151 |
|
|
152 |
/** DES加密算法 */ |
|
153 |
private static final String DES_ALGORITHM = "DESede"; // 可用 DES,DESede,Blowfish |
|
154 |
/** DES默认加密 */ |
|
155 |
private static Cipher DES_CIPHER_ENC = null; |
|
156 |
/** DES默认解密 */ |
|
157 |
private static Cipher DES_CIPHER_DEC = null; |
|
158 |
private static String AESECBPKCS5Padding = "AES/ECB/PKCS5Padding"; |
|
159 |
|
|
160 |
static { |
|
161 |
// 添加JCE算法 |
|
162 |
Security.addProvider(new com.sun.crypto.provider.SunJCE()); |
|
163 |
// 初始化默认DES加密 |
|
164 |
try { |
|
165 |
// 密钥 |
|
166 |
SecretKey desKey = new SecretKeySpec(new byte[] { 0x11, 0x22, 0x4F, 0x58, (byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB, (byte) 0xDD, 0x55, 0x66, 0x77, 0x29, 0x74, |
|
167 |
(byte) 0x98, 0x30, 0x40, 0x36, (byte) 0xE2 }, DES_ALGORITHM); |
|
168 |
// 初始化默认加密 |
|
169 |
DES_CIPHER_ENC = Cipher.getInstance(DES_ALGORITHM); |
|
170 |
DES_CIPHER_ENC.init(Cipher.ENCRYPT_MODE, desKey, RANDOM); |
|
171 |
// 初始化默认解密 |
|
172 |
DES_CIPHER_DEC = Cipher.getInstance(DES_ALGORITHM); |
|
173 |
DES_CIPHER_DEC.init(Cipher.DECRYPT_MODE, desKey, RANDOM); |
|
174 |
} catch (Exception e) { |
|
175 |
System.err.println("DES默认加密解密初始化失败:" + e.getMessage()); |
|
176 |
} |
|
177 |
} |
|
178 |
|
|
179 |
/** |
|
180 |
* DES加密(默认密钥) |
|
181 |
* |
|
182 |
* |
|
183 |
* @param strings |
|
184 |
* @param str |
|
185 |
* 需要加密的明文 |
|
186 |
* @return 加密后的密文(base64编码字符串) |
|
187 |
*/ |
|
188 |
public static String desEncryp(String[] strings, String str) { |
|
189 |
return desEncryp(str, false); |
|
190 |
} |
|
191 |
|
|
192 |
/** |
|
193 |
* DES加密(默认密钥) |
|
194 |
* |
|
195 |
* @param str |
|
196 |
* 需要加密的明文 |
|
197 |
* @return 加密后的密文(base64编码字符串,Url安全) |
|
198 |
*/ |
|
199 |
public static String desEncrypUrl(String str) { |
|
200 |
return desEncryp(str, true); |
|
201 |
} |
|
202 |
|
|
203 |
/** |
|
204 |
* DES加密(默认密钥) |
|
205 |
* |
|
206 |
* @param str |
|
207 |
* 需要加密的明文 |
|
208 |
* @param urlSafety |
|
209 |
* 密文是否需要Url安全 |
|
210 |
* @return 加密后的密文(str为null返回null) |
|
211 |
*/ |
|
212 |
public static String desEncryp(String str, boolean urlSafety) { |
|
213 |
if (null != str) { |
|
214 |
try { |
|
215 |
byte[] bytes = DES_CIPHER_ENC.doFinal(str.getBytes("UTF-8"));// 加密 |
|
216 |
if (urlSafety) { |
|
217 |
return Base64.encodeBase64URLSafeString(bytes); |
|
218 |
} else { |
|
219 |
return new String(B64.encode(bytes)); |
|
220 |
} |
|
221 |
} catch (Exception e) { |
|
222 |
System.err.println("DES加密失败, 密文:" + str + ", 错误:" + e.getMessage()); |
|
223 |
} |
|
224 |
} |
|
225 |
return null; |
|
226 |
} |
|
227 |
|
|
228 |
/** |
|
229 |
* DES解密(默认密钥) |
|
230 |
* |
|
231 |
* @param str |
|
232 |
* 需要解密的密文(base64编码字符串) |
|
233 |
* @return 解密后的明文 |
|
234 |
*/ |
|
235 |
public static String desDecrypt(String str) { |
|
236 |
if (null != str) { |
|
237 |
try { |
|
238 |
byte[] bytes = DES_CIPHER_DEC.doFinal(B64.decode(str));// 解密 |
|
239 |
return new String(bytes, "UTF-8"); |
|
240 |
} catch (Exception e) { |
|
241 |
System.err.println("DES解密失败, 密文:" + str + ", 错误:" + e.getMessage()); |
|
242 |
} |
|
243 |
} |
|
244 |
return null; |
|
245 |
} |
|
246 |
|
|
247 |
/** |
|
248 |
* DES加密 |
|
249 |
* |
|
250 |
* @param str |
|
251 |
* 需要加密的明文 |
|
252 |
* @param key |
|
253 |
* 密钥(长度小于24字节自动补足,大于24取前24字节) |
|
254 |
* @return 加密后的密文(base64编码字符串) |
|
255 |
*/ |
|
256 |
public static String desEncryp(String str, String key) {//加密 |
|
257 |
return desEncryp(str, key, false); |
|
258 |
} |
|
259 |
|
|
260 |
|
|
261 |
/** |
|
262 |
* DES加密 |
|
263 |
* |
|
264 |
* @param str |
|
265 |
* 需要加密的明文 |
|
266 |
* @param key |
|
267 |
* 密钥(长度小于24字节自动补足,大于24取前24字节) |
|
268 |
* @return 加密后的密文(base64编码字符串,Url安全) |
|
269 |
*/ |
|
270 |
public static String desEncrypUrl(String str, String key) { |
|
271 |
return desEncryp(str, key, true); |
|
272 |
} |
|
273 |
|
|
274 |
/** |
|
275 |
* DES加密 |
|
276 |
* |
|
277 |
* @param str |
|
278 |
* 需要加密的明文 |
|
279 |
* @param key |
|
280 |
* 密钥(长度小于24字节自动补足,大于24取前24字节) |
|
281 |
* @param urlSafety |
|
282 |
* 密文是否需要Url安全 |
|
283 |
* @return 加密后的密文(str/key为null返回null) |
|
284 |
*/ |
|
285 |
public static String desEncryp(String str, String key, boolean urlSafety) { |
|
286 |
if (null != str && null != key) { |
|
287 |
try { |
|
288 |
Cipher c = Cipher.getInstance(DES_ALGORITHM); |
|
289 |
c.init(Cipher.ENCRYPT_MODE, desKey(key), RANDOM); |
|
290 |
// 加密 |
|
291 |
byte[] bytes = c.doFinal(str.getBytes("UTF-8"));// 加密 |
|
292 |
// 返回b64处理后的字符串 |
|
293 |
if (urlSafety) { |
|
294 |
return Base64.encodeBase64URLSafeString(bytes); |
|
295 |
} else { |
|
296 |
return new String(B64.encode(bytes)); |
|
297 |
} |
|
298 |
} catch (Exception e) { |
|
299 |
System.err.println("DES加密失败, 密文:" + str + ", key:" + key + ", 错误:" + e.getMessage()); |
|
300 |
} |
|
301 |
} |
|
302 |
return null; |
|
303 |
} |
|
304 |
|
|
305 |
/** |
|
306 |
* DES解密 |
|
307 |
* |
|
308 |
* @param str |
|
309 |
* 需要解密的密文(base64编码字符串) |
|
310 |
* @param key |
|
311 |
* 密钥(长度小于24字节自动补足,大于24取前24字节) |
|
312 |
* @return 解密后的明文 |
|
313 |
*/ |
|
314 |
public static String desDecrypt(String str, String key) {//解密 |
|
315 |
|
|
316 |
if (null != str && null != key) { |
|
317 |
try { |
|
318 |
Cipher c = Cipher.getInstance(DES_ALGORITHM); |
|
319 |
c.init(Cipher.DECRYPT_MODE, desKey(key), RANDOM); |
|
320 |
byte[] bytes = c.doFinal(B64.decode(str)); |
|
321 |
String a = new String(bytes, "UTF-8"); |
|
322 |
return a!=null && !Objects.equals(a,"")?a:str; |
|
323 |
} catch (BadPaddingException e) { |
|
324 |
return str; |
|
325 |
// System.err.println("DES解密失败, 密文:" + str + ", key:" + key + ", 错误:" + e.getMessage()); |
|
326 |
} catch (Exception e) { |
|
327 |
return str; |
|
328 |
// System.err.println("DES解密失败, 密文:" + str + ", key:" + key + ", 错误:" + e.getMessage()); |
|
329 |
} |
|
330 |
} |
|
331 |
// System.err.println("解密失败str"+str); |
|
332 |
return str; |
|
333 |
} |
|
334 |
|
|
335 |
/** DES密钥 */ |
|
336 |
private static SecretKey desKey(String key) { |
|
337 |
byte[] bs = key.getBytes(); |
|
338 |
if (bs.length != 24) { |
|
339 |
bs = Arrays.copyOf(bs, 24);// 处理数组长度为24 |
|
340 |
} |
|
341 |
return new SecretKeySpec(bs, DES_ALGORITHM); |
|
342 |
} |
|
343 |
|
|
344 |
/** AES加密算法 */ |
|
345 |
private static final String AES_ALGORITHM = "AES"; |
|
346 |
|
|
347 |
/** |
|
348 |
* AES加密 |
|
349 |
* |
|
350 |
* @param str |
|
351 |
* 需要加密的明文 |
|
352 |
* @param key |
|
353 |
* 密钥 |
|
354 |
* @return 加密后的密文(str/key为null返回null) |
|
355 |
*/ |
|
356 |
public static String aesEncryp(String str, String key) { |
|
357 |
return aesEncryp(str, key, false); |
|
358 |
} |
|
359 |
|
|
360 |
/** |
|
361 |
* AES加密 |
|
362 |
* |
|
363 |
* @param str |
|
364 |
* 需要加密的明文 |
|
365 |
* @param key |
|
366 |
* 密钥 |
|
367 |
* @param urlSafety |
|
368 |
* 密文是否需要Url安全 |
|
369 |
* @return 加密后的密文(str/key为null返回null) |
|
370 |
*/ |
|
371 |
public static String aesEncryp(String str, String key, boolean urlSafety) { |
|
372 |
if (null != str && null != key) { |
|
373 |
try { |
|
374 |
Cipher c = Cipher.getInstance(AESECBPKCS5Padding); |
|
375 |
c.init(Cipher.ENCRYPT_MODE, aesKey(key), RANDOM); |
|
376 |
byte[] bytes = c.doFinal(str.getBytes("UTF-8"));// 加密 |
|
377 |
if (urlSafety) { |
|
378 |
return Base64.encodeBase64URLSafeString(bytes); |
|
379 |
} else { |
|
380 |
return new String(B64.encode(bytes)); |
|
381 |
} |
|
382 |
} catch (Exception e) { |
|
383 |
System.err.println("AES加密失败, 密文:" + str + ", key:" + key + ", 错误:" + e.getMessage()); |
|
384 |
} |
|
385 |
} |
|
386 |
return null; |
|
387 |
} |
|
388 |
|
|
389 |
/** |
|
390 |
* AES解密 |
|
391 |
* |
|
392 |
* @param str |
|
393 |
* 需要解密的密文(base64编码字符串) |
|
394 |
* @param key |
|
395 |
* 密钥 |
|
396 |
* @return 解密后的明文 |
|
397 |
*/ |
|
398 |
public static String aesDecrypt(String str, String key) { |
|
399 |
if (null != str && null != key) { |
|
400 |
try { |
|
401 |
Cipher c = Cipher.getInstance(AESECBPKCS5Padding); |
|
402 |
c.init(Cipher.DECRYPT_MODE, aesKey(key), RANDOM); |
|
403 |
return new String(c.doFinal(B64.decode(str)), "UTF-8");// 解密 |
|
404 |
} catch (BadPaddingException e) { |
|
405 |
System.err.println("AES解密失败, 密文:" + str + ", key:" + key + ", 错误:" + e.getMessage()); |
|
406 |
} catch (Exception e) { |
|
407 |
System.err.println("AES解密失败, 密文:" + str + ", key:" + key + ", 错误:" + e.getMessage()); |
|
408 |
} |
|
409 |
} |
|
410 |
return null; |
|
411 |
} |
|
412 |
|
|
413 |
/** AES密钥 */ |
|
414 |
private static SecretKeySpec aesKey(String key) { |
|
415 |
byte[] bs = key.getBytes(); |
|
416 |
if (bs.length != 16) { |
|
417 |
bs = Arrays.copyOf(bs, 16);// 处理数组长度为16 |
|
418 |
} |
|
419 |
return new SecretKeySpec(bs, AES_ALGORITHM); |
|
420 |
} |
|
421 |
|
|
422 |
|
|
423 |
} |