0

我被困在一个严重的地方......,我正在尝试创建一个安全的服务器。所以我必须修改我的代码才能以安全的方式发送,问题如下。

它向我显示错误:javax.net.ssl.SSLException: Connection has closed: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message

我已经通过 keytool 制作了证书,但我不认为它有效

我到处找,但我找不到解决我的问题的方法......

这是我的代码:

服务器 :

System.setProperty("javax.net.ssl.keyStore", "test.store");
        System.setProperty("javax.net.ssl.keyStorePassword", "password");
        ServerSocketFactory serverSocketFactory =
      ServerSocketFactory.getDefault();
    ServerSocket    serverSocket =
      serverSocketFactory.createServerSocket(4444);
        System.out.println("Started...");
        while(true){
            new ServerThread(serverSocket.accept()).start();
        }

客户

System.setProperty("javax.net.ssl.trustStore", "test.store");
        SocketFactory socketFactory = SSLSocketFactory.getDefault();
        Socket socket  = socketFactory.createSocket("localhost", 4444);
        BufferedReader bf1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter username");
        writer.println(bf.readLine());
        String message = null;
        while(true){
            System.out.println("enter message send to server");
            message = bf.readLine();
            if(message.equals("quit")){
                socket.close();
                break;
            }
            writer.println(message);
            System.out.println("message from server : " + bf1.readLine());
        }

我认为给你 serverThread 并不重要,因为它没有用。

谢谢,祝你有美好的一天:)

4

1 回答 1

0

您的服务器没有在SSL中侦听,因为您调用了错误的工厂。您将需要SSLServerSocketFactory代替。尝试

ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();
于 2021-05-01T10:00:45.557 回答