0

How would one go about making a byte[] that is a packet that needs to be run through an RC4 encryption class and then sent to a server?

So let's say I need the packet to stsrt with a string, followed with an int, byte, int, string. How would I create that as a byte array? (Byte[])

Thanks!

1 Answers1

0

OK, so you have a data structure containing strings, integers and bytes, that you want to serialize to a byte array. There are several options:

  • create a Serializable class containing all this information as fields, and use an ObjectOutputStream to write it. Beware: the result will only be easily readable by a Java program using the exact same class.
  • create a class containing all this information as fields, and use a JSON object mapper (like Jackson) to write it.
  • create a class containing all this information as fields, and use an XML object mapper (like JAXB) to write it.
  • design a binary representation of this structure, that can be transformed back into the individual parts, and use a DataOutputStream to write it.
  • Use protocol buffers
  • ...
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • "design a binary representation of this structure, that can be transformed back into the individual parts, and use a DataOutputStream to write it." how can I go about doing this? –  Sep 26 '15 at 09:44
  • The simplest way is probably to use protocol buffers. But, for example, suppose your messages is an int, followed bya string, followed by an int, you could write the int, then write the length of the string as an int, then write the string, then write the int. – JB Nizet Sep 26 '15 at 09:49
  • What do u exactly mean? Do u have any examples? –  Sep 26 '15 at 13:23
  • Have you clicked on the link? It leads to the javadoc of DataOutputStream. This javadoc explains how the class works and shows all its methods, like writeInt(), writeUTF(), etc. Have you tried something? – JB Nizet Sep 26 '15 at 13:25