Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反序列化加密对象时为什么会得到EOFEException?_Java_Postgresql_Encryption - Fatal编程技术网

Java 反序列化加密对象时为什么会得到EOFEException?

Java 反序列化加密对象时为什么会得到EOFEException?,java,postgresql,encryption,Java,Postgresql,Encryption,我正在通过EncryptedByTearrayOutstream将一个序列化和加密的对象写入一个大型对象中的数据库。我可以检索这个大对象,但无法反序列化它 以下是我的写作代码: public void addMessages(int ID, List<Message> messages) { ObjectOutput output = null; ByteArrayOutputStream baos = new ByteArrayOutputStream();

我正在通过EncryptedByTearrayOutstream将一个序列化和加密的对象写入一个大型对象中的数据库。我可以检索这个大对象,但无法反序列化它

以下是我的写作代码:

public void addMessages(int ID, List<Message> messages) {

    ObjectOutput output = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteArrayInputStream bais = null;
    output = new ObjectOutputStream(CryptoHandler.encryptedStream(baos));
    output.writeObject(messages);
    bais = new ByteArrayInputStream(baos.toByteArray());
    output.close();
    baos.close();
    getMessages(ID, bais);
}

更新

我再次查看了您的代码,发现您应该在写入ObjectOutputStream后立即关闭它:

public void addMessages(int ID, List<Message> messages) {

    ObjectOutput output = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteArrayInputStream bais = null;
    output = new ObjectOutputStream(CryptoHandler.encryptedStream(baos));
    output.writeObject(messages);
    output.close(); // it shouldn't remain open after writing the object
    bais = new ByteArrayInputStream(baos.toByteArray());
    getMessages(ID, bais);
}
public void addMessages(int-ID,列表消息){
ObjectOutput=null;
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
ByteArrayInputStream bais=null;
输出=新的ObjectOutputStream(CryptoHandler.encryptedStream(BAS));
output.writeObject(消息);
output.close();//写入对象后不应保持打开状态
bais=新的ByteArrayInputStream(baos.toByteArray());
获取消息(ID,bai);
}

但我还建议您将baos.toByteArray()保存到byte[]中,而不是将ByteArrayInputStream作为参数。您应该传递字节数组,并在getMessages函数中生成ByteArrayInputStream,以使代码更具可读性。

您是否尝试将ObjectInputStream而不是ObjectInput添加到链中?如果将写+加密代码的输出直接链接到读+解密代码,而不将其存储到数据库?@AdamK CyptoHandler.decryptedObjectStream返回一个ObjetInputStream@OlegEstekhin好主意,我进行了单元测试,我将更新问题。当然,在从ByteArrayOutputStream获取字节数组之前,您应该关闭ObjectOutputStream。baos.close()是多余的。
public static CipherOutputStream encryptedStream(OutputStream out) {
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        return new CipherOutputStream(out, pbeCipher);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(CryptoHandler.class.getName()).log(Level.SEVERE, null, ex);
    }//Much more catches here...
}
public static ObjectInputStream decryptedObjectStream(InputStream in) {
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        return new ObjectInputStream(new CipherInputStream(in, pbeCipher));
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(CryptoHandler.class.getName()).log(Level.SEVERE, null, ex);
    } //Much more catches here...
}
public void addMessages(int ID, List<Message> messages) {

    ObjectOutput output = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteArrayInputStream bais = null;
    output = new ObjectOutputStream(CryptoHandler.encryptedStream(baos));
    output.writeObject(messages);
    output.close(); // it shouldn't remain open after writing the object
    bais = new ByteArrayInputStream(baos.toByteArray());
    getMessages(ID, bais);
}