I am trying to serialize a Message object using ObjectOutputStream ,take the byte[ ] output of the serializer,encrypt it using an encryption tool and then trying to de-serialize it and cast it as an object. It gives an error - invalid stream header. Is it not possible to modify the OutputStream after serializing it and then de-serialize it? Excuse me if I am doing something atrocious, I am a novice to Java. Code for the serializer - serialMsg():
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(this); bytes = bos.toByteArray(); out.close();
The output of the serializer is then used as below:
byte data = this.serialMsg(); // This is where I serialize the Message object byte out = util.Encrypt(data, fromsk , to.toString(), param); // This is where I encrypt the byte[ ] - data and obtain an encrypted byte [ ] - out
The object is then de-serialised as:
ByteArrayInputStream in = new ByteArrayInputStream(out); ObjectInputStream objin = new ObjectInputStream(in) ; msg = (Message)objin.readObject(); // This is where I cast the output of readObject into the type Message - the original type of the object that was serialized.
I hope the objective behind this is clear. I am trying to modify the output stream and then trying to cast it back into the original type.