【问题标题】:StreamCorruptedException when Serializing and encrypting ArrayList object序列化和加密 ArrayList 对象时出现 StreamCorruptedException
【发布时间】:2015-11-20 10:10:06
【问题描述】:

我正在尝试在我的文件中获取加密数据。但我得到一个 java.io.StreamCorruptedException。 以下是我的代码

public ArrayList<FootballClub> FootBallInputStream() throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
File file = new File("FootballClub.ser");
fileIn = new FileInputStream(file);

SecretKey key = KeyGenerator.getInstance("AES").generateKey();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);

CipherInputStream cipherIn = new CipherInputStream(fileIn, cipher);
in = new ObjectInputStream(cipherIn);

SealedObject sealed = (SealedObject) in.readObject();

ArrayList<FootballClub> e = (ArrayList<FootballClub>) sealed.getObject(cipher);

in.close();

fileIn.close();

return e;

}
public void FootBallOutputStream(ArrayList<FootballClub> e) throws FileNotFoundException, IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException {

File file = new File("FootballClub.ser");
fileOut = new FileOutputStream(file);


SecretKey key = KeyGenerator.getInstance("AES").generateKey();
Cipher cipher = (Cipher.getInstance("AES"));
cipher.init(Cipher.ENCRYPT_MODE, key);
SealedObject sealed = new SealedObject(e, cipher);

CipherOutputStream cipherOut = new CipherOutputStream(fileOut, cipher);
out = new ObjectOutputStream(cipherOut);
out.writeObject(sealed);
out.close();
fileOut.close();
}

我的例外

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: CF8CA0C1
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
    at premierleague.controller.Serializing.FootBallInputStream(Serializing.java:54)

请帮我解决这个例外。我已经尝试解决这个问题将近 24 小时。我还是想不通。

【问题讨论】:

  • 您使用密码流和 SealedObjects 进行了两次加密和解密。为什么?

标签: java encryption serialization arraylist


【解决方案1】:

您正在使用两个不同的键。您需要使用与加密相同的密钥进行解密。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    相关资源
    最近更新 更多