瀏覽代碼

新增 AES 加密数据库用户名密码

hubin 5 年之前
父節點
當前提交
622d267f4e

二進制
gradle/wrapper/gradle-wrapper.jar


+ 3 - 2
gradle/wrapper/gradle-wrapper.properties

@@ -1,5 +1,6 @@
+#Fri May 08 09:44:04 CST 2020
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
-distributionUrl=https\://downloads.gradle-dn.com/distributions/gradle-5.6.4-bin.zip
-zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME

+ 14 - 19
gradlew

@@ -125,8 +125,8 @@ if $darwin; then
     GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
 fi
 
-# For Cygwin, switch paths to Windows format before running java
-if $cygwin ; then
+# For Cygwin or MSYS, switch paths to Windows format before running java
+if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
     APP_HOME=`cygpath --path --mixed "$APP_HOME"`
     CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
     JAVACMD=`cygpath --unix "$JAVACMD"`
@@ -154,19 +154,19 @@ if $cygwin ; then
         else
             eval `echo args$i`="\"$arg\""
         fi
-        i=$((i+1))
+        i=`expr $i + 1`
     done
     case $i in
-        (0) set -- ;;
-        (1) set -- "$args0" ;;
-        (2) set -- "$args0" "$args1" ;;
-        (3) set -- "$args0" "$args1" "$args2" ;;
-        (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
-        (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
-        (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
-        (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
-        (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
-        (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+        0) set -- ;;
+        1) set -- "$args0" ;;
+        2) set -- "$args0" "$args1" ;;
+        3) set -- "$args0" "$args1" "$args2" ;;
+        4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+        5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+        6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+        7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+        8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+        9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
     esac
 fi
 
@@ -175,14 +175,9 @@ save () {
     for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
     echo " "
 }
-APP_ARGS=$(save "$@")
+APP_ARGS=`save "$@"`
 
 # Collect all arguments for the java command, following the shell quoting and substitution rules
 eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
 
-# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
-if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
-  cd "$(dirname "$0")"
-fi
-
 exec "$JAVACMD" "$@"

+ 3 - 0
gradlew.bat

@@ -29,6 +29,9 @@ if "%DIRNAME%" == "" set DIRNAME=.
 set APP_BASE_NAME=%~n0
 set APP_HOME=%DIRNAME%
 
+@rem Resolve any "." and ".." in APP_HOME to make it shorter.
+for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
+
 @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
 set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
 

+ 117 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/AES.java

@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * https://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.core.toolkit;
+
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.UnsupportedEncodingException;
+import java.security.NoSuchAlgorithmException;
+import java.util.Base64;
+
+/**
+ * AES CBC模式加密工具类
+ *
+ * @author hubin
+ * @since 2020-05-23
+ */
+public class AES {
+
+    /**
+     * 加密
+     *
+     * @param data 需要加密的内容
+     * @param key  加密密码
+     * @return
+     */
+    public static byte[] encrypt(byte[] data, byte[] key) {
+        try {
+            SecretKeySpec secretKey = new SecretKeySpec(key, Constants.AES);
+            byte[] enCodeFormat = secretKey.getEncoded();
+            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, Constants.AES);
+            Cipher cipher = Cipher.getInstance(Constants.AES_CBC_CIPHER);
+            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(key));
+            return cipher.doFinal(data);
+        } catch (Exception e) {
+            throw new MybatisPlusException(e);
+        }
+    }
+
+    /**
+     * 解密
+     *
+     * @param data 待解密内容
+     * @param key  解密密钥
+     * @return
+     */
+    public static byte[] decrypt(byte[] data, byte[] key) {
+        try {
+            SecretKeySpec secretKey = new SecretKeySpec(key, Constants.AES);
+            byte[] enCodeFormat = secretKey.getEncoded();
+            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, Constants.AES);
+            Cipher cipher = Cipher.getInstance(Constants.AES_CBC_CIPHER);
+            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(key));
+            return cipher.doFinal(data);
+        } catch (Exception e) {
+            throw new MybatisPlusException(e);
+        }
+    }
+
+    /**
+     * 加密
+     *
+     * @param data 需要加密的内容
+     * @param key  加密密码
+     * @return
+     */
+    public static String encrypt(String data, String key) {
+        try {
+            byte[] valueByte = encrypt(data.getBytes(Constants.UTF_8), key.getBytes(Constants.UTF_8));
+            return Base64.getEncoder().encodeToString(valueByte);
+        } catch (UnsupportedEncodingException e) {
+            throw new MybatisPlusException(e);
+        }
+    }
+
+    /**
+     * 解密
+     *
+     * @param data 待解密内容 base64 字符串
+     * @param key  解密密钥
+     * @return
+     */
+    public static String decrypt(String data, String key) {
+        try {
+            byte[] originalData = Base64.getDecoder().decode(data.getBytes());
+            byte[] valueByte = decrypt(originalData, key.getBytes(Constants.UTF_8));
+            return new String(valueByte);
+        } catch (UnsupportedEncodingException e) {
+            throw new MybatisPlusException(e);
+        }
+    }
+
+    /**
+     * 生成一个随机字符串密钥
+     *
+     * @return
+     * @throws NoSuchAlgorithmException
+     */
+    public static String generateRandomKey() {
+        return IdWorker.get32UUID().substring(0, 16);
+    }
+}

+ 8 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Constants.java

@@ -32,6 +32,14 @@ public interface Constants extends StringPool {
      * MD5
      */
     String MD5 = "MD5";
+    /**
+     * AES
+     */
+    String AES = "AES";
+    /**
+     * AES 算法
+     */
+    String AES_CBC_CIPHER = "AES/CBC/PKCS5Padding";
     /**
      * 实体类
      */

+ 38 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/toolkit/AESTest.java

@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2011-2019, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * https://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.core.toolkit;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * 测试 AES 算法
+ *
+ * @author hubin
+ * @since 2020-05-23
+ */
+public class AESTest {
+
+    @Test
+    public void encryptDecrypt() {
+        String data = "1@2#3qwe111";
+        String randomKey = AES.generateRandomKey();
+        System.out.println("AES 密钥:" + randomKey);
+        String result = AES.encrypt(data, randomKey);
+        System.out.println("AES 加密内容:" + result);
+        Assertions.assertEquals(data, AES.decrypt(result, randomKey));
+    }
+}