|
15 | 15 | */
|
16 | 16 | package com.worthed.util;
|
17 | 17 |
|
| 18 | +import java.io.IOException; |
18 | 19 | import java.security.MessageDigest;
|
| 20 | +import java.security.SecureRandom; |
| 21 | + |
| 22 | +import javax.crypto.Cipher; |
| 23 | +import javax.crypto.SecretKey; |
| 24 | +import javax.crypto.SecretKeyFactory; |
| 25 | +import javax.crypto.spec.DESKeySpec; |
| 26 | + |
| 27 | +import sun.misc.BASE64Decoder; |
| 28 | +import sun.misc.BASE64Encoder; |
19 | 29 |
|
20 | 30 | /**
|
21 | 31 | * DigestUtils
|
22 | 32 | *
|
| 33 | + * 此工具类需要sun的jar包(%JAVA_HOME%\jre\lib\rt.jar) |
| 34 | + * |
23 | 35 | * @author jingle1267@163.com
|
24 | 36 | */
|
25 | 37 | public class DigestUtils {
|
26 | 38 |
|
27 |
| - /** |
28 |
| - * Used to build output as Hex |
29 |
| - */ |
30 |
| - private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', |
31 |
| - 'e', 'f' }; |
32 |
| - |
33 |
| - /** |
34 |
| - * encode By MD5 |
35 |
| - * |
36 |
| - * @param str |
37 |
| - * @return String |
38 |
| - */ |
39 |
| - public static String md5(String str) { |
40 |
| - if (str == null) { |
41 |
| - return null; |
42 |
| - } |
43 |
| - try { |
44 |
| - MessageDigest messageDigest = MessageDigest.getInstance("MD5"); |
45 |
| - messageDigest.update(str.getBytes()); |
46 |
| - return new String(encodeHex(messageDigest.digest())); |
47 |
| - } catch (Exception e) { |
48 |
| - throw new RuntimeException(e); |
49 |
| - } |
50 |
| - } |
51 |
| - |
52 |
| - /** |
53 |
| - * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. |
54 |
| - * The returned array will be double the length of the passed array, as it takes two characters to represent any |
55 |
| - * given byte. |
56 |
| - * |
57 |
| - * @param data a byte[] to convert to Hex characters |
58 |
| - * @return A char[] containing hexadecimal characters |
59 |
| - */ |
60 |
| - protected static char[] encodeHex(final byte[] data) { |
61 |
| - final int l = data.length; |
62 |
| - final char[] out = new char[l << 1]; |
63 |
| - // two characters form the hex value. |
64 |
| - for (int i = 0, j = 0; i < l; i++) { |
65 |
| - out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4]; |
66 |
| - out[j++] = DIGITS_LOWER[0x0F & data[i]]; |
67 |
| - } |
68 |
| - return out; |
69 |
| - } |
| 39 | + private final static String DES = "DES"; |
| 40 | + |
| 41 | + /** |
| 42 | + * Used to build output as Hex |
| 43 | + */ |
| 44 | + private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', |
| 45 | + '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
| 46 | + |
| 47 | + /** |
| 48 | + * encode By MD5 |
| 49 | + * |
| 50 | + * @param str |
| 51 | + * @return String |
| 52 | + */ |
| 53 | + public static String md5(String str) { |
| 54 | + if (str == null) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + try { |
| 58 | + MessageDigest messageDigest = MessageDigest.getInstance("MD5"); |
| 59 | + messageDigest.update(str.getBytes()); |
| 60 | + return new String(encodeHex(messageDigest.digest())); |
| 61 | + } catch (Exception e) { |
| 62 | + throw new RuntimeException(e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Converts an array of bytes into an array of characters representing the |
| 68 | + * hexadecimal values of each byte in order. The returned array will be |
| 69 | + * double the length of the passed array, as it takes two characters to |
| 70 | + * represent any given byte. |
| 71 | + * |
| 72 | + * @param data |
| 73 | + * a byte[] to convert to Hex characters |
| 74 | + * @return A char[] containing hexadecimal characters |
| 75 | + */ |
| 76 | + protected static char[] encodeHex(final byte[] data) { |
| 77 | + final int l = data.length; |
| 78 | + final char[] out = new char[l << 1]; |
| 79 | + // two characters form the hex value. |
| 80 | + for (int i = 0, j = 0; i < l; i++) { |
| 81 | + out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4]; |
| 82 | + out[j++] = DIGITS_LOWER[0x0F & data[i]]; |
| 83 | + } |
| 84 | + return out; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Description 根据键值进行加密 |
| 89 | + * |
| 90 | + * @param data |
| 91 | + * @param key |
| 92 | + * 加密键byte数组 |
| 93 | + * @return |
| 94 | + * @throws Exception |
| 95 | + */ |
| 96 | + public static String encrypt(String data, String key) throws Exception { |
| 97 | + byte[] bt = encrypt(data.getBytes(), key.getBytes()); |
| 98 | + String strs = new BASE64Encoder().encode(bt); |
| 99 | + return strs; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Description 根据键值进行解密 |
| 104 | + * |
| 105 | + * @param data |
| 106 | + * @param key |
| 107 | + * 加密键byte数组 |
| 108 | + * @return |
| 109 | + * @throws IOException |
| 110 | + * @throws Exception |
| 111 | + */ |
| 112 | + public static String decrypt(String data, String key) throws IOException, |
| 113 | + Exception { |
| 114 | + if (data == null) |
| 115 | + return null; |
| 116 | + BASE64Decoder decoder = new BASE64Decoder(); |
| 117 | + byte[] buf = decoder.decodeBuffer(data); |
| 118 | + byte[] bt = decrypt(buf, key.getBytes()); |
| 119 | + return new String(bt); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Description 根据键值进行加密 |
| 124 | + * |
| 125 | + * @param data |
| 126 | + * @param key |
| 127 | + * 加密键byte数组 |
| 128 | + * @return |
| 129 | + * @throws Exception |
| 130 | + */ |
| 131 | + private static byte[] encrypt(byte[] data, byte[] key) throws Exception { |
| 132 | + // 生成一个可信任的随机数源 |
| 133 | + SecureRandom sr = new SecureRandom(); |
| 134 | + |
| 135 | + // 从原始密钥数据创建DESKeySpec对象 |
| 136 | + DESKeySpec dks = new DESKeySpec(key); |
| 137 | + |
| 138 | + // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 |
| 139 | + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); |
| 140 | + SecretKey securekey = keyFactory.generateSecret(dks); |
| 141 | + |
| 142 | + // Cipher对象实际完成加密操作 |
| 143 | + Cipher cipher = Cipher.getInstance(DES); |
| 144 | + |
| 145 | + // 用密钥初始化Cipher对象 |
| 146 | + cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); |
| 147 | + |
| 148 | + return cipher.doFinal(data); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Description 根据键值进行解密 |
| 153 | + * |
| 154 | + * @param data |
| 155 | + * @param key |
| 156 | + * 加密键byte数组 |
| 157 | + * @return |
| 158 | + * @throws Exception |
| 159 | + */ |
| 160 | + private static byte[] decrypt(byte[] data, byte[] key) throws Exception { |
| 161 | + // 生成一个可信任的随机数源 |
| 162 | + SecureRandom sr = new SecureRandom(); |
| 163 | + |
| 164 | + // 从原始密钥数据创建DESKeySpec对象 |
| 165 | + DESKeySpec dks = new DESKeySpec(key); |
| 166 | + |
| 167 | + // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 |
| 168 | + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); |
| 169 | + SecretKey securekey = keyFactory.generateSecret(dks); |
| 170 | + |
| 171 | + // Cipher对象实际完成解密操作 |
| 172 | + Cipher cipher = Cipher.getInstance(DES); |
| 173 | + |
| 174 | + // 用密钥初始化Cipher对象 |
| 175 | + cipher.init(Cipher.DECRYPT_MODE, securekey, sr); |
| 176 | + |
| 177 | + return cipher.doFinal(data); |
| 178 | + } |
| 179 | + |
70 | 180 | }
|
0 commit comments