2015-11-20 22 views
1

我试图在我的文件中获取加密的数据。但我得到一个java.io.StreamCorruptedException。 以下是我的代码序列化和加密ArrayList对象时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

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

回答

1

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

相关问题