From 726f38c332e2afffae525e49f0d277b3c6d83469 Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 7 Nov 2023 10:43:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0SM2=E3=80=81SM4=E7=9A=84?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=AF=86=E9=92=A5=E5=92=8C=E5=8A=A0=E8=A7=A3?= =?UTF-8?q?=E5=AF=86=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- neutrino-proxy-core/pom.xml | 11 ++ .../core/util/SmEncryptUtil.java | 103 ++++++++++++++++++ pom.xml | 2 +- 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 neutrino-proxy-core/src/main/java/org/dromara/neutrinoproxy/core/util/SmEncryptUtil.java diff --git a/neutrino-proxy-core/pom.xml b/neutrino-proxy-core/pom.xml index fc566dbf..8ba6da84 100644 --- a/neutrino-proxy-core/pom.xml +++ b/neutrino-proxy-core/pom.xml @@ -24,12 +24,23 @@ io.netty netty-all + + + org.bouncycastle + bcprov-jdk15to18 + + cn.hutool hutool-core ${hutool.version} + + cn.hutool + hutool-crypto + ${hutool.version} + diff --git a/neutrino-proxy-core/src/main/java/org/dromara/neutrinoproxy/core/util/SmEncryptUtil.java b/neutrino-proxy-core/src/main/java/org/dromara/neutrinoproxy/core/util/SmEncryptUtil.java new file mode 100644 index 00000000..8bf5ab86 --- /dev/null +++ b/neutrino-proxy-core/src/main/java/org/dromara/neutrinoproxy/core/util/SmEncryptUtil.java @@ -0,0 +1,103 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.dromara.neutrinoproxy.core.util; + +import cn.hutool.core.util.HexUtil; +import cn.hutool.crypto.SecureUtil; +import cn.hutool.crypto.SmUtil; +import lombok.Data; + +import javax.crypto.SecretKey; +import java.security.KeyPair; + +/** + * 国密算法加解密工具 + * @author: az + * @date: 2023/11/07 + */ +public class SmEncryptUtil { + + record Sm2KeyPairRecord(String privateKey, String publicKey) { + } + + /** + * 生成SM2密钥对 + * @return + */ + public static Sm2KeyPairRecord generateSm2KeyPair() { + KeyPair keyPair = SecureUtil.generateKeyPair("SM2"); + byte[] privateKeyBytes = keyPair.getPrivate().getEncoded(); + byte[] publicKeyBytes = keyPair.getPublic().getEncoded(); + + String privateKey = HexUtil.encodeHexStr(privateKeyBytes); + String publicKey = HexUtil.encodeHexStr(publicKeyBytes); + + return new Sm2KeyPairRecord(privateKey, publicKey); + } + + /** + * 使用SM2算法对数据进行加密 + * @param publicKey 加密所需的公钥 + * @param data 需要加密的数据 + * @return 加密后的字节数组 + */ + public static byte[] encryptBySm2(String publicKey, byte[] data) { + return SmUtil.sm2(null, publicKey).encrypt(data); + } + + /** + * 使用SM2算法对数据进行解密 + * @param privateKey 解密所需私钥 + * @param data 需要解密的数据 + * @return 解密后的字节数组 + */ + public static byte[] decryptBySm2(String privateKey, byte[] data) { + return SmUtil.sm2(privateKey, null).decrypt(data); + } + + public static byte[] generateSm4Key() { + SecretKey key = SecureUtil.generateKey("AES", 128); + return key.getEncoded(); + } + + /** + * 使用SM4算法加密数据 + * @param key 密钥 + * @param data 待加密的数据 + * @return 已加密的数据 + */ + public static byte[] encryptBySm4(byte[] key, byte[] data) { + return SmUtil.sm4(key).encrypt(data); + } + + /** + * 使用SM4算法解密数据 + * @param key 密钥 + * @param encryptedData 已加密数据 + * @return 解密后的数据 + */ + public static byte[] decryptBySm4(byte[] key, byte[] encryptedData) { + return SmUtil.sm4(key).decrypt(encryptedData); + } + +} diff --git a/pom.xml b/pom.xml index a5bbabfa..28237270 100644 --- a/pom.xml +++ b/pom.xml @@ -1 +1 @@ - 4.0.0 org.noear solon-parent 2.5.12 org.dromara.neutrino-proxy neutrino-proxy pom ${revision} neutrino-proxy-core neutrino-proxy-client neutrino-proxy-server UTF-8 UTF-8 UTF-8 2.0.1-SNAPSHOT 0.9.28 21 3.8.0 1.1.0 io.netty netty-all 4.1.100.Final org.yaml snakeyaml 1.33 junit junit 4.12 test org.apache.commons commons-lang3 3.9 com.google.guava guava 28.0-jre commons-fileupload commons-fileupload 1.3.1 com.h2database h2 2.2.224 mysql mysql-connector-java 8.0.33 org.mariadb.jdbc mariadb-java-client 2.7.4 com.zaxxer HikariCP 4.0.3 org.dromara.solon-plugins job-solon-plugin 0.1.1 cn.hutool hutool-core org.noear solon.logging.logback org.projectlombok lombok provided org.apache.commons commons-lang3 com.google.guava guava org.noear solon-test test src/main/resources org.apache.maven.plugins maven-compiler-plugin ${maven-compiler-plugin.version} ${java.version} ${java.version} UTF-8 org.projectlombok lombok ${lombok.version} org.codehaus.mojo flatten-maven-plugin ${maven-flatten.version} true resolveCiFriendliesOnly flatten process-resources flatten flatten.clean clean clean tencent https://mirrors.cloud.tencent.com/nexus/repository/maven-public/ false sonatype-nexus-snapshots Sonatype Nexus Snapshots https://oss.sonatype.org/content/repositories/snapshots false sonatype-nexus-snapshots Sonatype Nexus Snapshots https://oss.sonatype.org/content/repositories/snapshots false native org.noear solon-maven-plugin ${solon.version} process-aot process-aot org.codehaus.plexus plexus-utils 3.5.1 org.graalvm.buildtools native-maven-plugin ${native.version} true add-reachability-metadata add-reachability-metadata \ No newline at end of file + 4.0.0 org.noear solon-parent 2.5.12 org.dromara.neutrino-proxy neutrino-proxy pom ${revision} neutrino-proxy-core neutrino-proxy-client neutrino-proxy-server UTF-8 UTF-8 UTF-8 2.0.1-SNAPSHOT 0.9.28 21 3.8.0 1.1.0 io.netty netty-all 4.1.100.Final org.yaml snakeyaml 1.33 junit junit 4.12 test org.apache.commons commons-lang3 3.9 com.google.guava guava 28.0-jre commons-fileupload commons-fileupload 1.3.1 com.h2database h2 2.2.224 mysql mysql-connector-java 8.0.33 org.mariadb.jdbc mariadb-java-client 2.7.4 com.zaxxer HikariCP 4.0.3 org.bouncycastle bcprov-jdk15to18 1.69 org.dromara.solon-plugins job-solon-plugin 0.1.1 cn.hutool hutool-core org.noear solon.logging.logback org.projectlombok lombok provided org.apache.commons commons-lang3 com.google.guava guava org.noear solon-test test src/main/resources org.apache.maven.plugins maven-compiler-plugin ${maven-compiler-plugin.version} ${java.version} ${java.version} UTF-8 org.projectlombok lombok ${lombok.version} org.codehaus.mojo flatten-maven-plugin ${maven-flatten.version} true resolveCiFriendliesOnly flatten process-resources flatten flatten.clean clean clean tencent https://mirrors.cloud.tencent.com/nexus/repository/maven-public/ false sonatype-nexus-snapshots Sonatype Nexus Snapshots https://oss.sonatype.org/content/repositories/snapshots false sonatype-nexus-snapshots Sonatype Nexus Snapshots https://oss.sonatype.org/content/repositories/snapshots false native org.noear solon-maven-plugin ${solon.version} process-aot process-aot org.codehaus.plexus plexus-utils 3.5.1 org.graalvm.buildtools native-maven-plugin ${native.version} true add-reachability-metadata add-reachability-metadata \ No newline at end of file