Browse Source

测试解密

wuweihao 2 years ago
parent
commit
bad437d432

+ 32 - 2
720yun_fd_manage/gis_common/src/main/java/com/gis/common/util/EncryptUtils.java

@@ -15,6 +15,7 @@ import javax.crypto.SecretKeyFactory;
 import javax.crypto.spec.DESKeySpec;
 import javax.crypto.spec.IvParameterSpec;
 import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
 
 /**
  * 加密
@@ -30,11 +31,32 @@ public class EncryptUtils {
 
     private static IvParameterSpec iv = new IvParameterSpec(strParam.getBytes(StandardCharsets.UTF_8));
 
+    private static HashMap<String, Object> cipherMap = new HashMap<>();
+
+//    private static DESKeySpec getDesKeySpec(String source) throws Exception {
+//        if (source == null || source.length() == 0) {
+//            return null;
+//        }
+//        cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
+//        String strKey = "Passw0rd";
+//        return new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8));
+//    }
+
     private static DESKeySpec getDesKeySpec(String source) throws Exception {
         if (source == null || source.length() == 0) {
             return null;
         }
-        cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
+
+        // 2023-02-10 初始化cipher 避免大量实例化时候发生Cipher not initialized 异常
+        if (cipherMap.containsKey("cipher")){
+            cipher = (Cipher) cipherMap.get("cipher");
+        } else {
+            cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
+//            cipher = Cipher.getInstance("DES/ECB/NoPadding");
+            cipherMap.put("cipher", cipher);
+        }
+
+//        cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
         String strKey = "Passw0rd";
         return new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8));
     }
@@ -151,6 +173,14 @@ public class EncryptUtils {
 
 
 //        System.out.println(desEncrypt(str));
-        System.out.println(desDecrypt("2cd4167b1c627f11a6ce4ce7cadf1b18"));
+//        System.out.println(desDecrypt("2cd4167b1c627f11a6ce4ce7cadf1b18"));
+
+        testLoop("BDA385EC848C1A425F746869011C8D23", 100);
+    }
+
+    private static void testLoop(String appId, int num){
+        for (int i = 0; i < num; i++) {
+            getType(appId);
+        }
     }
 }