我最近一直在为消息系统使用 SSL 套接字。
我终于开始使用 CA 颁发的证书了。我将它导入我的 keystore ,设置 keystore 属性,并启动我的客户端,但是当我尝试发送数据时,我总是收到握手异常。
我启用了 Debug模式,我发现这是由于不支持密码套件
。对此有什么想法吗?
如果有帮助,它还说它忽略了来自 TLSV1 和 TLSV1.1 的大约 10 个密码套件。
我还安装了 Unlimited Strength Cryptographic Policy。
客户端代码
public static void Message(String args){
System.setProperty("https.protocols", "TLSv1");
System.setProperty("javax.net.debug", "all");
try
{
String host = "localhost";
int port = 3498;
InetAddress address = InetAddress.getByName(host);
socket = (SSLSocket)SSLSocketFactory.getDefault().createSocket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String number = "Hello from the other side!";
String sendMessage = number + "\n";
bw.write(args);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
//Get the return message from the server
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
服务器代码
private void initListen() {
System.setProperty("javax.net.ssl.keyStore", "/Users/181095/server.jks");
try {
SSLServerSocketFactory sf = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket serverSocket = (SSLServerSocket)sf.createServerSocket(Ting.port);
System.out.println("Server Started and listening to the port "
+ Ting.port);
// Server is running always. This is done using this while(true)
// loop
while (true) {
// Reading the message from the client
socket = (SSLSocket)serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String input = br.readLine();
System.out.println("Message received from client is " + input);
/*if(PacketReader.isPacket(input)){
if(PacketReader.shouldSendDataBack(input)){
}
}*/
/*
*
* if client has data waiting or client has new data
* request new data.
* else, wait 5 seconds and repeat
*
*
*/
// Sending the response back to the client.
/*OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("");*/
//TODO Implement method to send this data to client storage data.
//bw.flush();
}
//Check for exceptions and try to close the socket.
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (Exception e) {
}
}
}
编辑: 我刚刚添加了密码,但我的条目/证书没有 key ,这是否会导致错误?我该如何解决?
最佳答案
令人惊讶的是,这可能意味着服务器无法找到私钥/证书对。在这种情况下,这是因为您没有通过 javax.net.ssl.keyStorePassword
指定 keystore 密码。
关于java - 普通 Java 套接字中没有密码套件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37905836/