AE86 11 months ago
parent
commit
e19d37fccc

+ 0 - 202
dbsyncer-common/src/main/java/org/dbsyncer/common/util/AESEncyptUtil.java

@@ -1,202 +0,0 @@
-package org.dbsyncer.common.util;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.crypto.Cipher;
-import javax.crypto.KeyGenerator;
-import javax.crypto.SecretKey;
-import javax.crypto.spec.SecretKeySpec;
-import java.io.UnsupportedEncodingException;
-import java.security.NoSuchAlgorithmException;
-import java.security.SecureRandom;
-import java.util.Arrays;
-
-public abstract class AESEncyptUtil {
-    
-    private final static Logger logger = LoggerFactory.getLogger(AESEncyptUtil.class);
-
-    // 默认缺省种子
-    public static String keySeed = "c32ad1415f6c89fee76d8457c31efb4b";
-
-    /**
-     * 加密
-     * 
-     * @param content
-     *            需要加密的内容
-     * @param keyseed
-     *            密钥
-     * @return
-     */
-    public static String encrypt(String content, String keyseed) throws Exception {
-        KeyGenerator kgen = KeyGenerator.getInstance("AES");
-        // 解决linux环境下密码解密问题
-        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
-        secureRandom.setSeed(keyseed.getBytes("UTF-8"));
-        kgen.init(128, secureRandom);
-        SecretKey secretKey = kgen.generateKey();
-        byte[] enCodeFormat = secretKey.getEncoded();
-        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
-        Cipher cipher = Cipher.getInstance("AES");// 创建密码器
-        byte[] byteContent = content.getBytes("utf-8");
-        cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
-        byte[] result = cipher.doFinal(byteContent);
-        return byte2Hex(result); // 加密
-    }
-
-    /**
-     * 解密
-     * 
-     * @param content
-     *            待解密内容
-     * @param keyseed
-     *            解密密钥
-     * @return
-     */
-    public static byte[] decrypt(byte[] content, String keyseed) throws Exception {
-        KeyGenerator kgen = KeyGenerator.getInstance("AES");
-        // 解决linux环境下密码解密问题
-        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
-        secureRandom.setSeed(keyseed.getBytes("UTF-8"));
-        kgen.init(128, secureRandom);
-        SecretKey secretKey = kgen.generateKey();
-        byte[] enCodeFormat = secretKey.getEncoded();
-        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
-        Cipher cipher = Cipher.getInstance("AES");// 创建密码器
-        cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
-        byte[] result = cipher.doFinal(hex2Byte(content));
-        return result; // 加密
-    }
-
-    /**
-     * 解密
-     * 
-     * @param content
-     *            待解密内容
-     * @param keyseed
-     *            解密密钥
-     * @return
-     */
-    public static String decrypt(String content, String keyseed) throws Exception {
-        KeyGenerator kgen = KeyGenerator.getInstance("AES");
-        // 解决linux环境下密码解密问题
-        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
-        secureRandom.setSeed(keyseed.getBytes("UTF-8"));
-        kgen.init(128, secureRandom);
-        SecretKey secretKey = kgen.generateKey();
-        byte[] enCodeFormat = secretKey.getEncoded();
-        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
-        Cipher cipher = Cipher.getInstance("AES");// 创建密码器
-        cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
-        String result = new String(cipher.doFinal(hex2Byte(content.getBytes("UTF-8"))), "UTF-8");
-        return result.trim();
-    }
-
-    /**
-     * 对于js加密
-     * 
-     * @param content
-     *            内容
-     * @param keyseed
-     *            解密密钥
-     * @return
-     * @throws Exception
-     */
-    public static String encryptForJS(String content, String keyseed) throws Exception {
-        SecretKeySpec key = getKeySpecFromBytes(keyseed.toUpperCase());
-        Cipher cipher = Cipher.getInstance("AES");
-        cipher.init(Cipher.ENCRYPT_MODE, key);
-        byte[] byteEnc = cipher.doFinal(content.getBytes("UTF-8"));
-        return byte2Hex(byteEnc);
-    }
-
-    /**
-     * 解密
-     * 
-     * @param content
-     *            待解密内容
-     * @param keyseed
-     *            解密密钥
-     * @return
-     */
-    public static String decryptForJS(String content, String keyseed) throws Exception {
-        SecretKeySpec key = getKeySpecFromBytes(keyseed.toUpperCase());
-        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
-        cipher.init(Cipher.DECRYPT_MODE, key);
-        String result = new String(cipher.doFinal(hex2Byte(content.getBytes("UTF-8"))), "UTF-8");
-        return result.trim();
-
-    }
-
-    private static String byte2Hex(byte[] b) {
-        String hs = "";
-        String stmp = "";
-        for (int n = 0; n < b.length; n++) {
-            stmp = Integer.toHexString(b[n] & 0xFF);
-            if (stmp.length() == 1) {
-                hs += "0" + stmp;
-            } else {
-                hs += stmp;
-            }
-        }
-        return hs.toUpperCase();
-    }
-
-    /**
-     * 十六进制字符串到字节转换
-     * 
-     * @param b
-     *            byte类型的数组
-     * @return
-     */
-    private static byte[] hex2Byte(byte[] b) {
-        if ((b.length % 2) != 0) {
-            throw new IllegalArgumentException("长度不是偶数!");
-        }
-        byte[] b2 = new byte[b.length / 2];
-
-        for (int n = 0; n < b.length; n += 2) {
-            try {
-                String item = null;
-                // 判断n值是否在b字节长度范围之内,否则,造成堆内存溢出
-                if (n + 2 <= b.length) {
-                    item = new String(b, n, 2, "UTF-8");
-                    b2[n / 2] = (byte) Integer.parseInt(item, 16);
-                }
-                item = "";
-                item = null;
-            } catch (UnsupportedEncodingException e) {
-                logger.error(e.getMessage(), e);
-            }
-        }
-        // 在垃圾回收延迟的情况下,进行记忆清楚,避免信息被窃取
-        byte temp = 0;
-        // 将字节数组赋值为0,删除原有数据
-        Arrays.fill(b, temp);
-        return b2;
-    }
-
-    /**
-     * 从十六进制字符串生成Key
-     * 
-     * @param strBytes
-     *            str字节
-     * @return
-     * @throws NoSuchAlgorithmException
-     */
-    private static SecretKeySpec getKeySpecFromBytes(String strBytes) throws NoSuchAlgorithmException {
-        SecretKeySpec spec = null;
-        try {
-            spec = new SecretKeySpec(hex2Byte(strBytes.getBytes("UTF-8")), "AES");
-        } catch (UnsupportedEncodingException e) {
-            logger.error(e.getMessage(), e);
-        }
-        try {
-            spec = new SecretKeySpec(hex2Byte(strBytes.getBytes("UTF-8")), "AES");
-        } catch (UnsupportedEncodingException e) {
-            logger.error(e.getMessage(), e);
-        }
-        return spec;
-    }
-
-}

+ 98 - 0
dbsyncer-common/src/main/java/org/dbsyncer/common/util/AESUtil.java

@@ -0,0 +1,98 @@
+/**
+ * DBSyncer Copyright 2020-2024 All Rights Reserved.
+ */
+package org.dbsyncer.common.util;
+
+import org.dbsyncer.common.CommonException;
+
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.UnsupportedEncodingException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.util.Arrays;
+
+public abstract class AESUtil {
+
+    private static KeyGenerator keyGenerator;
+
+    static {
+        try {
+            keyGenerator = KeyGenerator.getInstance("AES");
+        } catch (NoSuchAlgorithmException e) {
+            throw new CommonException(e);
+        }
+    }
+
+    public static String encrypt(String content, String key) {
+        try {
+            Cipher cipher = Cipher.getInstance("AES");
+            cipher.init(Cipher.ENCRYPT_MODE, getSecretKeySpec(key));
+            byte[] result = cipher.doFinal(content.getBytes("utf-8"));
+            return byte2Hex(result);
+        } catch (Exception e) {
+            throw new CommonException(e);
+        }
+    }
+
+    public static String decrypt(String content, String key) {
+        try {
+            Cipher cipher = Cipher.getInstance("AES");
+            cipher.init(Cipher.DECRYPT_MODE, getSecretKeySpec(key));
+            String result = new String(cipher.doFinal(hex2Byte(content.getBytes("UTF-8"))), "UTF-8");
+            return result.trim();
+        } catch (Exception e) {
+            throw new CommonException(e);
+        }
+    }
+
+    private static SecretKeySpec getSecretKeySpec(String key) throws NoSuchAlgorithmException, UnsupportedEncodingException {
+        // 解决linux环境下密码解密问题
+        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
+        secureRandom.setSeed(key.getBytes("UTF-8"));
+        keyGenerator.init(128, secureRandom);
+        byte[] enCodeFormat = keyGenerator.generateKey().getEncoded();
+        return new SecretKeySpec(enCodeFormat, "AES");
+    }
+
+    private static String byte2Hex(byte[] b) {
+        String hs = "";
+        String stmp = "";
+        for (int n = 0; n < b.length; n++) {
+            stmp = Integer.toHexString(b[n] & 0xFF);
+            if (stmp.length() == 1) {
+                hs += "0" + stmp;
+            } else {
+                hs += stmp;
+            }
+        }
+        return hs.toUpperCase();
+    }
+
+    private static byte[] hex2Byte(byte[] b) {
+        if ((b.length % 2) != 0) {
+            throw new IllegalArgumentException("长度不是偶数!");
+        }
+        byte[] b2 = new byte[b.length / 2];
+
+        for (int n = 0; n < b.length; n += 2) {
+            try {
+                String item = null;
+                // 判断n值是否在b字节长度范围之内,否则,造成堆内存溢出
+                if (n + 2 <= b.length) {
+                    item = new String(b, n, 2, "UTF-8");
+                    b2[n / 2] = (byte) Integer.parseInt(item, 16);
+                }
+                item = null;
+            } catch (UnsupportedEncodingException e) {
+                throw new CommonException(e);
+            }
+        }
+        byte temp = 0;
+        // 将字节数组赋值为0,删除原有数据
+        Arrays.fill(b, temp);
+        return b2;
+    }
+
+}

+ 5 - 2
dbsyncer-parser/src/main/java/org/dbsyncer/parser/convert/handler/AesDecryptHandler.java

@@ -1,6 +1,9 @@
+/**
+ * DBSyncer Copyright 2020-2024 All Rights Reserved.
+ */
 package org.dbsyncer.parser.convert.handler;
 
-import org.dbsyncer.common.util.AESEncyptUtil;
+import org.dbsyncer.common.util.AESUtil;
 import org.dbsyncer.parser.convert.AbstractHandler;
 
 /**
@@ -14,7 +17,7 @@ public class AesDecryptHandler extends AbstractHandler {
 
     @Override
     public Object convert(String args, Object value) throws Exception {
-        return AESEncyptUtil.decrypt(String.valueOf(value), args);
+        return AESUtil.decrypt(String.valueOf(value), args);
     }
 
 }

+ 5 - 2
dbsyncer-parser/src/main/java/org/dbsyncer/parser/convert/handler/AesEncryptHandler.java

@@ -1,6 +1,9 @@
+/**
+ * DBSyncer Copyright 2020-2024 All Rights Reserved.
+ */
 package org.dbsyncer.parser.convert.handler;
 
-import org.dbsyncer.common.util.AESEncyptUtil;
+import org.dbsyncer.common.util.AESUtil;
 import org.dbsyncer.parser.convert.AbstractHandler;
 
 /**
@@ -14,6 +17,6 @@ public class AesEncryptHandler extends AbstractHandler {
 
     @Override
     public Object convert(String args, Object value) throws Exception {
-        return AESEncyptUtil.encrypt(String.valueOf(value), args);
+        return AESUtil.encrypt(String.valueOf(value), args);
     }
 }