Browse Source

测试DES加密解密

lcl 1 year ago
parent
commit
c95370451c

+ 27 - 16
src/main/java/com/kxs/adminap/controller/TestController.java

@@ -3,6 +3,7 @@ package com.kxs.adminap.controller;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Base64;
 import java.util.Dictionary;
 import java.util.List;
 
@@ -15,7 +16,9 @@ import com.kxs.adminap.config.Base;
 // import com.kxs.adminap.dao.TestTablerepository;
 // import com.kxs.adminap.enity.TestTable;
 import com.kxs.adminap.service.DbConn;
+import com.kxs.adminap.util.DesUtil;
 import com.kxs.adminap.util.RedisUtils;
+import com.kxs.adminap.util.Util;
 
 import jakarta.annotation.Resource;
 
@@ -34,30 +37,38 @@ public class TestController {
 		return result;
 	}
 
-	// @RequestMapping("/test")
-	// public String test() {
-	// 	String result = "ok";
+	@RequestMapping("/test")
+	public String test() {
+		String result = "ok";
 		
-	// 	// redisUtils.set("javaset", "ohyeah");
-	// 	// result += redisUtils.get("javaset");
+		// redisUtils.set("javaset", "ohyeah");
+		// result += redisUtils.get("javaset");
 
-	// 	// redisUtils.set("hahaha", "咔咔咔咔");
-	// 	// result += redisUtils.get("hahaha");
+		// redisUtils.set("hahaha", "咔咔咔咔");
+		// result += redisUtils.get("hahaha");
 
-	// 	// redisUtils.set("kkk", 123);
-	// 	// result += redisUtils.getInteger("kkk");
+		// redisUtils.set("kkk", 123);
+		// result += redisUtils.getInteger("kkk");
 
-	// 	TestTable d = testTablerepository.findById(2).get();
-	// 	redisUtils.addList("test:list", d);
+		// TestTable d = testTablerepository.findById(2).get();
+		// redisUtils.addList("test:list", d);
 
-	// 	return result;
-	// }
+		String json = "{\"MerchantId\":12}";
+		try {
+			json = DesUtil.encrypt(json, "frgt7890", "utf8");
+			json = DesUtil.decrypt("bYyEqmuNJyX5TAhNNjyL0A==", "*ga34|^7", "utf8");
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
 
-	@RequestMapping("/test2")
-	public List<Object> test2() {
-		return redisUtils.getList("test:list");
+		return result;
 	}
 
+	// @RequestMapping("/test2")
+	// public List<Object> test2() {
+	// 	return redisUtils.getList("test:list");
+	// }
+
 	// @RequestMapping("/test3")
 	// public TestTable test3() {
 	// 	return (TestTable)redisUtils.getList("test:list").get(0);

+ 104 - 0
src/main/java/com/kxs/adminap/util/DesUtil.java

@@ -0,0 +1,104 @@
+package com.kxs.adminap.util;
+
+import java.nio.charset.StandardCharsets;
+import java.security.Key;
+import java.security.SecureRandom;
+import java.util.Base64;
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.DESedeKeySpec;
+import javax.crypto.spec.IvParameterSpec;
+
+public class DesUtil {
+    public static Key setKey(String strKey) {
+        Key key = null;
+        try {
+            KeyGenerator generator = KeyGenerator.getInstance("DES");
+            generator.init(new SecureRandom(strKey.getBytes())); // 根据参数生成key
+            key = generator.generateKey();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return key;
+    }
+
+    /**
+     * @param source
+     *                编码内容
+     * @param key
+     *                密钥
+     * @param charSet
+     *                编码格式
+     * @return
+     */
+    public static String encrypt(String source, String key, String charSet) {
+        String encrypt = null;
+        try {
+            byte[] ret = encrypt(source.getBytes(charSet), key);
+            encrypt = new String(Base64.getEncoder().encode(ret));
+        } catch (Exception e) {
+            e.printStackTrace();
+            encrypt = null;
+        }
+        return encrypt;
+    }
+
+    /**
+     * @param encryptedData
+     *                      解码内容
+     * @param key
+     *                      密钥
+     * @param charSet
+     *                      编码格式
+     * @return
+     */
+    public static String decrypt(String encryptedData, String key, String charSet) {
+        // String descryptedData = null;
+        // try {
+        //     byte[] ret = descrypt(Base64.getDecoder().decode(encryptedData.getBytes(StandardCharsets.UTF_8)), key);
+        //     descryptedData = new String(ret, charSet);
+        // } catch (Exception e) {
+        //     e.printStackTrace();
+        //     descryptedData = null;
+        // }
+        // return descryptedData;
+        String result = "";
+        try {
+            Key desKey = setKey(key);
+            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
+            cipher.init(Cipher.DECRYPT_MODE, desKey);
+            byte[] base64Byte = Base64.getDecoder().decode(encryptedData);
+            byte[] decryptData = cipher.doFinal(base64Byte);
+            result = new String(decryptData, StandardCharsets.UTF_8);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return result;
+    }
+
+    private static byte[] encrypt(byte[] primaryData, String key) {
+        Key desKey = setKey(key);
+        try {
+            Cipher cipher = Cipher.getInstance("DES"); // Cipher对象实际完成加密操作
+            cipher.init(Cipher.ENCRYPT_MODE, desKey); // 用密钥初始化Cipher对象(加密)
+            return cipher.doFinal(primaryData);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    private static byte[] descrypt(byte[] encryptedData, String key) {
+        Key desKey = setKey(key);
+        try {
+            Cipher cipher = Cipher.getInstance("DES"); // Cipher对象实际完成解密操作
+            cipher.init(Cipher.DECRYPT_MODE, desKey); // 用密钥初始化Cipher对象(解密)
+            return cipher.doFinal(encryptedData);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+}

+ 9 - 0
src/main/java/com/kxs/adminap/util/Util.java

@@ -1,10 +1,19 @@
 package com.kxs.adminap.util;
 
+import java.nio.charset.StandardCharsets;
+import java.security.Key;
 import java.security.MessageDigest;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 
+import javax.crypto.Cipher;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.DESedeKeySpec;
+import javax.crypto.spec.IvParameterSpec;
+
+import org.apache.tomcat.util.codec.binary.Base64;
+
 public class Util {
     public static String CheckInt(Object str) {
         if(str instanceof Integer) {