Skip to content

Commit 622d267

Browse files
committed
新增 AES 加密数据库用户名密码
1 parent 68d7dbc commit 622d267

File tree

7 files changed

+183
-21
lines changed

7 files changed

+183
-21
lines changed

gradle/wrapper/gradle-wrapper.jar

3.01 KB
Binary file not shown.
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Fri May 08 09:44:04 CST 2020
2+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
13
distributionBase=GRADLE_USER_HOME
24
distributionPath=wrapper/dists
3-
distributionUrl=https\://downloads.gradle-dn.com/distributions/gradle-5.6.4-bin.zip
4-
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6+
zipStoreBase=GRADLE_USER_HOME

gradlew

+14-19
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ if $darwin; then
125125
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
126126
fi
127127

128-
# For Cygwin, switch paths to Windows format before running java
129-
if $cygwin ; then
128+
# For Cygwin or MSYS, switch paths to Windows format before running java
129+
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
130130
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
131131
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
132132
JAVACMD=`cygpath --unix "$JAVACMD"`
@@ -154,19 +154,19 @@ if $cygwin ; then
154154
else
155155
eval `echo args$i`="\"$arg\""
156156
fi
157-
i=$((i+1))
157+
i=`expr $i + 1`
158158
done
159159
case $i in
160-
(0) set -- ;;
161-
(1) set -- "$args0" ;;
162-
(2) set -- "$args0" "$args1" ;;
163-
(3) set -- "$args0" "$args1" "$args2" ;;
164-
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
165-
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
166-
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
167-
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
168-
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
169-
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
160+
0) set -- ;;
161+
1) set -- "$args0" ;;
162+
2) set -- "$args0" "$args1" ;;
163+
3) set -- "$args0" "$args1" "$args2" ;;
164+
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
165+
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
166+
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
167+
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
168+
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
169+
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
170170
esac
171171
fi
172172

@@ -175,14 +175,9 @@ save () {
175175
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
176176
echo " "
177177
}
178-
APP_ARGS=$(save "$@")
178+
APP_ARGS=`save "$@"`
179179

180180
# Collect all arguments for the java command, following the shell quoting and substitution rules
181181
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
182182

183-
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
184-
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
185-
cd "$(dirname "$0")"
186-
fi
187-
188183
exec "$JAVACMD" "$@"

gradlew.bat

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ if "%DIRNAME%" == "" set DIRNAME=.
2929
set APP_BASE_NAME=%~n0
3030
set APP_HOME=%DIRNAME%
3131

32+
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
33+
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34+
3235
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
3336
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
3437

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2011-2020, baomidou ([email protected]).
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.baomidou.mybatisplus.core.toolkit;
17+
18+
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
19+
20+
import javax.crypto.Cipher;
21+
import javax.crypto.spec.IvParameterSpec;
22+
import javax.crypto.spec.SecretKeySpec;
23+
import java.io.UnsupportedEncodingException;
24+
import java.security.NoSuchAlgorithmException;
25+
import java.util.Base64;
26+
27+
/**
28+
* AES CBC模式加密工具类
29+
*
30+
* @author hubin
31+
* @since 2020-05-23
32+
*/
33+
public class AES {
34+
35+
/**
36+
* 加密
37+
*
38+
* @param data 需要加密的内容
39+
* @param key 加密密码
40+
* @return
41+
*/
42+
public static byte[] encrypt(byte[] data, byte[] key) {
43+
try {
44+
SecretKeySpec secretKey = new SecretKeySpec(key, Constants.AES);
45+
byte[] enCodeFormat = secretKey.getEncoded();
46+
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, Constants.AES);
47+
Cipher cipher = Cipher.getInstance(Constants.AES_CBC_CIPHER);
48+
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(key));
49+
return cipher.doFinal(data);
50+
} catch (Exception e) {
51+
throw new MybatisPlusException(e);
52+
}
53+
}
54+
55+
/**
56+
* 解密
57+
*
58+
* @param data 待解密内容
59+
* @param key 解密密钥
60+
* @return
61+
*/
62+
public static byte[] decrypt(byte[] data, byte[] key) {
63+
try {
64+
SecretKeySpec secretKey = new SecretKeySpec(key, Constants.AES);
65+
byte[] enCodeFormat = secretKey.getEncoded();
66+
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, Constants.AES);
67+
Cipher cipher = Cipher.getInstance(Constants.AES_CBC_CIPHER);
68+
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(key));
69+
return cipher.doFinal(data);
70+
} catch (Exception e) {
71+
throw new MybatisPlusException(e);
72+
}
73+
}
74+
75+
/**
76+
* 加密
77+
*
78+
* @param data 需要加密的内容
79+
* @param key 加密密码
80+
* @return
81+
*/
82+
public static String encrypt(String data, String key) {
83+
try {
84+
byte[] valueByte = encrypt(data.getBytes(Constants.UTF_8), key.getBytes(Constants.UTF_8));
85+
return Base64.getEncoder().encodeToString(valueByte);
86+
} catch (UnsupportedEncodingException e) {
87+
throw new MybatisPlusException(e);
88+
}
89+
}
90+
91+
/**
92+
* 解密
93+
*
94+
* @param data 待解密内容 base64 字符串
95+
* @param key 解密密钥
96+
* @return
97+
*/
98+
public static String decrypt(String data, String key) {
99+
try {
100+
byte[] originalData = Base64.getDecoder().decode(data.getBytes());
101+
byte[] valueByte = decrypt(originalData, key.getBytes(Constants.UTF_8));
102+
return new String(valueByte);
103+
} catch (UnsupportedEncodingException e) {
104+
throw new MybatisPlusException(e);
105+
}
106+
}
107+
108+
/**
109+
* 生成一个随机字符串密钥
110+
*
111+
* @return
112+
* @throws NoSuchAlgorithmException
113+
*/
114+
public static String generateRandomKey() {
115+
return IdWorker.get32UUID().substring(0, 16);
116+
}
117+
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public interface Constants extends StringPool {
3232
* MD5
3333
*/
3434
String MD5 = "MD5";
35+
/**
36+
* AES
37+
*/
38+
String AES = "AES";
39+
/**
40+
* AES 算法
41+
*/
42+
String AES_CBC_CIPHER = "AES/CBC/PKCS5Padding";
3543
/**
3644
* 实体类
3745
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2011-2019, hubin ([email protected]).
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.baomidou.mybatisplus.core.toolkit;
17+
18+
import org.junit.jupiter.api.Assertions;
19+
import org.junit.jupiter.api.Test;
20+
21+
/**
22+
* 测试 AES 算法
23+
*
24+
* @author hubin
25+
* @since 2020-05-23
26+
*/
27+
public class AESTest {
28+
29+
@Test
30+
public void encryptDecrypt() {
31+
String data = "1@2#3qwe111";
32+
String randomKey = AES.generateRandomKey();
33+
System.out.println("AES 密钥:" + randomKey);
34+
String result = AES.encrypt(data, randomKey);
35+
System.out.println("AES 加密内容:" + result);
36+
Assertions.assertEquals(data, AES.decrypt(result, randomKey));
37+
}
38+
}

0 commit comments

Comments
 (0)