guoshilong
2023-08-09 34c2f71c21bc56b359b27ad92061477a0ff3ff48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package cn.gistack.system.user.sync.util;
 
import cn.gistack.system.user.sync.constant.CimsConstants;
import cn.gistack.system.user.sync.exception.BusinessException;
import cn.gistack.system.user.sync.exception.ErrorCode;
import org.springframework.util.StringUtils;
 
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
 
/**
 * AES加密解密
 *
 * @author wd
 */
public class SecurityUtil {
 
    private SecurityUtil() {
    }
 
    /**
     * AES加密
     *
     * @param content 明文
     * @return 密文
     */
    public static String encryptAES(String content, String secretKey) {
        checkParam(content, secretKey);
        //AES加密
        return encrypt(content, secretKey);
    }
 
    /**
     * AES解密
     *
     * @param encryptResultStr 密文
     * @return 明文
     */
    public static String decryptAES(String encryptResultStr, String secretKey) {
        checkParam(encryptResultStr, secretKey);
        try {
            // BASE64位解密
            byte[] decryptFrom = Base64.getDecoder().decode(encryptResultStr);
 
            //AES解密
            byte[] decryptResult = decrypt(decryptFrom, secretKey);
            return new String(decryptResult);
        } catch (Exception e) {
            // 当密文不规范时会报错,可忽略,但调用的地方需要考虑
            throw new BusinessException(ErrorCode.CONTENT_EMPTY_ERROR);
        }
    }
 
    /**
     * 校验参数
     */
    private static void checkParam(String encryptResultStr, String secretKey) {
        if (StringUtils.isEmpty(encryptResultStr)) {
            throw new BusinessException(ErrorCode.CONTENT_EMPTY_ERROR);
        }
        if (!secretKey.matches(CimsConstants.SECRET_KEY_REGEX)) {
            throw new BusinessException(ErrorCode.SECRET_KEY_ERROR);
        }
    }
 
 
    /**
     * 加密
     *
     * @param content 需要加密的内容
     * @param secretKey  加密密钥
     * @return
     */
    private static String encrypt(String content, String secretKey) {
        try {
            byte[] raw = secretKey.getBytes(CimsConstants.SECRET_KEY_ENCODING);
            SecretKeySpec keySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//NOSONAR  sonar扫描忽略这段代码
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            byte[] bytes = cipher.doFinal(content.getBytes(CimsConstants.SECRET_KEY_ENCODING));
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.CONTENT_ENCODE_ERROR);
        }
    }
 
    /**
     * 解密
     *
     * @param content 待解密内容
     * @param secretKey  解密密钥
     * @return
     */
    private static byte[] decrypt(byte[] content, String secretKey) {
        try {
            byte[] raw = secretKey.getBytes(CimsConstants.SECRET_KEY_ENCODING);
            SecretKeySpec keySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//NOSONAR  sonar扫描忽略这段代码
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            return cipher.doFinal(content);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.SECRET_DECODE_ERROR);
        }
    }
}