How to secure a client/ server socket communication

client-server, java, security, sockets

Solution

The JDK provides an SSL socket implementation that seems an appropriate place to start. It's straightforward to use.

Originally, you probably have code like this:

final SocketFactory factory = SocketFactory.getDefault();
final Socket socket = factory.createSocket(host, port);

You simply change the factory:

final SocketFactory factory = SSLSocketFactory.getDefault();
final Socket socket = factory.createSocket(host, port);

The same is true for `ServerSocketFactory`.

Problem

Assume that client and server apps are running on different machine Today: ``` // Server will receive the request and cast it as needed ProxyResponse message = (ProxyResponse) objStream.readObject(); // Update message with some content ... // Sent it back to client ObjectOutputStream oos = new ObjectOutputStream(toClient); oos.writeObject(message); ``` I'd like to further enhance this to make sure data sent out is somehow protected. How would you recommend i approach this? I am looking for an example of how data passed between client an server be encrypted and decrypted on both ends and sent over an SSL.

Original source