1、sonar代码规范问题修复;2、Aes对称加解密算法替换;

This commit is contained in:
gaoqr
2024-09-13 14:20:06 +08:00
parent b3df3504db
commit d83250ec0d
5 changed files with 55 additions and 78 deletions
@@ -1,13 +1,12 @@
package com.cf.imes.framework.common.util.encrypt;
import cn.hutool.core.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import java.security.GeneralSecurityException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
@@ -23,7 +22,7 @@ public class AesUtils {
private static final String AES_ALG = "AES";
private static final String AES_CBC_PCK_ALG = "AES/CBC/PKCS5Padding";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/GCM/NoPadding";
/**
@@ -34,14 +33,14 @@ public class AesUtils {
* @return
* @throws Exception
*/
public static String decrypt(String content, String aesKey) {
public static String decrypt(String base64Content, String aesKey) {
try {
Cipher cipher = Cipher.getInstance(AES_CBC_PCK_ALG);
IvParameterSpec iv = new IvParameterSpec(initIv(AES_CBC_PCK_ALG));
cipher.init(Cipher.DECRYPT_MODE, generateKey(aesKey.getBytes()), iv);
byte[] cleanBytes = cipher.doFinal(Base64.getDecoder().decode(content.getBytes()));
return new String(cleanBytes, CharsetUtil.UTF_8);
byte[] content = Base64.getDecoder().decode(base64Content);
GCMParameterSpec params = new GCMParameterSpec(128, content, 0, 12);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(aesKey), params);
byte[] decryptData = cipher.doFinal(content, 12, content.length - 12);
return new String(decryptData);
} catch (Exception e) {
log.error("[AesUtils][decrypt]解密失败:{}", e.getMessage(), e);
// nacos热发布配置,空字符串会导致nacos轮训刷新配置
@@ -56,44 +55,16 @@ public class AesUtils {
* @return
* @throws NoSuchAlgorithmException
*/
private static SecretKey generateKey(byte[] bytes) throws NoSuchAlgorithmException {
private static SecretKey getSecretKey(String key) throws NoSuchAlgorithmException {
// 生成随机数
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
// 设置随机数种子
random.setSeed(bytes);
random.setSeed(key.getBytes());
// aes算法生成器
KeyGenerator keyGen = KeyGenerator.getInstance(AES_ALG);
keyGen.init(128, random); // 使用128位的密钥
return keyGen.generateKey();
}
/**
* 初始向量的方法, 全部为0. 这里的写法适合于其它算法,针对AES算法的话,IV值一定是128位的(16字节).
*
* @param fullAlg
* @return
* @throws GeneralSecurityException
*/
private static byte[] initIv(String fullAlg) {
try {
Cipher cipher = Cipher.getInstance(fullAlg);
int blockSize = cipher.getBlockSize();
byte[] iv = new byte[blockSize];
for (int i = 0; i < blockSize; ++i) {
iv[i] = 0;
}
return iv;
} catch (Exception e) {
int blockSize = 16;
byte[] iv = new byte[blockSize];
for (int i = 0; i < blockSize; ++i) {
iv[i] = 0;
}
return iv;
}
SecretKey secretKey = keyGen.generateKey();
return new SecretKeySpec(secretKey.getEncoded(), AES_ALG);
}
}