From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001 From: xiejun <xiejun@vci-tech.com> Date: 星期五, 01 十一月 2024 15:11:19 +0800 Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成" --- Source/BladeX-Tool/blade-starter-api-crypto/src/main/java/org/springblade/core/api/crypto/util/ApiCryptoUtil.java | 124 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 124 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-starter-api-crypto/src/main/java/org/springblade/core/api/crypto/util/ApiCryptoUtil.java b/Source/BladeX-Tool/blade-starter-api-crypto/src/main/java/org/springblade/core/api/crypto/util/ApiCryptoUtil.java new file mode 100644 index 0000000..fa66b67 --- /dev/null +++ b/Source/BladeX-Tool/blade-starter-api-crypto/src/main/java/org/springblade/core/api/crypto/util/ApiCryptoUtil.java @@ -0,0 +1,124 @@ +package org.springblade.core.api.crypto.util; + +import org.springblade.core.api.crypto.annotation.decrypt.ApiDecrypt; +import org.springblade.core.api.crypto.annotation.encrypt.ApiEncrypt; +import org.springblade.core.api.crypto.bean.CryptoInfoBean; +import org.springblade.core.api.crypto.config.ApiCryptoProperties; +import org.springblade.core.api.crypto.enums.CryptoType; +import org.springblade.core.api.crypto.exception.EncryptBodyFailException; +import org.springblade.core.api.crypto.exception.EncryptMethodNotFoundException; +import org.springblade.core.api.crypto.exception.KeyNotConfiguredException; +import org.springblade.core.tool.utils.*; +import org.springframework.core.MethodParameter; +import org.springframework.lang.Nullable; + +import java.util.Objects; + +/** + * <p>杈呭姪妫�娴嬪伐鍏风被</p> + * + * @author licoy.cn, L.cm + */ +public class ApiCryptoUtil { + + /** + * 鑾峰彇鏂规硶鎺у埗鍣ㄤ笂鐨勫姞瀵嗘敞瑙d俊鎭� + * + * @param methodParameter 鎺у埗鍣ㄦ柟娉� + * @return 鍔犲瘑娉ㄨВ淇℃伅 + */ + @Nullable + public static CryptoInfoBean getEncryptInfo(MethodParameter methodParameter) { + ApiEncrypt encryptBody = ClassUtil.getAnnotation(methodParameter.getMethod(), ApiEncrypt.class); + if (encryptBody == null) { + return null; + } + return new CryptoInfoBean(encryptBody.value(), encryptBody.secretKey()); + } + + /** + * 鑾峰彇鏂规硶鎺у埗鍣ㄤ笂鐨勮В瀵嗘敞瑙d俊鎭� + * + * @param methodParameter 鎺у埗鍣ㄦ柟娉� + * @return 鍔犲瘑娉ㄨВ淇℃伅 + */ + @Nullable + public static CryptoInfoBean getDecryptInfo(MethodParameter methodParameter) { + ApiDecrypt decryptBody = ClassUtil.getAnnotation(methodParameter.getMethod(), ApiDecrypt.class); + if (decryptBody == null) { + return null; + } + return new CryptoInfoBean(decryptBody.value(), decryptBody.secretKey()); + } + + /** + * 閫夋嫨鍔犲瘑鏂瑰紡骞惰繘琛屽姞瀵� + * + * @param jsonData json 鏁版嵁 + * @param infoBean 鍔犲瘑淇℃伅 + * @return 鍔犲瘑缁撴灉 + */ + 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) { + secretKey = ApiCryptoUtil.checkSecretKey(properties.getDesKey(), secretKey, "DES"); + return DesUtil.encryptToBase64(jsonData, secretKey); + } + if (type == CryptoType.AES) { + secretKey = ApiCryptoUtil.checkSecretKey(properties.getAesKey(), secretKey, "AES"); + return AesUtil.encryptToBase64(jsonData, secretKey); + } + if (type == CryptoType.RSA) { + String privateKey = Objects.requireNonNull(properties.getRsaPrivateKey()); + return RsaUtil.encryptByPrivateKeyToBase64(privateKey, jsonData); + } + throw new EncryptBodyFailException(); + } + + /** + * 閫夋嫨鍔犲瘑鏂瑰紡骞惰繘琛岃В瀵� + * + * @param bodyData byte array + * @param infoBean 鍔犲瘑淇℃伅 + * @return 瑙e瘑缁撴灉 + */ + 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) { + secretKey = ApiCryptoUtil.checkSecretKey(properties.getAesKey(), secretKey, "AES"); + return AesUtil.decryptFormBase64(bodyData, secretKey); + } + if (type == CryptoType.DES) { + secretKey = ApiCryptoUtil.checkSecretKey(properties.getDesKey(), secretKey, "DES"); + return DesUtil.decryptFormBase64(bodyData, secretKey); + } + if (type == CryptoType.RSA) { + String privateKey = Objects.requireNonNull(properties.getRsaPrivateKey()); + return RsaUtil.decryptFromBase64(privateKey, bodyData); + } + throw new EncryptMethodNotFoundException(); + } + + /** + * 妫�楠岀閽� + * + * @param k1 閰嶇疆鐨勭閽� + * @param k2 娉ㄨВ涓婄殑绉侀挜 + * @param keyName key鍚嶇О + * @return 绉侀挜 + */ + 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; + } +} -- Gitblit v1.9.3