Java 序列化和加密ArrayList对象时StreamCorruptedException
我正在尝试获取文件中的加密数据。但是我得到一个java.io.StreamCorruptedException。 下面是我的代码Java 序列化和加密ArrayList对象时StreamCorruptedException,java,encryption,serialization,arraylist,Java,Encryption,Serialization,Arraylist,我正在尝试获取文件中的加密数据。但是我得到一个java.io.StreamCorruptedException。 下面是我的代码 public ArrayList<FootballClub> FootBallInputStream() throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKe
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();
}
public ArrayList FootBallInputStream()抛出FileNotFoundException、IOException、ClassNotFoundException、NoSuchAlgorithmException、NoSuchPaddingException、InvalidKeyException、IllegalBlockSizeException、BadPaddingException{
File File=新文件(“FootballClub.ser”);
fileIn=新文件输入流(文件);
SecretKey key=KeyGenerator.getInstance(“AES”).generateKey();
Cipher Cipher=Cipher.getInstance(“AES”);
cipher.init(cipher.DECRYPT_模式,密钥);
CipherInputStream cipherIn=新的CipherInputStream(fileIn,cipher);
in=新对象输入流(cipherIn);
SealedObject sealed=(SealedObject)在.readObject()中;
ArrayList e=(ArrayList)sealed.getObject(cipher);
in.close();
fileIn.close();
返回e;
}
public void FootBallOutputStream(ArrayList e)抛出FileNotFoundException、IOException、NoSuchAlgorithmException、NoSuchPaddingException、InvalidKeyException、IllegalBlockSizeException{
File File=新文件(“FootballClub.ser”);
fileOut=新的FileOutputStream(文件);
SecretKey key=KeyGenerator.getInstance(“AES”).generateKey();
Cipher Cipher=(Cipher.getInstance(“AES”);
cipher.init(cipher.ENCRYPT_模式,密钥);
SealedObject sealed=新的SealedObject(e,密码);
CipherOutputStream cipherOut=新的CipherOutputStream(文件输出,密码);
out=新对象输出流(cipherOut);
out.writeObject(密封);
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)
线程“main”java.io.StreamCorruptedException中的异常:无效流头:CF8CA0C1
位于java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
位于java.io.ObjectInputStream。(ObjectInputStream.java:298)
在英超联赛中.controller.serialization.FootBallInputStream(serialization.java:54)
请帮我处理这个例外。我已经尝试解决这个问题将近24小时了。我还是搞不懂。你用的是两个不同的键。解密需要使用与加密相同的密钥。加密和解密两次,使用密码流和密封对象。为什么?