|
@@ -18,6 +18,7 @@ import javax.crypto.spec.SecretKeySpec;
|
|
|
import com.chelvc.framework.common.model.Pool;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import lombok.NonNull;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
|
|
@@ -27,6 +28,7 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
* @author woody
|
|
|
* @date 2024/1/30
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
public final class AESUtils {
|
|
|
/**
|
|
|
* 密钥长度
|
|
@@ -75,6 +77,21 @@ public final class AESUtils {
|
|
|
private AESUtils() {
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将密钥转换成向量
|
|
|
+ *
|
|
|
+ * @param secret 密钥
|
|
|
+ * @return 向量
|
|
|
+ */
|
|
|
+ public static String secret2iv(@NonNull String secret) {
|
|
|
+ int size = Math.min(secret.length(), 16);
|
|
|
+ StringBuilder iv = new StringBuilder(size);
|
|
|
+ for (int i = secret.length() - 1, max = 0; i >= 0 && max < size; i--, max++) {
|
|
|
+ iv.append(secret.charAt(i));
|
|
|
+ }
|
|
|
+ return iv.toString();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取密码处理器
|
|
|
*
|
|
@@ -376,7 +393,30 @@ public final class AESUtils {
|
|
|
* @return 密文(base64)
|
|
|
*/
|
|
|
public static String encrypt(@NonNull Cipher cipher, String plaintext) {
|
|
|
- return plaintext == null ? null : encrypt(cipher, plaintext.getBytes(StandardCharsets.UTF_8));
|
|
|
+ return encrypt(cipher, plaintext, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AES加密
|
|
|
+ *
|
|
|
+ * @param cipher 密码处理器
|
|
|
+ * @param plaintext 明文
|
|
|
+ * @param force 是否忽略异常
|
|
|
+ * @return 密文(base64)
|
|
|
+ */
|
|
|
+ public static String encrypt(@NonNull Cipher cipher, String plaintext, boolean force) {
|
|
|
+ if (plaintext == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return encrypt(cipher, plaintext.getBytes(StandardCharsets.UTF_8));
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (!force) {
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ log.warn("Plaintext encrypt failed: {}, {}", plaintext, e.getMessage());
|
|
|
+ }
|
|
|
+ return plaintext;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -398,8 +438,30 @@ public final class AESUtils {
|
|
|
* @return 明文
|
|
|
*/
|
|
|
public static String decrypt(@NonNull Cipher cipher, String ciphertext) {
|
|
|
- byte[] bytes = ObjectUtils.ifNull(ciphertext, Base64::decodeBase64);
|
|
|
- return bytes == null || bytes.length == 0 ? ciphertext : decrypt(cipher, bytes);
|
|
|
+ return decrypt(cipher, ciphertext, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AES解密
|
|
|
+ *
|
|
|
+ * @param cipher 密码处理器
|
|
|
+ * @param ciphertext 密文(base64)
|
|
|
+ * @param force 是否忽略异常
|
|
|
+ * @return 明文
|
|
|
+ */
|
|
|
+ public static String decrypt(@NonNull Cipher cipher, String ciphertext, boolean force) {
|
|
|
+ if (ciphertext == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return decrypt(cipher, Base64.decodeBase64(ciphertext));
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (!force) {
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ log.warn("Ciphertext decrypt failed: {}, {}", ciphertext, e.getMessage());
|
|
|
+ }
|
|
|
+ return ciphertext;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -421,12 +483,24 @@ public final class AESUtils {
|
|
|
* @return 密文数组
|
|
|
*/
|
|
|
public static String[] encrypt(@NonNull Cipher cipher, String... plaintexts) {
|
|
|
+ return encrypt(cipher, plaintexts, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据加密
|
|
|
+ *
|
|
|
+ * @param cipher 密码处理器
|
|
|
+ * @param plaintexts 明文数组
|
|
|
+ * @param force 是否忽略异常
|
|
|
+ * @return 密文数组
|
|
|
+ */
|
|
|
+ public static String[] encrypt(@NonNull Cipher cipher, String[] plaintexts, boolean force) {
|
|
|
if (ObjectUtils.isEmpty(plaintexts)) {
|
|
|
return plaintexts;
|
|
|
}
|
|
|
String[] ciphertexts = new String[plaintexts.length];
|
|
|
for (int i = 0; i < plaintexts.length; i++) {
|
|
|
- ciphertexts[i] = encrypt(cipher, plaintexts[i]);
|
|
|
+ ciphertexts[i] = encrypt(cipher, plaintexts[i], force);
|
|
|
}
|
|
|
return ciphertexts;
|
|
|
}
|
|
@@ -439,10 +513,22 @@ public final class AESUtils {
|
|
|
* @return 密文集合
|
|
|
*/
|
|
|
public static Set<String> encrypt(@NonNull Cipher cipher, Set<String> plaintexts) {
|
|
|
+ return encrypt(cipher, plaintexts, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据加密
|
|
|
+ *
|
|
|
+ * @param cipher 密码处理器
|
|
|
+ * @param plaintexts 明文集合
|
|
|
+ * @param force 是否忽略异常
|
|
|
+ * @return 密文集合
|
|
|
+ */
|
|
|
+ public static Set<String> encrypt(@NonNull Cipher cipher, Set<String> plaintexts, boolean force) {
|
|
|
if (ObjectUtils.isEmpty(plaintexts)) {
|
|
|
return plaintexts;
|
|
|
}
|
|
|
- return plaintexts.stream().map(plaintext -> encrypt(cipher, plaintext)).collect(Collectors.toSet());
|
|
|
+ return plaintexts.stream().map(plaintext -> encrypt(cipher, plaintext, force)).collect(Collectors.toSet());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -453,10 +539,22 @@ public final class AESUtils {
|
|
|
* @return 密文列表
|
|
|
*/
|
|
|
public static List<String> encrypt(@NonNull Cipher cipher, List<String> plaintexts) {
|
|
|
+ return encrypt(cipher, plaintexts, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据加密
|
|
|
+ *
|
|
|
+ * @param cipher 密码处理器
|
|
|
+ * @param plaintexts 明文列表
|
|
|
+ * @param force 是否忽略异常
|
|
|
+ * @return 密文列表
|
|
|
+ */
|
|
|
+ public static List<String> encrypt(@NonNull Cipher cipher, List<String> plaintexts, boolean force) {
|
|
|
if (ObjectUtils.isEmpty(plaintexts)) {
|
|
|
return plaintexts;
|
|
|
}
|
|
|
- return plaintexts.stream().map(plaintext -> encrypt(cipher, plaintext)).collect(Collectors.toList());
|
|
|
+ return plaintexts.stream().map(plaintext -> encrypt(cipher, plaintext, force)).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -467,12 +565,24 @@ public final class AESUtils {
|
|
|
* @return 明文数组
|
|
|
*/
|
|
|
public static String[] decrypt(@NonNull Cipher cipher, String... ciphertexts) {
|
|
|
+ return decrypt(cipher, ciphertexts, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据解密
|
|
|
+ *
|
|
|
+ * @param cipher 密码处理器
|
|
|
+ * @param ciphertexts 密文数组
|
|
|
+ * @param force 是否忽略异常
|
|
|
+ * @return 明文数组
|
|
|
+ */
|
|
|
+ public static String[] decrypt(@NonNull Cipher cipher, String[] ciphertexts, boolean force) {
|
|
|
if (ObjectUtils.isEmpty(ciphertexts)) {
|
|
|
return ciphertexts;
|
|
|
}
|
|
|
String[] plaintexts = new String[ciphertexts.length];
|
|
|
for (int i = 0; i < ciphertexts.length; i++) {
|
|
|
- plaintexts[i] = decrypt(cipher, ciphertexts[i]);
|
|
|
+ plaintexts[i] = decrypt(cipher, ciphertexts[i], force);
|
|
|
}
|
|
|
return plaintexts;
|
|
|
}
|
|
@@ -485,10 +595,22 @@ public final class AESUtils {
|
|
|
* @return 明文集合
|
|
|
*/
|
|
|
public static Set<String> decrypt(@NonNull Cipher cipher, Set<String> ciphertexts) {
|
|
|
+ return decrypt(cipher, ciphertexts, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据解密
|
|
|
+ *
|
|
|
+ * @param cipher 密码处理器
|
|
|
+ * @param ciphertexts 密文集合
|
|
|
+ * @param force 是否忽略异常
|
|
|
+ * @return 明文集合
|
|
|
+ */
|
|
|
+ public static Set<String> decrypt(@NonNull Cipher cipher, Set<String> ciphertexts, boolean force) {
|
|
|
if (ObjectUtils.isEmpty(ciphertexts)) {
|
|
|
return ciphertexts;
|
|
|
}
|
|
|
- return ciphertexts.stream().map(ciphertext -> decrypt(cipher, ciphertext)).collect(Collectors.toSet());
|
|
|
+ return ciphertexts.stream().map(ciphertext -> decrypt(cipher, ciphertext, force)).collect(Collectors.toSet());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -499,9 +621,21 @@ public final class AESUtils {
|
|
|
* @return 明文列表
|
|
|
*/
|
|
|
public static List<String> decrypt(@NonNull Cipher cipher, List<String> ciphertexts) {
|
|
|
+ return decrypt(cipher, ciphertexts, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据解密
|
|
|
+ *
|
|
|
+ * @param cipher 密码处理器
|
|
|
+ * @param ciphertexts 密文列表
|
|
|
+ * @param force 是否忽略异常
|
|
|
+ * @return 明文列表
|
|
|
+ */
|
|
|
+ public static List<String> decrypt(@NonNull Cipher cipher, List<String> ciphertexts, boolean force) {
|
|
|
if (ObjectUtils.isEmpty(ciphertexts)) {
|
|
|
return ciphertexts;
|
|
|
}
|
|
|
- return ciphertexts.stream().map(ciphertext -> decrypt(cipher, ciphertext)).collect(Collectors.toList());
|
|
|
+ return ciphertexts.stream().map(ciphertext -> decrypt(cipher, ciphertext, force)).collect(Collectors.toList());
|
|
|
}
|
|
|
}
|