我试图在我的文件中获取加密数据。但我得到一个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小时都试图解决这个问题。我仍然无法弄清楚。
答案 0 :(得分:1)
您正在使用两个不同的密钥。您需要使用相同的密钥进行解密和加密。