优化代码。

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