Having issues between my server and client communicating with eachother in java

69 views Asked by At

I know I am doing something obviously wrong but I cannot figure it out. Basically, I am creating a client that sends its name and nonce to the server. The server should read this, then send back to the client its own nonce, plus an encryption of its own name using. Once the client receives that, it will send to the server its own encryption of its name to the server to decrypt. The problem I am having is that the server is not receiving the first input form the client, before any encryption happens. Below I have included the code for the sever, (named Bob) and the client, (named Alice). I know that the client connects to the server properly, but after that it cannot read the input.

import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.Arrays;

public class Bob {
    public static void main(String[] args) throws Exception {

        String Bobidentity = "Bob";
        
        // Start listening for incoming connections
        int portNumber = 1500;

        ServerSocket serverSocket = new ServerSocket(portNumber);
        System.out.println("Bob waiting for Alice...");

        // Accept Alice's connection
        Socket socket = serverSocket.accept();
        System.out.println("Alice connected.");

        // Get input/output streams
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());

        // Receive identity and nonce from Alice
        String aliceIdentity = (String) in.readObject();
        int aliceNonce = in.readInt();

        System.out.println("Received Alice Identity (1) " + aliceIdentity);
        System.out.println("Received Alice Nonce (1) " + aliceNonce);


        int bobNonce = 3;

        // Send nonce to Alice
        out.writeInt(bobNonce);

        // Generate shared secret key using both nonces
        byte[] sharedSecretBytes = (Integer.toString(bobNonce) + Integer.toString(aliceNonce)).getBytes();
        SecretKey secretKey = new SecretKeySpec(sharedSecretBytes, "DES");

        // Encrypt and send message to Alice containing Bob's identity and nonce NA
        byte[] messageToAlice = (Bobidentity + "," + aliceNonce).getBytes();
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedMessage = cipher.doFinal(messageToAlice);
        out.writeObject(encryptedMessage);

        // Receive encrypted message from Alice containing Alice's identity and nonce NB
        byte[] encryptedResponse = (byte[]) in.readObject();

        System.out.println("Encrypted message from Alice (3) " + Arrays.toString(encryptedResponse));

        byte[] decryptedResponse = cipher.doFinal(encryptedResponse);
        String[] parts = new String(decryptedResponse).split(",");
        String aliceIdentityReceived = parts[0];
        int receivedNonce = Integer.parseInt(parts[1]);

        System.out.println("Decrypted Identity from Alice (3) " + aliceIdentityReceived);
        System.out.println("Decrypted Nonce from Alice (3) " + receivedNonce);



        // Close the connection
        socket.close();
        serverSocket.close();
    }
}
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.Arrays;

public class Alice {
    public static void main(String[] args) throws Exception {
        // Connect to Bob

        String hostName = "localhost";
        int portNumber = 1500;

        //Socket socket = new Socket("localhost", 12345);
        try (
            Socket socket = new Socket(hostName, portNumber);
        ) {

            ObjectOutputStream outt = new ObjectOutputStream(socket.getOutputStream());

            int nonce = 5;
            String identity = "Alice";

            // Send identity and nonce to Bob
            outt.writeObject(identity);
            outt.writeInt(nonce);

            // Receive nonce and encrypted message from Bob
            ObjectInputStream inn = new ObjectInputStream(socket.getInputStream());

                int receivedNonce = inn.readInt();
                byte[] encryptedMessage = (byte[]) inn.readObject();

                System.out.println("Encrypted message from Bob (2) " + Arrays.toString(encryptedMessage));
                System.out.println("Bobs Nonce (2) " + receivedNonce);

                // Generate shared secret key using both nonces
                byte[] sharedSecretBytes = (Integer.toString(nonce) + Integer.toString(receivedNonce)).getBytes();
                SecretKey secretKey = new SecretKeySpec(sharedSecretBytes, "DES");

                // Decrypt and extract Bob's identity and nonce NA
                Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, secretKey);
                byte[] decryptedMessage = cipher.doFinal(encryptedMessage);
                String[] parts = new String(decryptedMessage).split(",");
                String bobIdentity = parts[0];
                int bobNonce = Integer.parseInt(parts[1]);

                System.out.println("Decrypted Bobs identity (2) " + bobIdentity);
                System.out.println("Decrypted nonce (2) " + bobNonce);


                // Send encrypted message to Bob containing Alice's identity and nonce NB
                byte[] messageToBob = (identity + "," + receivedNonce).getBytes();
                byte[] encryptedResponse = cipher.doFinal(messageToBob);
                outt.writeObject(encryptedResponse);
                // Close the connection


        }
        catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " +
                    hostName);
            System.exit(1);
        }

    }
}
0

There are 0 answers