· 7 years ago · Apr 06, 2018, 03:40 PM
1import java.security.Key;
2
3import javax.crypto.Cipher;
4import javax.crypto.KeyGenerator;
5import javax.crypto.SecretKey;
6import javax.crypto.SecretKeyFactory;
7import javax.crypto.spec.DESedeKeySpec;
8
9import org.apache.commons.codec.binary.Base64;
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13public class DESede {
14
15 private static final Logger log = LoggerFactory.getLogger(DESede.class);
16 /**
17 * 密鑰算法
18 */
19 private static final String KEY_ALGORITHM = "DESede";
20
21 //算法/模å¼/å¡«å……
22 // private static final String DEFAULT_CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";
23 private static final String DEFAULT_CIPHER_ALGORITHM = "DESede/ECB/ISO10126Padding";
24
25 /**
26 * åˆå§‹åŒ–密鑰
27 *
28 * @return byte[] 密鑰
29 * @throws Exception
30 */
31 public static byte[] initSecretKey() throws Exception {
32 // è¿”å›žç”ŸæˆæŒ‡å®šç®—法的秘密密鑰的 KeyGenerator å°è±¡
33 KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
34 // åˆå§‹åŒ–æ¤å¯†é‘°ç”Ÿæˆå™¨ï¼Œä½¿å…¶å…·æœ‰ç¢ºå®šçš„密鑰大å°
35 kg.init(168);
36 // 生æˆä¸€ä¸ªå¯†é‘°
37 SecretKey secretKey = kg.generateKey();
38 return secretKey.getEncoded();
39 }
40
41 /**
42 * 轉æ›å¯†é‘°
43 *
44 * @param key 二進制密鑰
45 * @return Key 密鑰
46 * @throws Exception
47 */
48 private static Key toKey(byte[] key) throws Exception {
49 // 實例化DES密鑰è¦å‰‡
50 DESedeKeySpec dks = new DESedeKeySpec(key);
51 // 實例化密鑰工å»
52 SecretKeyFactory skf = SecretKeyFactory.getInstance(KEY_ALGORITHM);
53 // 生æˆå¯†é‘°
54 SecretKey secretKey = skf.generateSecret(dks);
55 return secretKey;
56 }
57
58 /**
59 * åŠ å¯†
60 *
61 * @param data å¾…åŠ å¯†æ•¸æ“š
62 * @param key 密鑰
63 * @return byte[] åŠ å¯†æ•¸æ“š
64 * @throws Exception
65 */
66 public static byte[] encrypt(byte[] data, Key key) throws Exception {
67 return encrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
68 }
69
70 /**
71 * åŠ å¯†
72 *
73 * @param data å¾…åŠ å¯†æ•¸æ“š
74 * @param key 二進制密鑰
75 * @return byte[] åŠ å¯†æ•¸æ“š
76 * @throws Exception
77 */
78 public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
79 return encrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
80 }
81
82 /**
83 * åŠ å¯†
84 *
85 * @param data å¾…åŠ å¯†æ•¸æ“š
86 * @param key 二進制密鑰
87 * @param cipherAlgorithm åŠ å¯†ç®—æ³•/工作模å¼/填充方法
88 *
89 * @return byte[] åŠ å¯†æ•¸æ“š
90 * @throws Exception
91 */
92 public static byte[] encrypt(byte[] data, byte[] key, String cipherAlgorithm) throws Exception {
93 // 还原密鑰
94 Key k = toKey(key);
95 return encrypt(data, k, cipherAlgorithm);
96 }
97
98 /**
99 * åŠ å¯†
100 *
101 * @param data å¾…åŠ å¯†æ•¸æ“š
102 * @param key 密鑰
103 * @param cipherAlgorithm åŠ å¯†ç®—æ³•/工作模å¼/å¡«å……æ–¹å¼
104 * @return byte[] åŠ å¯†æ•¸æ“š
105 * @throws Exception
106 */
107 public static byte[] encrypt(byte[] data, Key key, String cipherAlgorithm) throws Exception {
108 // 實例化
109 Cipher cipher = Cipher.getInstance(cipherAlgorithm);
110 // 使用密鑰åˆå§‹åŒ–,è¨ç½®ç‚ºåŠ å¯†æ¨¡å¼
111 cipher.init(Cipher.ENCRYPT_MODE, key);
112 // 執行æ“作
113 return cipher.doFinal(data);
114 }
115
116 /**
117 * 解密
118 *
119 * @param data 待解密數據
120 * @param key 二進制密鑰
121 * @return byte[] 解密數據
122 * @throws Exception
123 */
124 public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
125 return decrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
126 }
127
128 /**
129 * 解密
130 *
131 * @param data 待解密數據
132 * @param key 密鑰
133 * @return byte[] 解密數據
134 * @throws Exception
135 */
136 public static byte[] decrypt(byte[] data, Key key) throws Exception {
137 return decrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
138 }
139
140 /**
141 * 解密
142 *
143 * @param data 待解密數據
144 * @param key 二進制密鑰
145 * @param cipherAlgorithm åŠ å¯†ç®—æ³•/工作模å¼/å¡«å……æ–¹å¼
146 * @return byte[] 解密數據
147 * @throws Exception
148 */
149 public static byte[] decrypt(byte[] data, byte[] key, String cipherAlgorithm) throws Exception {
150 // 还原密鑰
151 Key k = toKey(key);
152 return decrypt(data, k, cipherAlgorithm);
153 }
154
155 /**
156 * 解密
157 *
158 * @param data 待解密數據
159 * @param key 密鑰
160 * @param cipherAlgorithm åŠ å¯†ç®—æ³•/工作模å¼/å¡«å……æ–¹å¼
161 * @return byte[] 解密數據
162 * @throws Exception
163 */
164 public static byte[] decrypt(byte[] data, Key key, String cipherAlgorithm) throws Exception {
165 // 實例化
166 Cipher cipher = Cipher.getInstance(cipherAlgorithm);
167 // 使用密鑰åˆå§‹åŒ–,è¨ç½®ç‚ºè§£å¯†æ¨¡å¼
168 cipher.init(Cipher.DECRYPT_MODE, key);
169 // 執行æ“作
170 return cipher.doFinal(data);
171 }
172
173 private static String showByteArray(byte[] data) {
174 if (null == data) {
175 return null;
176 }
177 StringBuilder sb = new StringBuilder("{");
178 for (byte b : data) {
179 sb.append(b).append(",");
180 }
181 sb.deleteCharAt(sb.length() - 1);
182 sb.append("}");
183 return sb.toString();
184 }
185
186 //public String generateKey
187
188 public String encrypt(String key, String plainText) {
189 String cipherText = null;
190 try {
191 Key k = toKey(Base64.decodeBase64(key));
192 byte[] encryptData = encrypt(plainText.getBytes(), k);
193 cipherText = new String(Base64.encodeBase64(encryptData));
194 } catch (Exception e) {
195 log.error("encrypt error - Key:" + key + ", Plain Text: " + plainText, e);
196 }
197 return cipherText;
198 }
199
200 public String decrypt(String key, String cipherText) {
201 String plainText = null;
202 try {
203 Key k = toKey(Base64.decodeBase64(key));
204 byte[] decryptData = decrypt(Base64.decodeBase64(cipherText), k);
205 plainText = new String(decryptData);
206 } catch (Exception e) {
207 log.error("decrypt error - Key:" + key + ", Cipher Text: " + cipherText, e);
208 }
209 return plainText;
210 }
211
212}