gpt4 book ai didi

java - 加密和序列化时出现 NullPointerException 异常

转载 作者:行者123 更新时间:2023-12-01 10:51:56 24 4
gpt4 key购买 nike

我正在尝试加密 ArrayList 类型并将其序列化。以下是我的代码。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import premierleague.model.FootballClub;
import premierleague.model.Match;

/**
*
* @author Akila
*/
public class Serializing implements Serializable{

private FileInputStream fileIn;
private FileOutputStream fileOut;
private ObjectInputStream in;
private ObjectOutputStream out;

public ArrayList<FootballClub> FootBallInputStream() throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException {
Cipher cipher = Cipher.getInstance("DES");
File file = new File("FootballClub.ser");
fileIn = new FileInputStream(file);
CipherInputStream CipherIn = new CipherInputStream(in, cipher);
in = new ObjectInputStream(CipherIn);
ArrayList<FootballClub> e = (ArrayList<FootballClub>) in.readObject();
in.close();
fileIn.close();

return e;

}

public void FootBallOutputStream(ArrayList<FootballClub> e) throws FileNotFoundException, IOException, NoSuchAlgorithmException, NoSuchPaddingException {
Cipher cipher = Cipher.getInstance("DES");
File file = new File("FootballClub.ser");
fileOut = new FileOutputStream(file);
CipherOutputStream cipherOut = new CipherOutputStream(out,cipher);
out = new ObjectOutputStream(cipherOut);
out.writeObject(e);
out.close();
fileOut.close();
}


}

虽然我在尝试使用这些方法时遇到 NullPointer 异常。

Exception in thread "main" java.lang.NullPointerException
at javax.crypto.CipherInputStream.getMoreData(CipherInputStream.java:103)
at javax.crypto.CipherInputStream.read(CipherInputStream.java:224)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2289)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2302)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2773)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:798)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at premierleague.controller.Serializing.FootBallInputStream(Serializing.java:41)

我已经初始化了 CipherInputStream 对象和 ObjectInputStream 对象。然而,它抛出了一个空指针异常

编辑

 public ArrayList<FootballClub> FootBallInputStream() throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {

SecretKey key = KeyGenerator.getInstance("DES").generateKey();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
File file = new File("FootballClub.ser");
fileIn = new FileInputStream(file);
CipherInputStream CipherIn = new CipherInputStream(fileIn, cipher);
in = new ObjectInputStream(CipherIn);
ArrayList<FootballClub> e = (ArrayList<FootballClub>) in.readObject();
in.close();
fileIn.close();

return e;

}

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

SecretKey key = KeyGenerator.getInstance("DES").generateKey();
Cipher cipher = (Cipher.getInstance("DES"));
cipher.init(Cipher.ENCRYPT_MODE, key);
File file = new File("FootballClub.ser");
fileOut = new FileOutputStream(file);
CipherOutputStream cipherOut = new CipherOutputStream(fileOut, cipher);
out = new ObjectOutputStream(cipherOut);
out.writeObject(e);
out.close();
fileOut.close();
}

异常

Exception in thread "main" java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2304)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2773)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:798)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at premierleague.controller.Serializing.FootBallInputStream(Serializing.java:47)

足球俱乐部类

package premierleague.model;


/**
*
* @author Akila
*/
public class FootballClub extends SportsClub implements Comparable<FootballClub>{

private int wins;
private int defeats;
private int draws;
private int currentPoints=0;
private int goalsRecieved;
private int goalsScored;
// public FootballClub(String name, String location) {
// super.getClubName()= ;
// super.getLocation()= location;
//
// }




public int getWins() {
return wins;
}

public void setWins(int wins) {
this.wins = wins;
}

public int getDefeats() {
return defeats;
}

public void setDefeats(int defeats) {
this.defeats = defeats;
}

public int getDraws() {
return draws;
}

public void setDraws(int draws) {
this.draws = draws;
}

public int getCurrentPoints() {
return currentPoints;
}

public void setCurrentPoints(int currentPoints) {
this.currentPoints = currentPoints;
}

public int getGoalsRecieved() {
return goalsRecieved;
}

public void setGoalsRecieved(int goalsRecieved) {
this.goalsRecieved = goalsRecieved;
}

public int getGoalsScored() {
return goalsScored;
}

public void setGoalsScored(int goalsScored) {
this.goalsScored = goalsScored;
}

// @Override
// public int compareTo(FootballClub o) {
// if (this.getCurrentPoints()> o.getCurrentPoints()) {
// return 1;
//
// }else if (this.getCurrentPoints()== o.getCurrentPoints()){
// if (this.getCurrentPoints()> o.getCurrentPoints()) {
// return 1;
//
// }else{
// return -1;
// }
// }else{
// return -1;
// }
//
// }
//

@Override
public int compareTo(FootballClub o) {
if(this.getCurrentPoints()>o.getCurrentPoints()){
return 1;
}else if(this.getCurrentPoints()==o.getCurrentPoints()){
if(this.getCurrentPoints()>o.getCurrentPoints()){
return 1;
}else {
return -1;
}
}else{
return -1;
}
}
public static final long serialVersionUID = 948599023243074087L;
}

新编辑

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)

最佳答案

尝试

cipher = Cipher.getInstance("DES") 

而不是

Cipher cipher = Cipher.getInstance("DES");

也可能检查你的括号,你在第一个方法中返回了一些东西,但它们不是返回类型

关于java - 加密和序列化时出现 NullPointerException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33818911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com