2015-11-20 52 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

您正在使用兩個不同的密鑰。您需要使用與加密相同的密鑰進行解密。

相關問題