From a07c71f1a06aa5f7ea11fb35bb5fdcf8ac556845 Mon Sep 17 00:00:00 2001 From: maimaishu <14610861+maimaishu@user.noreply.gitee.com> Date: Wed, 7 Jan 2026 08:44:35 +0800 Subject: [PATCH] =?UTF-8?q?[+]=E5=A2=9E=E5=8A=A0=E5=9B=BD=E5=AF=86SM4?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=8A=A0=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zt-framework/zt-common/pom.xml | 7 ++ .../util/security/CryptoSignatureUtils.java | 103 +++++++++++++++++- 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/zt-framework/zt-common/pom.xml b/zt-framework/zt-common/pom.xml index db7d9ceb..216e0c4b 100644 --- a/zt-framework/zt-common/pom.xml +++ b/zt-framework/zt-common/pom.xml @@ -163,6 +163,13 @@ mockito-core test + + + org.bouncycastle + bcprov-jdk18on + 1.78.1 + compile + diff --git a/zt-framework/zt-common/src/main/java/com/zt/plat/framework/common/util/security/CryptoSignatureUtils.java b/zt-framework/zt-common/src/main/java/com/zt/plat/framework/common/util/security/CryptoSignatureUtils.java index b0d9be83..6546cb55 100644 --- a/zt-framework/zt-common/src/main/java/com/zt/plat/framework/common/util/security/CryptoSignatureUtils.java +++ b/zt-framework/zt-common/src/main/java/com/zt/plat/framework/common/util/security/CryptoSignatureUtils.java @@ -1,8 +1,10 @@ package com.zt.plat.framework.common.util.security; import cn.hutool.crypto.SecureUtil; +import cn.hutool.crypto.symmetric.SM4; import com.zt.plat.framework.common.util.json.JsonUtils; - +import lombok.extern.slf4j.Slf4j; +import org.bouncycastle.jce.provider.BouncyCastleProvider; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; @@ -10,11 +12,13 @@ import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.security.Security; import java.util.*; /** * 通用的签名、加解密工具类 */ +@Slf4j public final class CryptoSignatureUtils { public static final String ENCRYPT_TYPE_AES = "AES"; @@ -25,6 +29,15 @@ public final class CryptoSignatureUtils { private static final String AES_TRANSFORMATION = "AES/ECB/PKCS5Padding"; public static final String SIGNATURE_FIELD = "signature"; + private static final String CHARSET = "UTF-8"; + + //@Value("${sa.encrypt.sm4.key}") + private static String SM4_KEY = "1234567890123456"; + + static { + Security.addProvider(new BouncyCastleProvider()); + } + private CryptoSignatureUtils() { } @@ -215,4 +228,92 @@ public final class CryptoSignatureUtils { } return sb.toString(); } + + +//-------------------------------- 国密方式2 ------------------------------------------------------- + + /** + * 加密 + */ + public static String enCode(String data) { + try { + // 第一步: SM4 加密 + SM4 sm4 = new SM4(hexToBytes(stringToHex(SM4_KEY))); + String encryptHex = sm4.encryptHex(data); + + // 第二步: Base64 编码 + return new String(Base64.getEncoder().encode(encryptHex.getBytes(CHARSET)), CHARSET); + + } catch (Exception e) { + log.error("国密加密失败{}",e.getMessage(),e); + System.out.println("加密失败"+e); + return ""; + } + } + + /** + * 解密 + */ + public static String deCode(String data) { + try { + + // 第一步: Base64 解码 + byte[] base64Decode = Base64.getDecoder().decode(data); + + // 第二步: SM4 解密 + SM4 sm4 = new SM4(hexToBytes(stringToHex(SM4_KEY))); + return sm4.decryptStr(new String(base64Decode)); + + } catch (Exception e) { + log.error("国密解密失败{}",e.getMessage(),e); + return ""; + } + } + + + + public static String stringToHex(String input) { + char[] chars = input.toCharArray(); + StringBuilder hex = new StringBuilder(); + for (char c : chars) { + hex.append(Integer.toHexString((int) c)); + } + return hex.toString(); + } + + /** + * 16 进制串转字节数组 + * + * @param hex 16进制字符串 + * @return byte数组 + */ + public static byte[] hexToBytes(String hex) { + int length = hex.length(); + byte[] result; + if (length % 2 == 1) { + length++; + result = new byte[(length / 2)]; + hex = "0" + hex; + } else { + result = new byte[(length / 2)]; + } + int j = 0; + for (int i = 0; i < length; i += 2) { + result[j] = hexToByte(hex.substring(i, i + 2)); + j++; + } + return result; + } + + /** + * 16 进制字符转字节 + * + * @param hex 16进制字符 0x00到0xFF + * @return byte + */ + private static byte hexToByte(String hex) { + return (byte) Integer.parseInt(hex, 16); + } + + }