@@ -209,4 +209,128 @@ public static void main(String[] args) {
209209 - aes -> 16 字节
210210 - 加解密使用的初始化向量相同
211211
212- ## DES
212+ - DES 加密示例代码如下:
213+ - DES 加密,key的大小必须是8 个字节
214+ - 如果没有指定分组密码模式和填充模式,ECB/PKCS5Padding就是默认值
215+ - 如果指定分组密码模式为CBC,必须指定初始向量,初始向量中密钥的长度必须是8 个字节
216+ - 如果没有指定填充模式为NoPadding模式,原文的长度必须是8 个字节的整倍数
217+ ` ` ` $xslt
218+ public class DESDemo {
219+ // DES加密算法,key的大小必须是8个字节
220+ public static void main(String[] args) throws Exception {
221+ String input = "frank";
222+ String key = "12345678";
223+ // 指定获取Cipher的算法,如果没有指定分组密码模式和填充模式,ECB/PKCS5Padding就是默认值
224+ // CBC模式,必须指定初始向量,初始向量中密钥的长度必须是8个字节
225+ // NoPadding模式,原文的长度必须是8个字节的整倍数
226+ // String transformation = "DES";
227+ String transformation = "DES/CBC/PKCS5Padding";
228+ // 指定获取密钥的算法
229+ String algorithm = "DES";
230+ String encryptDES = DESEncrypt(input, key, transformation, algorithm);
231+ System.out.println("加密:" + encryptDES);
232+ String s = DESDecrypt(encryptDES, key, transformation, algorithm);
233+ System.out.println("解密:" + s);
234+ }
235+
236+ /**
237+ *
238+ * @param input 明文
239+ * @param key 密钥(DES,密钥的长度必须是8个字节)
240+ * @param transformation 获取Cipher对象的算法
241+ * @param algorithm 获取密钥的算法
242+ * @return 返回密文
243+ * @throws Exception
244+ */
245+ private static String DESEncrypt(String input, String key, String transformation, String algorithm) throws Exception {
246+ // 1,获取Cipher对象
247+ Cipher cipher = Cipher.getInstance(transformation);
248+ // 指定密钥规则
249+ SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
250+ // 2.初始化向量的秘钥长度需要根据算法而定,des 8个字节长度 aes 16个字节长度
251+ //这里为了方便,统一使用传来的秘钥
252+ IvParameterSpec iv = new IvParameterSpec(key.getBytes());
253+ cipher.init(Cipher.ENCRYPT_MODE, sks, iv);
254+ // cipher.init(Cipher.ENCRYPT_MODE, sks);
255+ // 3. 加密
256+ byte[] bytes = cipher.doFinal(input.getBytes());
257+ // 对数据进行Base64编码
258+ String encode = Base64.encode(bytes);
259+ return encode;
260+ }
261+
262+
263+ private static String DESDecrypt(String input, String key, String transformation, String algorithm) throws Exception {
264+ Cipher cipher = Cipher.getInstance(transformation);
265+ SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
266+ IvParameterSpec iv = new IvParameterSpec(key.getBytes());
267+ cipher.init(Cipher.DECRYPT_MODE, sks, iv);
268+ // cipher.init(Cipher.DECRYPT_MODE, sks);
269+ byte[] bytes = cipher.doFinal(Base64.decode(input));
270+ return new String(bytes);
271+ }
272+ }
273+ ` ` `
274+ - AES 加密示例代码如下(AES和DES加密高度类似)
275+ - AES 加密,key的大小必须是16 个字节
276+ - 如果没有指定分组密码模式和填充模式,ECB/PKCS5Padding就是默认值
277+ - 如果没有指定分组密码模式为CBC,必须指定初始向量,初始向量中密钥的长度必须是16 个字节
278+ - 如果没有指定填充模式为NoPadding模式,原文的长度必须是16 个字节的整倍数
279+ ` ` ` $xslt
280+ public class AESDemo {
281+ // AES加密算法,key的大小必须是16个字节
282+ public static void main(String[] args) throws Exception {
283+ String input = "frank";
284+ String key = "1234567887654321";
285+ // 指定获取Cipher的算法,如果没有指定分组密码模式和填充模式,ECB/PKCS5Padding就是默认值
286+ // CBC模式,必须指定初始向量,初始向量中密钥的长度必须是16个字节
287+ // NoPadding模式,原文的长度必须是16个字节的整倍数
288+ // String transformation = "AES";
289+ String transformation = "AES/CBC/PKCS5Padding";
290+ // 指定获取密钥的算法
291+ String algorithm = "AES";
292+ String encryptAES = AESEncrypt(input, key, transformation, algorithm);
293+ System.out.println("加密:" + encryptAES);
294+ String s = AESDecrypt(encryptAES, key, transformation, algorithm);
295+ System.out.println("解密:" + s);
296+ }
297+
298+ /**
299+ *
300+ * @param input 明文
301+ * @param key 密钥(AES,密钥的长度必须是16个字节)
302+ * @param transformation 获取Cipher对象的算法
303+ * @param algorithm 获取密钥的算法
304+ * @return 返回密文
305+ * @throws Exception
306+ */
307+ private static String AESEncrypt(String input, String key, String transformation, String algorithm) throws Exception {
308+ // 1,获取Cipher对象
309+ Cipher cipher = Cipher.getInstance(transformation);
310+ // 指定密钥规则
311+ SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
312+ // 2.初始化向量的秘钥长度需要根据算法而定,des 8个字节长度 aes 16个字节长度
313+ //这里为了方便,统一使用传来的秘钥
314+ IvParameterSpec iv = new IvParameterSpec(key.getBytes());
315+ cipher.init(Cipher.ENCRYPT_MODE, sks, iv);
316+ // cipher.init(Cipher.ENCRYPT_MODE, sks);
317+ // 3. 加密
318+ byte[] bytes = cipher.doFinal(input.getBytes());
319+ // 对数据进行Base64编码
320+ String encode = Base64.encode(bytes);
321+ return encode;
322+ }
323+
324+
325+ private static String AESDecrypt(String input, String key, String transformation, String algorithm) throws Exception {
326+ Cipher cipher = Cipher.getInstance(transformation);
327+ SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
328+ IvParameterSpec iv = new IvParameterSpec(key.getBytes());
329+ cipher.init(Cipher.DECRYPT_MODE, sks, iv);
330+ // cipher.init(Cipher.DECRYPT_MODE, sks);
331+ byte[] bytes = cipher.doFinal(Base64.decode(input));
332+ return new String(bytes);
333+ }
334+ }
335+
336+ ` ` `
0 commit comments