调通SSL过程(解决了若干BUG:发送时的字节序问题,接收时的json转带有byte[]属性的java对象问题);尚存问题:sm4不能用

This commit is contained in:
free will committed 2021-06-30 23:53:01 +08:00
1 parent db2b45395e
commit 3e96c8dc0a
12 files changed
+273 -75

No files matched your search

+61 -17
View File
@@ -3,11 +3,14 @@ package TCPNet;
import Security.BC_KeyManager;
import TCPNet.request.BaseRequest;
import TCPNet.response.BaseResponse;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.JSONToken;
import org.bouncycastle.crypto.InvalidCipherTextException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@@ -23,18 +26,51 @@ import java.util.Random;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
class SSL_SetupMessage extends BaseRequest {
String Pubkey;
String Secretkey;
public String Pubkey;
public String Secretkey;
}
class SSL_Message_Helper{
// 为了直接将SSL_Message的json格式解析为java对象,
// 而定义本类
public int IsEncrypted;
public int MType;
public String Data;
public String Pubkey = "";
public int Code = 0;
}
class SSL_Message extends BaseRequest{
int IsEncrypted;
int MType;
byte[] Data;
String Pubkey="";
int Code=0;
public int IsEncrypted;
public int MType;
public byte[] Data;
public String Pubkey="";
public int Code=0;
@Override
public void unJson(byte[] data,Object thisObj) {
SSL_Message_Helper newObj= JSONObject.parseObject(new String(data), SSL_Message_Helper.class);
try {
Class thiscls = SSL_Message.class;
Class cls = SSL_Message_Helper.class;
Field[] fields = cls.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
f.setAccessible(true);
// 将属性值的名字的属性值的值放入this中
Field thisf = thiscls.getDeclaredField(f.getName());
thisf.setAccessible(true);
if(thisf.getType()==(byte[].class)){
thisf.set(this, ((String)(f.get(newObj))).getBytes(StandardCharsets.UTF_8));
}else {
thisf.set(this, f.get(newObj));
}
}
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
}
}
class SSL_RespMessage extends BaseResponse {
int Code;
public int Code;
}
public class SSL {
// 是否加密
@@ -89,20 +125,24 @@ public class SSL {
sslMessage.IsEncrypted=encrypted;
sslMessage.MType=setup;
sslMessage.Data =encData;
String ms;
String ms=null;
try {
ms=sm.toJson();
ms=sslMessage.toJson();
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
System.out.println("test 000: mesdata: "+new String(ms.getBytes(StandardCharsets.UTF_8)));
// 将SSL_Message写给服务器(内含:用户公钥、用户对称密钥)
if(!conn.writeV2(ms.getBytes(StandardCharsets.UTF_8))){
System.out.println("SSL write error");
return null;
}
System.out.println("test 111");
// 读取服务器应答信息
byte[] resData=conn.readSSL();
if(resData==null){
@@ -110,15 +150,19 @@ public class SSL {
return null;
}
System.out.println("test 222: "+new String(resData));
System.out.println("secretkey: "+secretKey);
// 用对称密钥解密应答信息
byte[] decRes;
try {
decRes=BC_KeyManager.SM4Decrypt(secretKey,resData);
} catch (IllegalBlockSizeException | InvalidKeyException | NoSuchProviderException
| NoSuchAlgorithmException | BadPaddingException | NoSuchPaddingException e) {
e.printStackTrace();
return null;
}
// try {
// todo:这里sm4有问题
decRes="{\"Code\":200}".getBytes(StandardCharsets.UTF_8);
// decRes=BC_KeyManager.SM4Decrypt(secretKey,resData);
// } catch (IllegalBlockSizeException | InvalidKeyException | NoSuchProviderException
// | NoSuchAlgorithmException | BadPaddingException | NoSuchPaddingException e) {
// e.printStackTrace();
// return null;
// }
// 将解密信息转为Java对象
SSL_RespMessage resp=new SSL_RespMessage();