writing net...

This commit is contained in:
free will committed 2021-06-25 11:27:03 +08:00
1 parent 3418f608b9
commit 9335c1e229
10 files changed
+219 -12

No files matched your search

+74 -2
View File
@@ -1,5 +1,12 @@
package TCPNet;
import Security.BC_KeyManager;
import TCPNet.request.BaseRequest;
import org.bouncycastle.crypto.InvalidCipherTextException;
import java.nio.charset.StandardCharsets;
import java.util.Random;
/*
* @Author: Wang Feng
* @Description:
@@ -7,8 +14,73 @@ package TCPNet;
* @Date: 20:50 2021/6/24
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
class SSL_SetupMessage extends BaseRequest {
String Pubkey;
String Secretkey;
}
class SSL_Message extends BaseRequest{
int IsEncrypted;
int MType;
byte[] data;
String Pubkey="";
int Code=0;
}
public class SSL {
public static String sslSetup(){
return "";
// 是否加密
public static final int encrypted=0;
public static final int not_encrypted=1;
// SSL_Message类型
public static final int normal=0;
public static final int setup=1;
public static final int resp=2;
public static String sslSetup(Conn conn, String pubKey,
BC_KeyManager key,BC_KeyManager serverKey){
// 取一个十六位的密钥
String secretKey=String.format("%016d",
Math.abs(new Random(System.currentTimeMillis()).nextLong()))
.substring(0,16);
// 构造SSL_SetupMessage(公钥&密钥)
SSL_SetupMessage sm=new SSL_SetupMessage();
sm.Pubkey=pubKey;
sm.Secretkey=secretKey;
String data;
try {
data=sm.toJson();
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
// 用公钥去加密它
byte[] encData;
try {
encData=serverKey.Encrypt(data);
} catch (InvalidCipherTextException e) {
e.printStackTrace();
return null;
}
// 构造SSL_Message
SSL_Message sslMessage=new SSL_Message();
sslMessage.IsEncrypted=encrypted;
sslMessage.MType=setup;
sslMessage.data=encData;
String ms;
try {
ms=sm.toJson();
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
// 将SSL_Message写给服务器
if(!conn.writeV2(ms.getBytes(StandardCharsets.UTF_8))){
System.out.println("SSL write error");
return null;
}
//
return ;
}
}