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