1

I'm trying to do a server client communication using UDP and it involves sending encrypted text using RC4 to each other. It goes something like this:

  1. Start Host.

  2. Start Client.

  3. Client sends encrypted text using RC4

  4. Host receive encrypted text and decrypt using RC4

My RC4 is taken from an online source and it seems to be working. If I were to do both encryption and decryption on client side (for testing purpose), it works. But the problem occurs after I sent my encrypted text over to the host. When my host decrypts the message the output isn't the expected output.

Here is an example of my code on client:

RC4 rc4 = new RC4(rc4Key);      
String message = "Hello";       
char[] result = rc4.encrypt(message.toCharArray());
System.out.println("encrypted string: " + new String(result));              //M®FW?
System.out.println("decrypted string: " + new String(rc4.decrypt(result))); //Hello

From the above, I assume my RC4 is working because I seem to be able to encrypt and decrypt properly. So now I send the encrypted text over to my host

sentence = new String(result);
sendData = sentence.getBytes();
sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);        
clientSocket.send(sendPacket);

And on my host side, I will be receiving the encrypted text

receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
realSentence = Arrays.copyOf(receivePacket.getData(), receivePacket.getLength());
sentence = new String(realSentence);
RC4 rc4 = new RC4(ad.toString());               
char[] result = rc4.decrypt(sentence);
System.out.println("decrypted string: " + new String(result)); //H?ll?

This occurs only half the time, and I'm seeing a pattern that it only occurs when my encrypted text contains ? as a special character. So I'm guessing that when I convert char to string and then to byte and send over through UDP, something went wrong.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
user1958567
  • 67
  • 1
  • 6

1 Answers1

2

For future reference, a good way to debug this is to forget all about the crypto and write a test that sends a known byte array (containing a special character) from one side to the other.

That being said, I think this line is at fault:

sendData = sentence.getBytes();

Here you are not specifying a character set when obtaining the bytes. This means you are using the platform default. At the other end of your connection, you are doing this:

sentence = new String(realSentence);

Again, converting bytes to a string without specifying a character set. My guess is that your destination has a different default character set.

Note: this code seems strange in general. Why should an RC4 cipher return a char array? I would hunt instead for example code that manages byte arrays and just send the raw values across the network.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • Hi Duncan, thanks for the input. My result is in charArray so i will have to convert them to bytes before sending them. Will it result to lose data too? Edit: I have no idea too. Maybe its a better choice for me to find another RC4 Example that uses byte array? – user1958567 Jan 28 '14 at 09:08
  • @user1958567 Either a) specify a character set, such as UTF-8 at both ends, or b) (preferably) find an RC4 implementation that works with byte arrays not character arrays. – Duncan Jones Jan 28 '14 at 09:10