Rui
Rui

Reputation: 3

How to send a DSA PublicKey via socket?

I'm doing a uni project where the client has to send his Public Key to the Server, so I'm trying to send a DSA PublicKey via socket from the client to the server but I'm struggling, and all the solutions I found only apply to RSA Public Keys.

I'm using this to send information to the server:

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

And i'm looking for a way to "transform" the PublicKey's values in a String and then the server converts that String into a PublicKey.

Part of the client code:

public Client(String nickname, int port, String ip, Socket socket){
        try {
            this.nickname=nickname;
            this.port= port;
            this.socket=socket;
            this.ip=ip;
            this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            this.out = new PrintWriter(socket.getOutputStream(), true);

            generateKeys(this);
            //sends client info to server
            out.println(nickname);
            out.println(port);
            out.println(ip);

            String c = this.getChave_pub().getEncoded().toString();
            out.println(c);
        }catch (IOException e){
            System.out.println("#### Error connecting! "+e+" ####");
        }
    }

public void generateKeys(Client client){
        KeyPairGenerator keyPairGen = null;
        try {
            keyPairGen = KeyPairGenerator.getInstance("DSA");
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Error "+e);
        }
        keyPairGen.initialize(2048);

        KeyPair par = keyPairGen.generateKeyPair();

        //separate the pair of keys and put the values in the variables of the clients
        client.setKey_priv(par.getPrivate());
        client.setKey_pub(par.getPublic());
    }

Part of the server code:

public ClientHandler(Socket socket){
        try{
            this.socket=socket;
            this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            this.out = new PrintWriter(socket.getOutputStream(), true);
            this.nickname= in.readLine();
            this.port = in.readLine();
            this.ip = in.readLine();
            //i used to have stuff here but it doesn't work
            System.out.println("A new user has entered: "+ nickname + "\n");
        } catch (IOException e){
            System.out.println("#### Error "+e+" ####");
    }
}

I've tried also using ObjectOutputStream and ObjectInputStream to send it as an object, but i kept getting errors, being one of them:

java.io.StreamCorruptedException: invalid stream header: 73720014 

Also, don't come too hard on me, I'm yet learning now all about Private and Public Keys, I understand why the code doesn't work but i can't find other ways to do it.

UPDATE: I have now changed the code to send Object, and it's working but only if i create the obj_o/obj_in and the line after I write/read the object, if I separate them I get this error:

java.io.StreamCorruptedException: invalid stream header: 73720014 

Can somebody explain why?

Part of the client code:

public Client(String nickname, int port, String ip, Socket socket){
        try {
            this.nickname=nickname;
            this.port= port;
            this.socket=socket;
            this.ip=ip;
            this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            this.out = new PrintWriter(socket.getOutputStream(), true);

            generateKeys(this);
            //sends client info to server
            out.println(nickname);
            out.println(port);
            out.println(ip);

            ObjectOutputStream obj_o = new ObjectOutputStream(socket.getOutputStream());
            obj_o.writeObject(this.getChave_pub());
        }catch (IOException e){
            System.out.println("#### Error connecting! "+e+" ####");
        }
    }

public void generateKeys(Client client){
        KeyPairGenerator keyPairGen = null;
        try {
            keyPairGen = KeyPairGenerator.getInstance("DSA");
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Error "+e);
        }
        keyPairGen.initialize(2048);

        KeyPair par = keyPairGen.generateKeyPair();

        //separate the pair of keys and put the values in the variables of the clients
        client.setKey_priv(par.getPrivate());
        client.setKey_pub(par.getPublic());
    }

Part of the server code:

public ClientHandler(Socket socket){
        try{
            this.socket=socket;
            this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            this.out = new PrintWriter(socket.getOutputStream(), true);
            this.nickname= in.readLine();
            this.port = in.readLine();
            this.ip = in.readLine();
            //only used to receive the object that represents the key
            ObjectInputStream obj_in = new ObjectInputStream(socket.getInputStream());      
            this.chave_pub=(PublicKey)obj_in.readObject();
            System.out.println("A new user has entered: "+ nickname + "\n");
        } catch (IOException e){
            System.out.println("#### Error "+e+" ####");
    }
}

Upvotes: 0

Views: 73

Answers (0)

Related Questions