优化代码。

This commit is contained in:
chenxudong 2025-10-16 09:27:08 +08:00
parent 1198ea8d02
commit b7a8f2464f
1 changed files with 8 additions and 18 deletions

View File

@ -1,17 +1,8 @@
package com.electromagnetic.industry.software.common.util;
import cn.hutool.core.codec.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
/**
* <p>Description: [AES对称加密和解密]</p>
@ -31,13 +22,12 @@ public class AESUtils {
*/
public static String decrypt(String enc, String key) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return new String(cipher.doFinal(Base64.decode(enc)));
} catch (NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | BadPaddingException |
InvalidKeyException e) {
// 使用Hutool创建AES解密器ECB模式PKCS7Padding
AES aes = SecureUtil.aes(key.getBytes());
// 执行解密Hutool会自动处理Base64解码
byte[] decrypted = aes.decrypt(enc);
return new String(decrypted);
} catch (Exception e) {
throw new RuntimeException(e);
}
}