Create app with SSLSocket Java

北战南征 提交于 2019-12-17 19:13:30

问题


I want to create an app use SSLSocket: client send a String to server and server will uppercase that String and send back to client for display.

SSLServer

public class SSLServer {
    public static void main(String args[]) throws Exception
    {
        try{
        //Creaet a SSLServersocket
        SSLServerSocketFactory factory=(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
        SSLServerSocket sslserversocket=(SSLServerSocket) factory.createServerSocket(1234);
        //Tạo 1 đối tượng Socket từ serversocket để lắng nghe và chấp nhận kết nối từ client
        SSLSocket sslsocket=(SSLSocket) sslserversocket.accept();
        //Tao cac luong de nhan va gui du lieu cua client
        DataInputStream is=new DataInputStream(sslsocket.getInputStream());
        PrintStream os=new PrintStream(sslsocket.getOutputStream());
        while(true)  //khi dang ket noi voi client
        {
            //Doc du lieu den
            String input=is.readUTF();
            String ketqua=input.toUpperCase();
            //Du lieu tra ve
            os.println(ketqua);
        }
        }
        catch(IOException e)
        {
           System.out.print(e);
        }
    }
}

SSLClient

public class SSLClient {
    public static void main(String args[])
    {
        try
        {
        //Mo 1 client socket den server voi so cong va dia chi xac dinh
        SSLSocketFactory factory=(SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket sslsocket=(SSLSocket) factory.createSocket("127.0.0.1",1234);

        //Tao luong nhan va gui du lieu len server
        DataOutputStream os=new DataOutputStream(sslsocket.getOutputStream());
        DataInputStream is=new DataInputStream(sslsocket.getInputStream());

        //Gui du lieu len server
        String str="helloworld";
        os.writeBytes(str);

        //Nhan du lieu da qua xu li tu server ve
        String responseStr;
        if((responseStr=is.readUTF())!=null)
        {
            System.out.println(responseStr);
        }

        os.close();
        is.close();
        sslsocket.close();
        }
        catch(UnknownHostException e)
        {
             System.out.println(e.getMessage());
        }
        catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
}

When run SSLServer. It displays this error:

javax.net.ssl.SSLException: No available certificate or key corresponds 
    to the SSL cipher suites which are enabled

I have search and do some ways but.. Can you help me.


回答1:


This will generate certificate:

keytool -genkey -keystore yourKEYSTORE -keyalg RSA

Enter yourPASSWORD and than start your server with ssl debug information(put yourKEYSTORE into directory with SSLServer.class):

java -Djavax.net.ssl.keyStore=yourKEYSTORE -Djavax.net.ssl.keyStorePassword=yourPASSWORD -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol -Djavax.net.debug=ssl SSLServer

Than start your client(put yourKEYSTORE into directory with SSLClient.class):

java -Djavax.net.ssl.trustStore=yourKEYSTORE -Djavax.net.ssl.trustStorePassword=yourPASSWORD SSLClient



回答2:


Check the certificates that you have installed. Make sure they are supporting the cipher suites that you are negotiating.




回答3:


@corVaroxid's answer is correct. But if you want to set configurations programmatically to avoid global settings (like me), you can go like below (Kotlin):

val password = "yourPassword".toCharArray()

val keyStore = KeyStore.getInstance(File("yourKeystorePath.jks"), password)

val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
trustManagerFactory.init(keyStore)

val keyManagerFactory = KeyManagerFactory.getInstance("NewSunX509")
keyManagerFactory.init(keyStore, password)

val context = SSLContext.getInstance("TLS") //"SSL" "TLS"
context.init(keyManagerFactory.keyManagers, trustManagerFactory.trustManagers, null)

val factory = context.serverSocketFactory

(factory.createServerSocket(LISTENING_PORT) as SSLServerSocket).use { serverSocket ->
    logger.trace("Listening on port: $LISTENING_PORT")

    // ...
}

Or in Java:

final char[] password = "yourPassword".toCharArray();

final KeyStore keyStore = KeyStore.getInstance(new File("yourKeystorePath.jks"), password);

final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);

final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("NewSunX509");
keyManagerFactory.init(keyStore, password);

final SSLContext context = SSLContext.getInstance("TLS");//"SSL" "TLS"
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

final SSLServerSocketFactory factory = context.getServerSocketFactory();

try (SSLServerSocket serverSocket = ((SSLServerSocket) factory.createServerSocket(LISTENING_PORT))) { 
    logger.trace("Listening on port: " + LISTENING_PORT);

    // ...
}


来源:https://stackoverflow.com/questions/13874387/create-app-with-sslsocket-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!