添加SM2、SM4的生成密钥和加解密方法

This commit is contained in:
=
2023-11-07 10:43:26 +08:00
parent 09f934bdd0
commit 726f38c332
3 changed files with 115 additions and 1 deletions
+11
View File
@@ -24,12 +24,23 @@
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15to18</artifactId>
</dependency>
<!--hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
<build>
@@ -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);
}
}
+1 -1
View File
@@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>␍␍ <parent><groupId>org.noear</groupId><artifactId>solon-parent</artifactId><version>2.5.12</version><relativePath /></parent>␍␍ <groupId>org.dromara.neutrino-proxy</groupId><artifactId>neutrino-proxy</artifactId><packaging>pom</packaging><version>${revision}</version>␍␍ <modules><module>neutrino-proxy-core</module><module>neutrino-proxy-client</module><module>neutrino-proxy-server</module></modules>␍␍ <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><maven.compiler.encoding>UTF-8</maven.compiler.encoding><revision>2.0.1-SNAPSHOT</revision>␍␍ <native.version>0.9.28</native.version>␍␍ <java.version>21</java.version><maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version><maven-flatten.version>1.1.0</maven-flatten.version></properties>␍␍ <dependencyManagement><dependencies><dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.100.Final</version></dependency><dependency><groupId>org.yaml</groupId><artifactId>snakeyaml</artifactId><version>1.33</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>28.0-jre</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><version>2.2.224</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><dependency><groupId>org.mariadb.jdbc</groupId><artifactId>mariadb-java-client</artifactId><version>2.7.4</version></dependency><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId><version>4.0.3</version></dependency><dependency><groupId>org.dromara.solon-plugins</groupId><artifactId>job-solon-plugin</artifactId><version>0.1.1</version><exclusions><exclusion><groupId>cn.hutool</groupId><artifactId>hutool-core</artifactId></exclusion></exclusions></dependency></dependencies></dependencyManagement>␍␍ <dependencies><dependency><groupId>org.noear</groupId><artifactId>solon.logging.logback</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId></dependency>␍␍ <dependency><groupId>org.noear</groupId><artifactId>solon-test</artifactId><scope>test</scope></dependency></dependencies>␍␍ <build><resources><resource><directory>src/main/resources</directory></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>${maven-compiler-plugin.version}</version><configuration><source>${java.version}</source><target>${java.version}</target><encoding>UTF-8</encoding><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></path></annotationProcessorPaths></configuration></plugin><!-- 添加flatten-maven-plugin插件 --><plugin><groupId>org.codehaus.mojo</groupId><artifactId>flatten-maven-plugin</artifactId><version>${maven-flatten.version}</version><configuration><updatePomFile>true</updatePomFile><flattenMode>resolveCiFriendliesOnly</flattenMode></configuration><executions><execution><id>flatten</id><phase>process-resources</phase><goals><goal>flatten</goal></goals></execution><execution><id>flatten.clean</id><phase>clean</phase><goals><goal>clean</goal></goals></execution></executions></plugin></plugins></build>␍␍ <repositories><repository><id>tencent</id><url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>sonatype-nexus-snapshots</id><name>Sonatype Nexus Snapshots</name><url>https://oss.sonatype.org/content/repositories/snapshots</url><releases><enabled>false</enabled></releases></repository></repositories><pluginRepositories><pluginRepository><id>sonatype-nexus-snapshots</id><name>Sonatype Nexus Snapshots</name><url>https://oss.sonatype.org/content/repositories/snapshots</url><releases><enabled>false</enabled></releases></pluginRepository></pluginRepositories>␍␍ <profiles><profile><id>native</id><build><plugins><plugin><groupId>org.noear</groupId><artifactId>solon-maven-plugin</artifactId><version>${solon.version}</version><executions><execution><id>process-aot</id><goals><goal>process-aot</goal></goals></execution></executions>␍␍ <dependencies><dependency><groupId>org.codehaus.plexus</groupId><artifactId>plexus-utils</artifactId><version>3.5.1</version></dependency></dependencies></plugin><plugin><groupId>org.graalvm.buildtools</groupId><artifactId>native-maven-plugin</artifactId><version>${native.version}</version><!-- 使用graalvm提供的可达性元数据,很多第三方库就直接可以构建成可执行文件了 --><configuration><metadataRepository><enabled>true</enabled></metadataRepository></configuration><executions><execution><id>add-reachability-metadata</id><goals><goal>add-reachability-metadata</goal></goals></execution></executions></plugin></plugins></build></profile></profiles></project>␍␍
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>␍␍ <parent><groupId>org.noear</groupId><artifactId>solon-parent</artifactId><version>2.5.12</version><relativePath /></parent>␍␍ <groupId>org.dromara.neutrino-proxy</groupId><artifactId>neutrino-proxy</artifactId><packaging>pom</packaging><version>${revision}</version>␍␍ <modules><module>neutrino-proxy-core</module><module>neutrino-proxy-client</module><module>neutrino-proxy-server</module></modules>␍␍ <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><maven.compiler.encoding>UTF-8</maven.compiler.encoding><revision>2.0.1-SNAPSHOT</revision>␍␍ <native.version>0.9.28</native.version>␍␍ <java.version>21</java.version><maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version><maven-flatten.version>1.1.0</maven-flatten.version></properties>␍␍ <dependencyManagement><dependencies><dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.100.Final</version></dependency><dependency><groupId>org.yaml</groupId><artifactId>snakeyaml</artifactId><version>1.33</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>28.0-jre</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><version>2.2.224</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><dependency><groupId>org.mariadb.jdbc</groupId><artifactId>mariadb-java-client</artifactId><version>2.7.4</version></dependency><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId><version>4.0.3</version></dependency><dependency> <groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15to18</artifactId><version>1.69</version></dependency><dependency> <groupId>org.dromara.solon-plugins</groupId><artifactId>job-solon-plugin</artifactId><version>0.1.1</version><exclusions><exclusion><groupId>cn.hutool</groupId><artifactId>hutool-core</artifactId></exclusion></exclusions></dependency></dependencies></dependencyManagement>␍␍ <dependencies><dependency><groupId>org.noear</groupId><artifactId>solon.logging.logback</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId></dependency>␍␍ <dependency><groupId>org.noear</groupId><artifactId>solon-test</artifactId><scope>test</scope></dependency></dependencies>␍␍ <build><resources><resource><directory>src/main/resources</directory></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>${maven-compiler-plugin.version}</version><configuration><source>${java.version}</source><target>${java.version}</target><encoding>UTF-8</encoding><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></path></annotationProcessorPaths></configuration></plugin><!-- 添加flatten-maven-plugin插件 --><plugin><groupId>org.codehaus.mojo</groupId><artifactId>flatten-maven-plugin</artifactId><version>${maven-flatten.version}</version><configuration><updatePomFile>true</updatePomFile><flattenMode>resolveCiFriendliesOnly</flattenMode></configuration><executions><execution><id>flatten</id><phase>process-resources</phase><goals><goal>flatten</goal></goals></execution><execution><id>flatten.clean</id><phase>clean</phase><goals><goal>clean</goal></goals></execution></executions></plugin></plugins></build>␍␍ <repositories><repository><id>tencent</id><url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>sonatype-nexus-snapshots</id><name>Sonatype Nexus Snapshots</name><url>https://oss.sonatype.org/content/repositories/snapshots</url><releases><enabled>false</enabled></releases></repository></repositories><pluginRepositories><pluginRepository><id>sonatype-nexus-snapshots</id><name>Sonatype Nexus Snapshots</name><url>https://oss.sonatype.org/content/repositories/snapshots</url><releases><enabled>false</enabled></releases></pluginRepository></pluginRepositories>␍␍ <profiles><profile><id>native</id><build><plugins><plugin><groupId>org.noear</groupId><artifactId>solon-maven-plugin</artifactId><version>${solon.version}</version><executions><execution><id>process-aot</id><goals><goal>process-aot</goal></goals></execution></executions>␍␍ <dependencies><dependency><groupId>org.codehaus.plexus</groupId><artifactId>plexus-utils</artifactId><version>3.5.1</version></dependency></dependencies></plugin><plugin><groupId>org.graalvm.buildtools</groupId><artifactId>native-maven-plugin</artifactId><version>${native.version}</version><!-- 使用graalvm提供的可达性元数据,很多第三方库就直接可以构建成可执行文件了 --><configuration><metadataRepository><enabled>true</enabled></metadataRepository></configuration><executions><execution><id>add-reachability-metadata</id><goals><goal>add-reachability-metadata</goal></goals></execution></executions></plugin></plugins></build></profile></profiles></project>␍␍