引入min-dev-java库,封装了keymanager

This commit is contained in:
free will committed 2021-06-22 22:48:12 +08:00
1 parent 4f97e55f94
commit 5c515bd334
19 files changed
+873

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
# 说明
本项目用于连接vpn-management
# 模块划分
## Security
封装sm2,方便调用
## TCPNet
封装TCPClient
## BC_API
封装向vpn-management获取数据
# 相关项目链接
本项目源码:https://gitea.qjm253.cn/wefree/min-vpn-client_v2
后台源码:https://gitea.qjm253.cn/PKUSZ-future-network-lab/vpn-management-server
后台API:http://121.15.171.82:8000/
Binary file not shown.
+75
View File
@@ -13,4 +13,79 @@
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<resources>
<resource>
<directory>libs</directory>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptors>
<!--assembly配置文件路径,注意需要在项目中新建文件properties/assembly-release.xml-->
<descriptor>properties/assembly-release.xml</descriptor>
</descriptors>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>min.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>libs/*.jar</Class-Path>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
+88
View File
@@ -0,0 +1,88 @@
<assembly xmlns = "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0
http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>release</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<!-- <dependencySets>-->
<!-- <dependencySet>-->
<!-- <unpack>false</unpack>-->
<!-- <useProjectArtifact>true</useProjectArtifact>-->
<!-- <outputDirectory>lib</outputDirectory>-->
<!-- <scope>provided</scope>-->
<!-- </dependencySet>-->
<!-- <dependencySet>-->
<!-- <unpack>false</unpack>-->
<!-- <useProjectArtifact>true</useProjectArtifact>-->
<!-- <outputDirectory>lib</outputDirectory>-->
<!-- <scope>system</scope>-->
<!-- </dependencySet>-->
<!-- <dependencySet>-->
<!-- <unpack>false</unpack>-->
<!-- <useProjectArtifact>true</useProjectArtifact>-->
<!-- <outputDirectory>lib</outputDirectory>-->
<!-- <scope>runtime</scope>-->
<!-- </dependencySet>-->
<!-- </dependencySets>-->
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<excludes>
<!-- 排除这些在kafkatool2/lib存在的相关依赖-->
<!-- <exclude>com.google.code.gson:gson</exclude>-->
<!-- <exclude>org.apache.zookeeper:zookeeper</exclude>-->
<!-- &lt;!&ndash; 这些应该用不到&ndash;&gt;-->
<!-- <exclude>org.apache.hadoop:hadoop-annotations</exclude>-->
<!-- <exclude>org.apache.hadoop:hadoop-auth</exclude>-->
<!-- <exclude>org.apache.hadoop:hadoop-common</exclude>-->
<!-- <exclude>org.apache.hadoop:hadoop-core</exclude>-->
<!-- <exclude>org.apache.hbase:hbase-annotations</exclude>-->
<!-- <exclude>org.apache.hbase:hbase-client</exclude>-->
<!-- <exclude>org.apache.hbase:hbase-protocol</exclude>-->
<!-- <exclude>org.apache.hbase:hbase-common</exclude>-->
<!-- <exclude>org.apache.kudu:kudu-client</exclude>-->
<!-- <exclude>io.netty:netty-all</exclude>-->
<!-- <exclude>javax.mail:mail</exclude>-->
<!-- <exclude>javax.servlet.jsp:jsp-api</exclude>-->
<!-- <exclude>javax.servlet:servlet-api</exclude>-->
<!--&lt;!&ndash; <exclude>junit:junit</exclude>&ndash;&gt;-->
<!-- <exclude>tomcat:jasper-compiler</exclude>-->
<!-- <exclude>tomcat:jasper-runtime</exclude>-->
<!-- <exclude>hsqldb:hsqldb</exclude>-->
<!-- <exclude>com.github.stephenc.findbugs:findbugs-annotations</exclude>-->
<!-- <exclude>com.google.code.findbugs:jsr</exclude>-->
<!-- <exclude>com.sun.jersey:jersey-core</exclude>-->
<!-- <exclude>com.sun.jersey:jersey-json</exclude>-->
<!-- <exclude>commons-httpclient:commons-httpclient</exclude>-->
<!-- <exclude>org.apache.curator:curator-client</exclude>-->
<!-- <exclude>org.apache.curator:curator-framework</exclude>-->
<!-- <exclude>org.apache.curator:curator-recipes</exclude>-->
<!-- <exclude>org.mortbay.jetty:jetty</exclude>-->
<!-- <exclude>org.mortbay.jetty:jetty-util</exclude>-->
<!-- wefree added: 去掉这些依赖,以解决与安卓编译时候的库冲突 -->
<exclude>org.jetbrains.kotlin</exclude>
<exclude>org.jetbrains.kotlin:kotlin-stdlib</exclude>
<exclude>org.jetbrains.kotlin:kotlin-stdlib-common</exclude>
<exclude>org.jetbrains:annotations</exclude>
<!-- <exclude>org.jetbrains.kotlin:kotlin-stdlib-jdk8</exclude>-->
<!-- <exclude>org.jetbrains.kotlin:kotlin-stdlib-reflect</exclude>-->
<!-- <exclude>org.jetbrains.kotlin:kotlin-android-extensions-runtime</exclude>-->
<!-- <exclude>org.jetbrains.kotlin:kotlin-test</exclude>-->
<!-- <exclude>org.jetbrains.kotlin:kotlin-test-junit</exclude>-->
<!-- <exclude>org.jetbrains.kotlin:kotlin-stdlib-js</exclude>-->
</excludes>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>
</assembly>
+307
View File
@@ -0,0 +1,307 @@
<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>
<!-- 公司或者组织的唯一标志,并且配置时生成的路径也是由此生成, 如com.companyname.project-group,maven会将该项目打成的jar包放本地路径:/com/companyname/project-group -->
<groupId>min</groupId>
<!-- 项目的唯一ID,一个groupId下面可能多个项目,就是靠artifactId来区分的 -->
<artifactId>min-vpn</artifactId>
<!-- 版本号 -->
<version>1.0</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<build>
<plugins>
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <source>1.8</source>-->
<!-- <target>1.8</target>-->
<!-- <compilerArguments>-->
<!-- <extdirs>${project.basedir}/libs</extdirs>-->
<!-- </compilerArguments>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<!-- <descriptorRefs>-->
<!-- <descriptorRef>jar-with-dependencies</descriptorRef>-->
<!-- </descriptorRefs>-->
<descriptors>
<!--assembly配置文件路径,注意需要在项目中新建文件properties/assembly-release.xml-->
<descriptor>properties/assembly-release.xml</descriptor>
</descriptors>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>min.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>libs/*.jar</Class-Path>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!--<plugin>-->
<!--<groupId>com.github.wvengen</groupId>-->
<!--<artifactId>proguard-maven-plugin</artifactId>-->
<!--&lt;!&ndash; <executions>&ndash;&gt;-->
<!--&lt;!&ndash; <execution>&ndash;&gt;-->
<!--&lt;!&ndash; <phase>package</phase>&ndash;&gt;-->
<!--&lt;!&ndash; <goals>&ndash;&gt;-->
<!--&lt;!&ndash; <goal>proguard</goal>&ndash;&gt;-->
<!--&lt;!&ndash; </goals>&ndash;&gt;-->
<!--&lt;!&ndash; </execution>&ndash;&gt;-->
<!--&lt;!&ndash; </executions>&ndash;&gt;-->
<!--<configuration>-->
<!--<proguardVersion>6.2.2</proguardVersion>-->
<!--<injar>${project.build.finalName}-release.jar</injar>-->
<!--<outjar>${project.build.finalName}-pg.jar</outjar>-->
<!--<obfuscate>true</obfuscate>-->
<!--<proguardInclude>${project.basedir}/proguard.cfg</proguardInclude>-->
<!--<libs>-->
<!--&lt;!&ndash; Include main JAVA library required.&ndash;&gt;-->
<!--<lib>${java.home}/lib/rt.jar</lib>-->
<!--&lt;!&ndash; Include crypto JAVA library if necessary.&ndash;&gt;-->
<!--<lib>${java.home}/lib/jce.jar</lib>-->
<!--<lib>${java.home}/lib/ext/jfxrt.jar</lib>-->
<!--<lib>${java.home}/lib/jfxswt.jar</lib>-->
<!--&lt;!&ndash; <lib>${java.home}/lib/</lib>&ndash;&gt;-->
<!--</libs>-->
<!--&lt;!&ndash; <options>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-target 1.8</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-dontoptimize</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-dontshrink</option>&ndash;&gt;-->
<!--&lt;!&ndash;&lt;!&ndash; <option>-keepattributes *Annotation*</option>&ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash;&lt;!&ndash; <option>-keepattributes Signature</option>&ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash;&lt;!&ndash; <option>-keepattributes InnerClasses</option>&ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash;&lt;!&ndash; <option>-keepattributes InnerClasses,EnclosingMethod</option>&ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keepattributes javafx.fxml.FXML,Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod,LocalVariable*Table,Synthetic</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-useuniqueclassmembernames</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keepclassmembers enum * { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-dontwarn</option>&ndash;&gt;-->
<!--&lt;!&ndash;&lt;!&ndash; <option>-keep class min.Main*{*;}</option>&ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keepclasseswithmembers public class * { public static void main(java.lang.String[]);}</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-dontusemixedcaseclassnames</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class **.log4j.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class **.slf4j.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class **.logback.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class **.netty.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class sun.misc.Unsafe { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class org.bouncycastle.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class org.sqlite.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class **.netty.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class org.apache.commons.codec.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keepnames class com.fasterxml.jackson.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class org.codehaus.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keepclassmembers public final enum org.codehaus.jackson.annotate.JsonAutoDetect$Visibility {&ndash;&gt;-->
<!--&lt;!&ndash; public static final org.codehaus.jackson.annotate.JsonAutoDetect$Visibility *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class min.config.** { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keep class min.util.DataFormat.$* { *; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keepnames class min.util.DataFormat, min.util.DataFormat.$* {&ndash;&gt;-->
<!--&lt;!&ndash; public <fields>;&ndash;&gt;-->
<!--&lt;!&ndash; public <methods>;&ndash;&gt;-->
<!--&lt;!&ndash; }&ndash;&gt;-->
<!--&lt;!&ndash; </option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keepclassmembers class * {&ndash;&gt;-->
<!--&lt;!&ndash; @javafx.fxml.FXML *;&ndash;&gt;-->
<!--&lt;!&ndash; @javax.inject.Inject *;&ndash;&gt;-->
<!--&lt;!&ndash; @javax.annotation.PostConstruct *;&ndash;&gt;-->
<!--&lt;!&ndash; }</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-adaptresourcefilenames **.fxml,**.png,**.css,**.properties</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-adaptresourcefilecontents **.fxml</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-adaptclassstrings</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-printmapping out.map</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-renamesourcefileattribute SourceFile</option>&ndash;&gt;-->
<!--&lt;!&ndash; <option>-keepattributes SourceFile,LineNumberTable</option>&ndash;&gt;-->
<!--&lt;!&ndash; </options>&ndash;&gt;-->
<!--</configuration>-->
<!--<dependencies>-->
<!--&lt;!&ndash; https://mvnrepository.com/artifact/net.sf.proguard/proguard-base &ndash;&gt;-->
<!--<dependency>-->
<!--<groupId>net.sf.proguard</groupId>-->
<!--<artifactId>proguard-base</artifactId>-->
<!--<version>6.2.2</version>-->
<!--</dependency>-->
<!--</dependencies>-->
<!--</plugin>-->
<plugin>
<!-- https://gitee.com/roseboy/classfinal -->
<groupId>net.roseboy</groupId>
<artifactId>classfinal-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<password>#</password><!--加密打包之后pom.xml会被删除,不用担心在jar包里找到此密码-->
<packages>min</packages>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>classFinal</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>libs</directory>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20080701</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<!-- <scope>test</scope>-->
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.42.Final</version>
</dependency>
<dependency>
<groupId>MINSocket</groupId>
<artifactId>MINSocket</artifactId>
<version>1.1.2</version>
<scope>system</scope>
<systemPath>${basedir}/libs/MINSocket-1.1.2-all.jar</systemPath>
</dependency>
<dependency>
<groupId>gmhelper</groupId>
<artifactId>gmhelper</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/libs/gmhelper.jar</systemPath>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<!-- &lt;!&ndash; https://mvnrepository.com/artifact/io.netty/netty-all &ndash;&gt;-->
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.5.0</version>
</dependency>
<!-- &lt;!&ndash; https://mvnrepository.com/artifact/com.nativelibs4java/jnaerator &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.nativelibs4java</groupId>-->
<!-- <artifactId>jnaerator</artifactId>-->
<!-- <version>0.12</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.65</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.65</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>net.sf.proguard</groupId>-->
<!-- <artifactId>proguard-base</artifactId>-->
<!-- <version>6.0.3</version>-->
<!-- </dependency>-->
</dependencies>
</project>
+9
View File
@@ -0,0 +1,9 @@
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 16:57 2021/6/22
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class BC_API {
}
+156
View File
@@ -0,0 +1,156 @@
package Security;
import Utils.Base64Helper;
import minsecurity.crypto.SM4;
import minsecurity.crypto.sm2.SM2KeyPair;
import minsecurity.crypto.sm2.SM2PrivateKey;
import minsecurity.crypto.sm2.SM2PublicKey;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Base64;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 21:04 2021/6/22
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class KeyManager {
private SM2PrivateKey prk;
private SM2PublicKey pub;
public KeyManager(){
prk=new SM2PrivateKey();
pub=new SM2PublicKey();
}
public void GenKeyPair(){
SM2KeyPair sm2KeyPair=SM2KeyPair.generateKeyPair();
prk=sm2KeyPair.getSm2PrivateKey();
pub= sm2KeyPair.getSm2PublicKey();
}
/**
* 获取公钥
* @return
*/
public String GetPubkey(){
return new String(this.pub.getBytes(), StandardCharsets.UTF_8);
}
/**
* 获取私钥
* @return
*/
public String GetPriKey(){
return new String(this.prk.getBytes(),StandardCharsets.UTF_8);
}
public boolean SetPubkey(String data){
return this.pub.setBytes(data.getBytes(StandardCharsets.UTF_8));
}
public boolean SetPrikey(String data){
return this.prk.setBytes(data.getBytes(StandardCharsets.UTF_8));
}
/**
* 公钥加密
* @param data
* @return
* @throws InvalidCipherTextException
*/
public byte[] Encrypt(String data) throws InvalidCipherTextException {
return this.pub.encrypt(data.getBytes(StandardCharsets.UTF_8));
}
/**
* 私钥解密
* @param data
* @return
* @throws InvalidCipherTextException
*/
public byte[] Decrypt(String data) throws InvalidCipherTextException {
return this.prk.decrypt(data.getBytes(StandardCharsets.UTF_8));
}
/**
* 判断公私钥是否匹配
* @return
*/
// public boolean IsOnCurve(){
//
// }
/**
* 私钥签名
* @param text
* @return
* @throws CryptoException
*/
public String Sign(byte[] text) throws CryptoException {
byte[] sig=this.prk.sign(text);
return Base64Helper.BlockChain_Base64Encode(sig);
}
/**
* 公钥验签
* @param text
* @param signature
* @param pubkey
* @return
*/
public boolean Verify(byte[] text,String signature,String pubkey){
byte[] sig=Base64Helper.BlockChain_Base64Decode(signature);
SM2PublicKey publicKey=new SM2PublicKey();
boolean flag=publicKey.setBytes(pubkey.getBytes(StandardCharsets.UTF_8));
if(!flag){
return false;
}
return publicKey.verify(text,sig);
}
/**
* SM4对称加密
* @param secretkey
* @param origData
* @return
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws InvalidKeyException
* @throws NoSuchProviderException
*/
public byte[] SM4Encrypt(String secretkey,byte[] origData) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, NoSuchProviderException {
return SM4.encrypt_ECB_Padding(secretkey.getBytes(StandardCharsets.UTF_8),origData);
}
/**
* SM4对称解密
* @param secretkey
* @param crypted
* @return
* @throws IllegalBlockSizeException
* @throws NoSuchPaddingException
* @throws BadPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws NoSuchProviderException
*/
public byte[] SM4Decrypt(String secretkey,byte[] crypted) throws IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException {
return SM4.decrypt_ECB_Padding(secretkey.getBytes(StandardCharsets.UTF_8),crypted);
}
}
+116
View File
@@ -0,0 +1,116 @@
package TCPNet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 17:05 2021/6/22
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public abstract class TCPClient {
private String ip;
private int port;
private Socket mSocket;
private SocketAddress mSocketAddress;
private OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;
private boolean _isConnected = false;
public TCPClient(String ip, int port) {
this.ip = ip;
this.port = port;
}
public void connect() {
try {
this.mSocket = new Socket();
this.mSocket.setKeepAlive(true);
this.mSocketAddress = new InetSocketAddress(ip, port);
this.mSocket.connect(mSocketAddress, 3000);// 设置连接超时时间为3秒
this.mOutputStream = mSocket.getOutputStream();
this.mInputStream = mSocket.getInputStream();
this.mReadThread = new TCPClient.ReadThread();
this.mReadThread.start();
this._isConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
if (this.mReadThread != null) {
this.mReadThread.interrupt();
}
if (this.mSocket != null) {
try {
this.mSocket.close();
this.mSocket = null;
} catch (IOException e) {
e.printStackTrace();
}
}
this._isConnected = false;
}
public boolean isConnected() {
return this._isConnected;
}
public void send(byte[] bOutArray) {
try {
this.mOutputStream.write(bOutArray);
} catch (IOException e) {
e.printStackTrace();
}
}
protected abstract void onDataReceive(byte[] bytes, int size);
private class ReadThread extends Thread {
@Override
public void run() {
super.run();
while (!isInterrupted()) {
try {
if (TCPClient.this.mInputStream == null) {
return;
}
int available = TCPClient.this.mInputStream.available();
if (available > 0) {
byte[] buffer = new byte[available];
int size = TCPClient.this.mInputStream.read(buffer);
if (size > 0) {
onDataReceive(buffer, size);
}
} else {
Thread.sleep(50);
}
} catch (Throwable e) {
System.out.println(e.getMessage());
return;
}
}
}
}
}
+38
View File
@@ -0,0 +1,38 @@
package Utils;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 22:20 2021/6/22
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class Base64Helper {
/**
* 与vpn-management适配的base64编码
* @param src
* @return
*/
public static String BlockChain_Base64Encode(byte[] src){
Base64.Encoder encoder = Base64.getUrlEncoder();
encoder = encoder.withoutPadding();
return encoder.encodeToString(src);
}
public static String BlockChain_Base64Encode(String src){
return BlockChain_Base64Encode(src.getBytes(StandardCharsets.UTF_8));
}
/**
* 与vpn-management适配的base64解码
* @param src
* @return
*/
public static byte[] BlockChain_Base64Decode(String src) {
Base64.Decoder decoder = Base64.getUrlDecoder();
return decoder.decode(src.getBytes(StandardCharsets.UTF_8));
}
}
+32
View File
@@ -0,0 +1,32 @@
import TCPNet.TCPClient;
import org.junit.Test;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 16:58 2021/6/22
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class TestMain {
@Test
public void testConnectionServer(){
TCPClient tcpClient = new TCPClient("121.15.171.82", 12345) {
@Override
protected void onDataReceive(byte[] bytes, int size) {
String content = "TCPServer say :" + new String(bytes, 0, size);
System.out.println(content);
}
};
tcpClient.connect(); // 连接TCPServer
if (tcpClient.isConnected()) {
//发送数据
String outContent = "TCPClient say hello";
tcpClient.send(outContent.getBytes());
}
//关闭连接
tcpClient.close();
}
}
+21
View File
@@ -0,0 +1,21 @@
package Utils;
import org.junit.Test;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 22:22 2021/6/22
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class TestBase64Helper {
@Test
public void testBase64(){
String test = "wefree";
String encodeText = Base64Helper.BlockChain_Base64Encode(test);
String test2 = new String(Base64Helper.BlockChain_Base64Decode(encodeText));
System.out.println(encodeText);
System.out.println(test2);
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
+5
View File
@@ -0,0 +1,5 @@
#Generated by Maven
#Tue Jun 22 21:24:50 CST 2021
version=1.0-SNAPSHOT
groupId=org.example
artifactId=MIN-VPN-Non-Core
@@ -0,0 +1,5 @@
TCPNet\TCPClient$ReadThread.class
TCPNet\TCPClient.class
BC_API.class
Security\KeyManager.class
TCPNet\TCPClient$1.class
@@ -0,0 +1,3 @@
D:\MyCode\MIN\MIN-VPN-Non-Core\src\main\java\Security\KeyManager.java
D:\MyCode\MIN\MIN-VPN-Non-Core\src\main\java\TCPNet\TCPClient.java
D:\MyCode\MIN\MIN-VPN-Non-Core\src\main\java\BC_API.java
@@ -0,0 +1,2 @@
TestMain.class
TestMain$1.class
@@ -0,0 +1 @@
D:\MyCode\MIN\MIN-VPN-Non-Core\src\test\java\TestMain.java