views:

35

answers:

1

I'm working on a server application in Java. I've successfully got past the handshake portion of the communication process, but how do I go about decrypting my input stream? Here is how I set up my server:

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;

import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;

import org.json.me.JSONException;

import dictionary.Dictionary;


public class Server {

    private static int port = 1234;

    public static void main(String[] args) throws JSONException {

        System.setProperty("javax.net.ssl.keyStore", "src/my.keystore");
        System.setProperty("javax.net.ssl.keyStorePassword", "test123");

        System.out.println("Starting server on port: " + port);
        HashMap<String, Game> games = new HashMap<String, Game>();
        final String[] enabledCipherSuites = { "SSL_RSA_WITH_RC4_128_SHA" };

        try{
            SSLServerSocketFactory socketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
            SSLServerSocket listener = (SSLServerSocket) socketFactory.createServerSocket(port);
            listener.setEnabledCipherSuites(enabledCipherSuites);
            Socket server; 

            Dictionary dict = new Dictionary();
            Game game = new Game(dict); //for testing, creates 1 global game.
            while(true){
                server = listener.accept(); 
                ClientConnection conn = new ClientConnection(server, game, "User");
                Thread t = new Thread(conn);
                t.start();  
            }
        }
        catch(IOException e){
            System.out.println("Failed setting up on port: " + port);
            e.printStackTrace();
        }
    }
}

I used a BufferedReader to get the data send from the client:

BufferedReader d = new BufferedReader(new InputStreamReader(socket.getInputStream()));

After the handshake is complete it appears like I'm getting encrypted data. I did some research online and it seems like I might need to use a Cipher, but I'm not sure. Any ideas?

A: 

available() always returns 0 and ready() always returns false when the underlying source is an SSLSocket.

There are few if any valid reasons to use either of these methods. Just read. It doesn't do you any good to know that there is or isn't any data available. You still have to read, so read, and you get the blocking for free, instead of having to code it, and instead of wasting time because you slept for 1000ms and the data arrived after 1ms, or wasting cycles because you did the reverse. And there's no benefit in custom-sizing a buffer to what available() returns: just use a fixed-size buffer and allocate it outside your read loop, thus saving GC cycles too.

EJP