//----发送客户端公钥去服务器端、并获取服务器端公钥--start---------------------
SecureKey sk = new SecureKey();//生成客户端公/私 钥
ServiceProxy proxy = ServiceProxy.getProxy(serviceConfig.getServicename());
HandclaspProtocol handclaspProtocol = new HandclaspProtocol("1",sk.getStringPublicKey());
Protocol publicKeyProtocol = proxy.createProtocol(handclaspProtocol);
try {
scoket.registerRec(publicKeyProtocol.getSessionID());
scoket.send(publicKeyProtocol.toBytes());//过程2
logger.info("send client publicKey sucess!");
} finally {
//scoket.dispose();
}
handclaspProtocol = null;
/**
* 过程3,接收服务器端生成公钥
*/
byte[] receivePublicBuffer = scoket.receive(publicKeyProtocol.getSessionID(), server.getCurrUserCount());
if(null == receivePublicBuffer){
logger.warn("获取服务器公钥失败!");
return false;
}
Protocol serverPublicProtocol = publicKeyProtocol.fromBytes(receivePublicBuffer);
HandclaspProtocol _handclaspProtocol = null;
if (serverPublicProtocol.getSDPType() == SDPType.Handclasp) {
_handclaspProtocol = (HandclaspProtocol)serverPublicProtocol.getSdpEntity();
logger.debug("get server publicKey time:" + (System.currentTimeMillis() - startTime) + "ms");
} else if (serverPublicProtocol.getSDPType() == SDPType.Exception) {
ExceptionProtocol ep = (ExceptionProtocol)serverPublicProtocol.getSdpEntity();
throw ThrowErrorHelper.throwServiceError(ep.getErrorCode(), ep.getErrorMsg());
} else if(serverPublicProtocol.getSDPType() == SDPType.Reset){
throw new RebootException("this server is reboot!");
} else {
throw new Exception("userdatatype error!");
}
logger.info("receive server publicKey sucess!");
publicKeyProtocol = null;
String keyInfo = serviceConfig.getSecureKey().getInfo();//授权文件
if(null == keyInfo || "".equals(keyInfo)){
logger.warn("获取授权文件失败!");
return false;
}
String ciphertext = sk.encryptByPublicKeyString(keyInfo, _handclaspProtocol.getData());
_handclaspProtocol = null;
serverPublicProtocol = null;
//----发送客户端公钥去服务器端、并获取服务器端公钥--end---------------------
//---发送授权文件到服务器端认证--------------------start-------------------------------
HandclaspProtocol handclaspProtocol_ = new HandclaspProtocol("2",ciphertext);
Protocol protocol_mw = proxy.createProtocol(handclaspProtocol_);
try {
scoket.registerRec(protocol_mw.getSessionID());
scoket.send(protocol_mw.toBytes());//过程4
logger.info("send keyInfo sucess!");
} finally {
//scoket.dispose();
}
handclaspProtocol_ = null;
/**
* 过程5
* 获取由客户端公钥加密后的DES密钥
*/
byte [] receiveDESKey = scoket.receive(protocol_mw.getSessionID(), server.getCurrUserCount());
if(null == receiveDESKey){
logger.warn("获取DES密钥失败!");
return false;
}
logger.info("receive DESKey sucess!");
HandclaspProtocol handclaspProtocol_mw = null;
Protocol serverDesKeyProtocol = Protocol.fromBytes(receiveDESKey);
if (serverDesKeyProtocol.getSDPType() == SDPType.Handclasp) {
handclaspProtocol_mw = (HandclaspProtocol)serverDesKeyProtocol.getSdpEntity();
} else if (serverDesKeyProtocol.getSDPType() == SDPType.Exception) {
ExceptionProtocol ep = (ExceptionProtocol)serverDesKeyProtocol.getSdpEntity();
throw ThrowErrorHelper.throwServiceError(ep.getErrorCode(), ep.getErrorMsg());
} else if(serverDesKeyProtocol.getSDPType() == SDPType.Reset){
throw new RebootException("this server is reboot!");
} else {
throw new Exception("userdatatype error!");
}