优化代码。
This commit is contained in:
parent
1198ea8d02
commit
b7a8f2464f
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue