diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/crypto/ApiCryptoSm4.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/crypto/ApiCryptoSm4.java new file mode 100644 index 000000000..75da031cb --- /dev/null +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/crypto/ApiCryptoSm4.java @@ -0,0 +1,22 @@ +package com.pig4cloud.pigx.common.api.encrypt.annotation.crypto; + +import com.pig4cloud.pigx.common.api.encrypt.annotation.decrypt.ApiDecrypt; +import com.pig4cloud.pigx.common.api.encrypt.annotation.encrypt.ApiEncrypt; +import com.pig4cloud.pigx.common.api.encrypt.enums.EncryptType; + +import java.lang.annotation.*; + +/** + * Sm4加密解密注解 + * + * @author lengleng + */ +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ApiEncrypt(EncryptType.SM4) +@ApiDecrypt(EncryptType.SM4) +public @interface ApiCryptoSm4 { + +} diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptSm4.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptSm4.java new file mode 100644 index 000000000..b8caffd41 --- /dev/null +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptSm4.java @@ -0,0 +1,28 @@ +package com.pig4cloud.pigx.common.api.encrypt.annotation.decrypt; + +import com.pig4cloud.pigx.common.api.encrypt.enums.EncryptType; +import org.springframework.core.annotation.AliasFor; + +import java.lang.annotation.*; + +/** + * sm4 解密 + * + * @author lengleng + * @version 2023/8/30 + * @see ApiDecrypt + */ +@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@ApiDecrypt(EncryptType.SM4) +public @interface ApiDecryptSm4 { + + /** + * Alias for {@link ApiDecrypt#secretKey()}. + * @return {String} + */ + @AliasFor(annotation = ApiDecrypt.class) + String secretKey() default ""; + +} diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/encrypt/ApiEncryptDes.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/encrypt/ApiEncryptDes.java index c4345337f..11dd120fe 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/encrypt/ApiEncryptDes.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/encrypt/ApiEncryptDes.java @@ -6,7 +6,7 @@ import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; /** - * des 加埋 + * des 加密 * * @author licoy.cn * @author L.cm diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/encrypt/ApiEncryptSm4.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/encrypt/ApiEncryptSm4.java new file mode 100644 index 000000000..8480acf24 --- /dev/null +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/encrypt/ApiEncryptSm4.java @@ -0,0 +1,28 @@ +package com.pig4cloud.pigx.common.api.encrypt.annotation.encrypt; + +import com.pig4cloud.pigx.common.api.encrypt.enums.EncryptType; +import org.springframework.core.annotation.AliasFor; + +import java.lang.annotation.*; + +/** + * sm4 加密 + * + * @author lengleng + * @version 2023/8/30 + * @see ApiEncrypt + */ +@Target({ ElementType.METHOD, ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@ApiEncrypt(EncryptType.SM4) +public @interface ApiEncryptSm4 { + + /** + * Alias for {@link ApiEncrypt#secretKey()}. + * @return {String} + */ + @AliasFor(annotation = ApiEncrypt.class) + String secretKey() default ""; + +} diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/config/ApiEncryptProperties.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/config/ApiEncryptProperties.java index 2e537620d..8e2733bc7 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/config/ApiEncryptProperties.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/config/ApiEncryptProperties.java @@ -46,6 +46,11 @@ public class ApiEncryptProperties { */ private String desKey; + /** + * sm4 密钥 + */ + private String sm4Key; + /** * rsa 私钥 */ diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/core/DefaultSecretKeyResolver.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/core/DefaultSecretKeyResolver.java index 54705382a..77873894e 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/core/DefaultSecretKeyResolver.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/core/DefaultSecretKeyResolver.java @@ -18,14 +18,12 @@ public class DefaultSecretKeyResolver implements ISecretKeyResolver { @Override public String getSecretKey(HttpServletRequest request, EncryptType encryptType) { - switch (encryptType) { - case DES: - return properties.getDesKey(); - case RSA: - return properties.getRsaPrivateKey(); - default: - return properties.getAesKey(); - } + return switch (encryptType) { + case SM4 -> properties.getSm4Key(); + case DES -> properties.getDesKey(); + case RSA -> properties.getRsaPrivateKey(); + default -> properties.getAesKey(); + }; } } diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/enums/EncryptType.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/enums/EncryptType.java index 3481b236b..a0ca5daa9 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/enums/EncryptType.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/enums/EncryptType.java @@ -14,6 +14,6 @@ public enum EncryptType { /** * 加密方式 */ - DES, AES, RSA + DES, AES, RSA, SM4 } diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/util/ApiCryptoUtil.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/util/ApiCryptoUtil.java index 64612fcfd..49eeed1e6 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/util/ApiCryptoUtil.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/util/ApiCryptoUtil.java @@ -1,9 +1,11 @@ package com.pig4cloud.pigx.common.api.encrypt.util; +import cn.hutool.core.util.HexUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.Mode; import cn.hutool.crypto.Padding; import cn.hutool.crypto.SecureUtil; +import cn.hutool.crypto.SmUtil; import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.symmetric.AES; import com.pig4cloud.pigx.common.api.encrypt.annotation.decrypt.ApiDecrypt; @@ -98,8 +100,12 @@ public class ApiCryptoUtil { return aes.encryptBase64(jsonData); } if (type == EncryptType.RSA) { - return SecureUtil.rsa(secretKey.getBytes(StandardCharsets.UTF_8), null).encryptBase64(jsonData, - KeyType.PrivateKey); + return SecureUtil.rsa(secretKey.getBytes(StandardCharsets.UTF_8), null) + .encryptBase64(jsonData, KeyType.PrivateKey); + } + + if (type == EncryptType.SM4) { + return SmUtil.sm4(HexUtil.decodeHex(secretKey)).encryptHex(jsonData); } throw new EncryptBodyFailException(); } @@ -134,9 +140,16 @@ public class ApiCryptoUtil { return SecureUtil.des(secretKey.getBytes(StandardCharsets.UTF_8)).decrypt(bodyData); } if (type == EncryptType.RSA) { - return SecureUtil.rsa(secretKey.getBytes(StandardCharsets.UTF_8), null).decrypt(bodyData, - KeyType.PrivateKey); + return SecureUtil.rsa(secretKey.getBytes(StandardCharsets.UTF_8), null) + .decrypt(bodyData, KeyType.PrivateKey); } + + if (type == EncryptType.SM4) { + return SmUtil.sm4(HexUtil.decodeHex(secretKey)) + .decryptStr(StrUtil.str(bodyData, Charset.defaultCharset())) + .getBytes(); + } + throw new EncryptMethodNotFoundException(); } diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/PasswordDecoderFilter.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/PasswordDecoderFilter.java index 5d567b6ed..da3ad0805 100644 --- a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/PasswordDecoderFilter.java +++ b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/PasswordDecoderFilter.java @@ -18,10 +18,9 @@ package com.pig4cloud.pigx.gateway.filter; import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.HexUtil; import cn.hutool.core.util.StrUtil; -import cn.hutool.crypto.Mode; -import cn.hutool.crypto.Padding; -import cn.hutool.crypto.symmetric.AES; +import cn.hutool.crypto.SmUtil; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; @@ -54,8 +53,6 @@ import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; import java.nio.charset.Charset; import java.util.List; import java.util.Map; @@ -159,15 +156,10 @@ public class PasswordDecoderFilter extends AbstractGatewayFilterFactory { */ private Function decryptAES() { return s -> { - // 构建前端对应解密AES 因子 - AES aes = new AES(Mode.CFB, Padding.NoPadding, - new SecretKeySpec(gatewayConfig.getEncodeKey().getBytes(), KEY_ALGORITHM), - new IvParameterSpec(gatewayConfig.getEncodeKey().getBytes())); - // 获取请求密码并解密 Map inParamsMap = HttpUtil.decodeParamMap((String) s, CharsetUtil.CHARSET_UTF_8); if (inParamsMap.containsKey(PASSWORD)) { - String password = aes.decryptStr(inParamsMap.get(PASSWORD)); + String password = SmUtil.sm4(HexUtil.decodeHex(gatewayConfig.getEncodeKey())).decryptStr(inParamsMap.get(PASSWORD)); // 返回修改后报文字符 inParamsMap.put(PASSWORD, password); }