PC
2024-03-30 48c8f6440a6c0b0c060467a0633bc669ca255345
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
package com.qianwen.core.api.crypto.util;
 
import java.util.Objects;
import com.qianwen.core.api.crypto.annotation.decrypt.ApiDecrypt;
import com.qianwen.core.api.crypto.annotation.encrypt.ApiEncrypt;
import com.qianwen.core.api.crypto.bean.CryptoInfoBean;
import com.qianwen.core.api.crypto.config.ApiCryptoProperties;
import com.qianwen.core.api.crypto.enums.CryptoType;
import com.qianwen.core.api.crypto.exception.EncryptBodyFailException;
import com.qianwen.core.api.crypto.exception.EncryptMethodNotFoundException;
import com.qianwen.core.api.crypto.exception.KeyNotConfiguredException;
import com.qianwen.core.tool.utils.AesUtil;
import com.qianwen.core.tool.utils.ClassUtil;
import com.qianwen.core.tool.utils.DesUtil;
import com.qianwen.core.tool.utils.RsaUtil;
import com.qianwen.core.tool.utils.StringUtil;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
 
/* loaded from: blade-starter-api-crypto-9.3.0.0-SNAPSHOT.jar:org/springblade/core/api/crypto/util/ApiCryptoUtil.class */
public class ApiCryptoUtil {
    @Nullable
    public static CryptoInfoBean getEncryptInfo(MethodParameter methodParameter) {
        ApiEncrypt encryptBody = (ApiEncrypt) ClassUtil.getAnnotation(methodParameter.getMethod(), ApiEncrypt.class);
        if (encryptBody == null) {
            return null;
        }
        return new CryptoInfoBean(encryptBody.value(), encryptBody.secretKey());
    }
 
    @Nullable
    public static CryptoInfoBean getDecryptInfo(MethodParameter methodParameter) {
        ApiDecrypt decryptBody = (ApiDecrypt) ClassUtil.getAnnotation(methodParameter.getMethod(), ApiDecrypt.class);
        if (decryptBody == null) {
            return null;
        }
        return new CryptoInfoBean(decryptBody.value(), decryptBody.secretKey());
    }
 
    public static String encryptData(ApiCryptoProperties properties, byte[] jsonData, CryptoInfoBean infoBean) {
        CryptoType type = infoBean.getType();
        if (type == null) {
            throw new EncryptMethodNotFoundException();
        }
        String secretKey = infoBean.getSecretKey();
        if (type == CryptoType.DES) {
            return DesUtil.encryptToBase64(jsonData, checkSecretKey(properties.getDesKey(), secretKey, "DES"));
        }
        if (type == CryptoType.AES) {
            return AesUtil.encryptToBase64(jsonData, checkSecretKey(properties.getAesKey(), secretKey, "AES"));
        }
        if (type == CryptoType.RSA) {
            String privateKey = (String) Objects.requireNonNull(properties.getRsaPrivateKey());
            return RsaUtil.encryptByPrivateKeyToBase64(privateKey, jsonData);
        }
        throw new EncryptBodyFailException();
    }
 
    public static byte[] decryptData(ApiCryptoProperties properties, byte[] bodyData, CryptoInfoBean infoBean) {
        CryptoType type = infoBean.getType();
        if (type == null) {
            throw new EncryptMethodNotFoundException();
        }
        String secretKey = infoBean.getSecretKey();
        if (type == CryptoType.AES) {
            return AesUtil.decryptFormBase64(bodyData, checkSecretKey(properties.getAesKey(), secretKey, "AES"));
        }
        if (type == CryptoType.DES) {
            return DesUtil.decryptFormBase64(bodyData, checkSecretKey(properties.getDesKey(), secretKey, "DES"));
        }
        if (type == CryptoType.RSA) {
            String privateKey = (String) Objects.requireNonNull(properties.getRsaPrivateKey());
            return RsaUtil.decryptFromBase64(privateKey, bodyData);
        }
        throw new EncryptMethodNotFoundException();
    }
 
    private static String checkSecretKey(String k1, String k2, String keyName) {
        if (StringUtil.isBlank(k1) && StringUtil.isBlank(k2)) {
            throw new KeyNotConfiguredException(String.format("%s key is not configured (未配置%s)", keyName, keyName));
        }
        return StringUtil.isBlank(k2) ? k1 : k2;
    }
}