javasslserversslexception

Server chat java - SSLException


I am stuck in a serious place..., I am trying to create a secure server. So I had to modify my code to be able to send in a secure way, the problem is the following.

It displays me the error: javax.net.ssl.SSLException: Connection has closed: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message

I already made a certificate via keytool but I don't have the impression that it works

I have looked everywhere but I can't find a solution to my problem...

Here are my codes:

Server :

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();
        }

Client

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());
        }

I don't thing it is important to give you the serverThread cause it is not usefull.

Thank and have nice day :)


Solution

  • You server is not listening in SSL as you are calling the wrong factory. You would need SSLServerSocketFactory instead. Try

    ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();