diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/cipher/AES128ECBWithPKCS7.java b/commons/common-util/src/main/java/com/schbrain/common/util/cipher/AES128ECBWithPKCS7.java index 0454101a7d0a1f198c83fcc09a97525e471ca3d4..ab0d38370237a993ac80f92b8c72b16b9d62c2fa 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/cipher/AES128ECBWithPKCS7.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/cipher/AES128ECBWithPKCS7.java @@ -1,7 +1,9 @@ package com.schbrain.common.util.cipher; import cn.hutool.crypto.symmetric.AES; +import com.schbrain.common.exception.BaseException; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.bouncycastle.jce.provider.BouncyCastleProvider; import java.security.Security; @@ -19,40 +21,55 @@ public class AES128ECBWithPKCS7 { /** * encrypt input text */ - public static String encrypt(String input, String key) { - byte[] encrypted = encryptByte(input, key); - if (null == encrypted) { - return ""; - } - return Base64.getEncoder().encodeToString(encrypted); + public static String encrypt(String input, String secretKey) { + return encrypt(input, secretKey, true); } - public static byte[] encryptByte(String input, String key) { - byte[] encrypted; - try { - encrypted = new AES("ECB", "PKCS7Padding", key.getBytes(UTF_8)).encrypt(input); - } catch (Exception e) { - log.error("AES128ECBWithPKCS7 encrypt ({}) error! ", input, e); - return null; - } - return encrypted; + /** + * encrypt input text + */ + public static String encrypt(String input, String secretKey, boolean toBase64) { + byte[] encrypted = encryptByte(input.getBytes(UTF_8), secretKey); + return toBase64 ? Base64.getEncoder().encodeToString(encrypted) : new String(encrypted, UTF_8); + } + + /** + * decrypt input text + */ + public static String decrypt(String input, String secretKey) { + return decrypt(input, secretKey, true); } /** * decrypt input text */ - public static String decrypt(String input, String key) { - return decryptByte(Base64.getDecoder().decode(input), key); + public static String decrypt(String input, String secretKey, boolean isBase64) { + byte[] bytes = isBase64 ? Base64.getDecoder().decode(input) : input.getBytes(UTF_8); + return new String(decryptByte(bytes, secretKey), UTF_8); } - public static String decryptByte(byte[] input, String key) { - try { - byte[] output = new AES("ECB", "PKCS7Padding", key.getBytes(UTF_8)).decrypt(input); - return new String(output, UTF_8); - } catch (Exception e) { - log.error("AES128ECBWithPKCS7 decryptByte ({}) error!", new String(input, UTF_8), e); - return ""; + /** + * encrypt input bytes + */ + public static byte[] encryptByte(byte[] input, String secretKey) { + return createAes(secretKey).encrypt(input); + } + + /** + * decrypt input bytes + */ + public static byte[] decryptByte(byte[] input, String secretKey) { + return createAes(secretKey).decrypt(input); + } + + /** + * create aes crypto + */ + private static AES createAes(String secretKey) { + if (StringUtils.isBlank(secretKey)) { + throw new BaseException("secretKey is blank!"); } + return new AES("ECB", "PKCS7Padding", secretKey.getBytes(UTF_8)); } -} \ No newline at end of file +}