1

Good morning, I have a quick question regarding the differences between a byte object in python (denoted b'') and how to replicate it in java.

The project I am working on is some personal work on an emulation server for a dead game to better my reversing skills. I have a working rendition of the project in python, but would like to switch over to java as I am better with the language and it comes with many additional tools included that are useful for a project like this.

I am using a ServerSocket to capture TCP data in the java project.

When data comes over the network from the Python project it looks a little something like this:

enter image description here

When I capture the same data over the java ServerSocket I get something like this:

enter image description here

My question is how can I reformat this ASCII text to get the proper data as seen in the python version of the software.

Currently I am able to get an output like this:

enter image description here By converting the byte[] data from the ServerSocket as such

while(true) {
        try {
            Socket socket = serverSocket.accept();
            onConnection(socket);

            byte[] incomingData = new byte[0];
            byte[] temp = new byte[1024];
            int k = -1;

            //this is due to the client of said game not sending EOL (readLine() does not work here)
            while((k = socket.getInputStream().read(temp, 0, temp.length)) > -1) {
                byte[] tbuff = new byte[incomingData.length + k];
                System.arraycopy(incomingData, 0, tbuff, 0, incomingData.length);
                System.arraycopy(temp, 0, tbuff, incomingData.length, k);
                incomingData = tbuff;

                receiveData(socket, incomingData); <--- this is the important bit
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

public void receiveData(Socket socket, byte[] data) {
    int lenLo = (int) (data[0]);
    int lenHi = (int) (data[1]);
    int length = lenHi * 256 + lenLo;


    if(lenHi < 0) {
        System.out.println("Invalid Packet Length");
    }

    if(data.length != length) {
        System.out.println("Incomplete Packet Received");
    }

    try {
        String test = new String(data, "UTF-8");

        serverGUI.serverDebug(test); //produces the string in a jframe (pic 2)
        serverGUI.debugByteArray(test.getBytes(StandardCharsets.UTF_8)); //produces the byte[] in jframe (pic 3 -- all bytes in this array are & 0xff prior to being printed out)
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

However this has obviously not produced the desired outcome. Any advice is appreciated or any resources that can be put forth are also appreciated.

Thanks in advance!

  • 1
    Not entirely clear what you're asking. Are you asking about how to pretty-print binary data that includes nonprintable characters, i.e. the replicate the formatting that Python uses when printing a bytestream to the console? If so, see https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java or `String.format("\x%02x", byteValue);` – Peteris Feb 18 '19 at 22:55
  • Sorry if I wasn't clear... looking back I wasn't. The java output seems to have additional spaces in it? This data is being fed into a Blowfish decryption using the same params (ecb -- no padding), python produces the correct output data whereas java does not.Both renditions of the software are using the same decryption key as well – Cody Hafemeister Feb 19 '19 at 01:06
  • "The java output seems to have additional spaces in it" does it? the gap in your example seems to correspond to the \t character in the python string. I'd suggest to print a standard hex representation (everything to hex - not as in the current python example) in both languages and compare that. – Peteris Feb 19 '19 at 02:27
  • Ok research is showing me that java BlowfishEngine uses Blowfish compat? I think the difference is essentially endianness. The python implementation is using little endian byte order and the data is meant to be transferred as such. Do you know a way to decode Blowfish in java using little endian byte order? Ordering the input data in a bytebuffer doesnt seem to result in the correct output. – Cody Hafemeister Feb 20 '19 at 00:27
  • That's a very different question that should be asked separately. – Peteris Feb 20 '19 at 06:00

0 Answers0