Proper way to read bytes through TCP Client [Solved]

I am experiencing a mis match of data being sent from my server application when compared to what is shown as received on the Spark Core TCP Client.

Here is the snippet of code for my server(Java) which sends a string converted to byte format:

public void clientReply(){
	String reply = "Hi, how are you Mr Spark Core";
	// initialize the cipher for encrypt mode
	Cipher cipher;
	try {
		cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
		IvParameterSpec ivspec = new IvParameterSpec(iv);
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);

		// encrypt the message
		byte[] encrypted = cipher.doFinal(reply.getBytes());

		System.out.println("Data Being Sent(HEX): "+bytesToHex(encrypted));
		System.out.println("Data Being Sent(RAW): "+Arrays.toString(encrypted));

		if(clientSocket.isConnected()){
			clientSocket.getOutputStream().write(encrypted);
			//	        	clientSocket.close();
		}

	} catch (NoSuchAlgorithmException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchPaddingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InvalidKeyException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InvalidAlgorithmParameterException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalBlockSizeException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (BadPaddingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}

public static String bytesToHex(byte[] bytes) {

		StringBuilder sBuilder = new StringBuilder();

		for (int i = 0; i < bytes.length; i++) {
			char c = (char) bytes[i];
			char hexDigit = hexArray[(c >> 4) & 0xF];
			sBuilder.append(hexDigit);
			hexDigit = hexArray[c & 0xF];
			sBuilder.append(hexDigit);
			sBuilder.append(' ');
		}
		sBuilder.append("\r\n");

		return sBuilder.toString();
	}

Here is the snippet of test code on my Spark Core which reads this data from the server:

void javaTest(char message[], String action){
	client.connect(server, 5000);
	while(!client.connected()){

	}
	client.write(message);
//	client.println(message);
	while(client.available() == 0){
		Serial.println("Waiting for response");
		delay(1000);
	}
	delay(200);
	Serial.println("response received");

	unsigned char returnData[client.available()];

	client.read(returnData, sizeof returnData);

	Serial.print("Data from Server(HEX): ");
	hexPrint(returnData, sizeof returnData);
	Serial.print("Data from Server(RAW): ");
	Serial.println((const char*)returnData);
	
//	cbcDecrypt(returnData, sizeof returnData);
}

void hexPrint(const unsigned char *buf, size_t len) {
	const char hex[] = "0123456789ABCDEF";
	for (size_t i = 0; i < len; i++) {
		char c = buf[i];
		char hexDigit = hex[(c >> 4) & 0xF];
		Serial.write(hexDigit);
		hexDigit = hex[c & 0xF];
		Serial.write(hexDigit);
		Serial.write(' ');
	}
	Serial.print("\r\n");
}

Here is the output printed by the Java Server:

Data Being Sent(HEX): 6D D6 1E EA C9 E9 56 6C 07 C4 7D 3C 1A 7D EE EE 0A 1A D4 10 DD C6 16 9E D1 14 00 C2 BB 04 7A 4E 

Data Being Sent(RAW): [109, -42, 30, -22, -55, -23, 86, 108, 7, -60, 125, 60, 26, 125, -18, -18, 10, 26, -44, 16, -35, -58, 22, -98, -47, 20, 0, -62, -69, 4, 122, 78]

Here is the output printed by the Spark:

Data from Server(HEX): 92 41 BB 66 56 0A 9E 08 CD C6 35 DC A9 3D 98 7F 16 A5 E2 1B E9 5A 0A 92 7E 5D 35 E6 F4 13 E0 1D
Data from Server(RAW): ’A»fV
                            ÍÆ5Ü©=˜¥âeéZ
                                          ’~]5æôà[À¨  °

As you can see the byte array is printed out on the server prior to write. Then as soon as the data is received on the Spark it is printed out. However the two do not match. Am I doing something wrong here?

Thanks Everyone

1 Like

Please disregard this post. I finally found the problem on my Java Server.

2 Likes

Cool, thanks for letting us know it’s solved! :slight_smile:

Thanks,
David