diff --git a/util/src/main/java/com/zfoo/util/security/AesUtils.java b/util/src/main/java/com/zfoo/util/security/AesUtils.java
index 083c3738..c1e68242 100644
--- a/util/src/main/java/com/zfoo/util/security/AesUtils.java
+++ b/util/src/main/java/com/zfoo/util/security/AesUtils.java
@@ -20,6 +20,7 @@ import io.netty.util.concurrent.FastThreadLocal;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
+import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
@@ -29,7 +30,7 @@ import java.util.Base64;
*
* 默认AES/ECB/PKCS5Padding
*
- * @author jaysunxiao
+ * @author godotg
* @version 3.0
*/
public abstract class AesUtils {
@@ -46,13 +47,6 @@ public abstract class AesUtils {
*/
private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";
- private static final FastThreadLocal LOCAL_CIPHER = new FastThreadLocal() {
- @Override
- protected Cipher initialValue() throws NoSuchPaddingException, NoSuchAlgorithmException {
- return Cipher.getInstance(ALGORITHM_STR);
- }
- };
-
static {
try {
KEY = new SecretKeySpec(KEY_STR.getBytes(StringUtils.DEFAULT_CHARSET_NAME), ALGORITHM);
@@ -61,6 +55,24 @@ public abstract class AesUtils {
}
}
+ private static final FastThreadLocal LOCAL_ENCRYPT_CIPHER = new FastThreadLocal() {
+ @Override
+ protected Cipher initialValue() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
+ var cipher = Cipher.getInstance(ALGORITHM_STR);
+ cipher.init(Cipher.ENCRYPT_MODE, KEY);
+ return cipher;
+ }
+ };
+
+ private static final FastThreadLocal LOCAL_DECRYPT_CIPHER = new FastThreadLocal() {
+ @Override
+ protected Cipher initialValue() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
+ var cipher = Cipher.getInstance(ALGORITHM_STR);
+ cipher.init(Cipher.DECRYPT_MODE, KEY);
+ return cipher;
+ }
+ };
+
/**
* 对str进行AES加密
@@ -81,9 +93,7 @@ public abstract class AesUtils {
public static byte[] encrypt(byte[] bytes) {
try {
- var cipher = LOCAL_CIPHER.get();
- cipher.init(Cipher.ENCRYPT_MODE, KEY);
- return cipher.doFinal(bytes);
+ return LOCAL_ENCRYPT_CIPHER.get().doFinal(bytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
@@ -108,9 +118,7 @@ public abstract class AesUtils {
public static byte[] decrypt(byte[] bytes) {
try {
- var cipher = LOCAL_CIPHER.get();
- cipher.init(Cipher.DECRYPT_MODE, KEY);
- return cipher.doFinal(bytes);
+ return LOCAL_DECRYPT_CIPHER.get().doFinal(bytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
diff --git a/util/src/test/java/com/zfoo/util/security/AesUtilsTest.java b/util/src/test/java/com/zfoo/util/security/AesUtilsTest.java
index 977fdd68..cdaf5a8b 100644
--- a/util/src/test/java/com/zfoo/util/security/AesUtilsTest.java
+++ b/util/src/test/java/com/zfoo/util/security/AesUtilsTest.java
@@ -23,9 +23,11 @@ public class AesUtilsTest {
@Test
public void test() {
- String passWord = "hello world";
- String encodePassWorld = AesUtils.getEncryptString(passWord);
- Assert.assertEquals(passWord, AesUtils.getDecryptString(encodePassWorld));
+ for (int i = 0; i < 10; i++) {
+ var passWord = "hello world";
+ var encodePassWorld = AesUtils.getEncryptString(passWord);
+ Assert.assertEquals(passWord, AesUtils.getDecryptString(encodePassWorld));
+ }
}
}