1

We are using spring framwork in java. we have to send http post req from 1 server to anther, the msg should be base 64 and encrypted.

The code and decode of base 64 works fine, as well the encryption, back than we where transfering string and everything was fine, now we are sending an object insted.

public class AuthenticationDTO implements Serializable{

    private byte[] publicKey;
    private byte[] privateKey;
    private byte[] cert;
    private String secret;

    public AuthenticationDTO() {}

    public AuthenticationDTO(byte[] publicKey, byte[] privateKey, String secret) {
        this.publicKey = publicKey;
        this.privateKey = privateKey;
        this.secret = secret;
    }

    public String getSecret() {
        return this.secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public byte[] getPublicKey() {
        return publicKey;
    }

    public byte[] getPrivateKey() {
        return privateKey;
    }

    public void setPublicKey(byte[] publicKey) {
        this.publicKey = publicKey;
    }

    public void setPrivateKey(byte[] privateKey) {
        this.privateKey = privateKey;
    }

    public byte[] getcert() {
        return cert;
    }

    public void setcert(byte[] cert) {
        this.cert = cert;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AuthenticationDTO that = (AuthenticationDTO) o;

        if (!Arrays.equals(publicKey, that.publicKey)) return false;
        if (!Arrays.equals(privateKey, that.privateKey)) return false;
        if (!Arrays.equals(cert, that.cert)) return false;
        return !(secret != null ? !secret.equals(that.secret) : that.secret != null);

    }

    @Override
    public int hashCode() {
        int result = publicKey != null ? Arrays.hashCode(publicKey) : 0;
        result = 31 * result + (privateKey != null ? Arrays.hashCode(privateKey) : 0);
        result = 31 * result + (cert != null ? Arrays.hashCode(cert) : 0);
        result = 31 * result + (secret != null ? secret.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "AuthenticationDTO{" +
                "publicKey=" + Arrays.toString(publicKey) +
                ", privateKey=" + Arrays.toString(privateKey) +
                ", cert=" + Arrays.toString(cert) +
                ", secret='" + secret + '\'' +
                '}';
    }
}

This is how i'm serialize the object :

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(authenticationDTO);
byte[] bytes = bos.toByteArray();

and this is how i deserialize the object:

ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream in = new ObjectInputStream(bis);
AuthenticationDTO authenticationDTO = null;
authenticationDTO = (AuthenticationDTO) in.readObject();

and getting an exception :

java.io.StreamCorruptedException: invalid stream header: 57FAC6C7
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806) ~[na:1.8.0_60]
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299) ~[na:1.8.0_60]

any idea?

USer22999299
  • 5,284
  • 9
  • 46
  • 78
  • Possibly a duplicate of http://stackoverflow.com/questions/23262160/java-io-streamcorruptedexception-invalid-stream-header-54657374 – PA001 Dec 17 '15 at 15:49

0 Answers0