how to do ssl socket programming

前端 未结 3 1324
情话喂你
情话喂你 2020-12-06 02:55

I am doing socket communication through follwing IP address it working but no i want to do communication in ssl mode but how can I change InetAddress serverAddr = Ine

相关标签:
3条回答
  • 2020-12-06 02:58

    Create SSLSocket instead of Socket. Rest is the same.

    SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("192.168.1.2", 12345);
    

    You may want to add aditional SSL properties. You have to do it ealier:

    To authenticate the server, the client's trust store must contain the server's certificate. Client SSL with server authentication is enabled by the URL attribute ssl or the property ssl set to peerAuthentication. In addition, the system properties javax.net.ssl.trustStore and javax.net.ssl.trustStorePassword need to be set.:

    System.setProperty("javax.net.ssl.trustStore","clientTrustStore.key");
    System.setProperty("javax.net.ssl.trustStorePassword","qwerty");
    

    If the server does client authentication, the client will need a key pair and a client certificate:

    System.setProperty("javax.net.ssl.keyStore","clientKeyStore.key");
    System.setProperty("javax.net.ssl.keyStorePassword","qwerty");
    
    0 讨论(0)
  • 2020-12-06 03:09

    Basically you need to use SSLSocket which is for SSL communication in Java.

    When creating the SSLSocket, you first need to configure the trust store which is to verify the server certificate.

    Then you need to get the SSLSocket and connect to the server and then start to do handshake with the server.

    Once the handshake complete successfully, you can start to exchange application data with server normally like other plain socket connection.

    A HTTPS client and HTTPS server demo in Java provides a demo on how to create SSL server and SSL client in Java. It's quite useful.

    0 讨论(0)
  • 2020-12-06 03:12

    Java has the SSLSocket class.

    http://download.oracle.com/javase/1.4.2/docs/api/javax/net/ssl/SSLSocket.html

    Hope this helps, haven't used it myself (yet).

    0 讨论(0)
提交回复
热议问题