此应用程序的主要目标是根据请求生成AES密钥服务器端,并使用SSL将其传递给客户端。我目前遇到两个错误。服务器错误:
javax.net.ssl.SSLHandshakeException:没有共同的密码套件
客户方:
javax.net.ssl.SSLHandshakeException:收到致命警报: handshake_failure
我对SSL没有太多经验,所以我不确定是什么导致了这些错误。
public class TCPServer extends Thread {
public static void main(String[] args) {
// ransome
int port = 49002;
String password = "password12345678";
// Build key
byte[] key = Arrays.copyOf(password.getBytes(), 16);
System.out.println("SERVER KEY");
String keyString = key.toString();
Socket sslsocket = null;
try {
String clientRequest;
SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
ServerSocket sslserversocket = sslserversocketfactory.createServerSocket(port);
while (true) {
System.out.println("SERVER accepting connections");
sslsocket = sslserversocket.accept();
/// Socket clientConnectionSocket = serverSocket.accept();
System.out.println("SERVER accepted connection (single threaded so others wait)");
// This is regarding the server state of the connection
while (sslsocket.isConnected() && !sslsocket.isClosed()) {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(sslsocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(sslsocket.getOutputStream());
clientRequest = inFromClient.readLine();
if (clientRequest != null)
outToClient.write(key);
else {
sslsocket.close();
System.out.println("SERVER client connection closed");
}
// System.out.println("SERVER client connection closed");
}
}
} catch (Exception e) {
e.printStackTrace();
if (sslsocket != null) {
try {
sslsocket.close();
} catch (IOException ioe) {
// ignore
}
}
}
}
}
public class TCPClient extends Thread {
public static void main() {
String host = "localhost";
int serverPort = 49002;
Socket sslsocket = null;
// ransome
byte[] key = null;
String keyString;
try {
InetAddress address = InetAddress.getByName(host);
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
sslsocket = sslsocketfactory.createSocket(address, serverPort);
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
System.out.println("CLIENT opening socket");
System.out.println("CLIENT connected to server");
DataOutputStream outToServer = new DataOutputStream(sslsocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(sslsocket.getInputStream()));
// outToServer.writeBytes(request + '\n');
keyString = inFromServer.readLine();
key = keyString.getBytes();
sslsocket.close();
System.out.println("Client KEY" + key);
} catch (Exception e) {
e.printStackTrace();
try {
if (sslsocket != null) {
sslsocket.close();
}
} catch (Exception cse) {
// ignore exception here
}
}
}
}