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..a9806c47 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; @@ -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,8 +93,7 @@ public abstract class AesUtils { public static byte[] encrypt(byte[] bytes) { try { - var cipher = LOCAL_CIPHER.get(); - cipher.init(Cipher.ENCRYPT_MODE, KEY); + var cipher = LOCAL_ENCRYPT_CIPHER.get(); return cipher.doFinal(bytes); } catch (Exception e) { throw new RuntimeException(e); @@ -108,8 +119,7 @@ public abstract class AesUtils { public static byte[] decrypt(byte[] bytes) { try { - var cipher = LOCAL_CIPHER.get(); - cipher.init(Cipher.DECRYPT_MODE, KEY); + var cipher = LOCAL_DECRYPT_CIPHER.get(); return cipher.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)); + } } }