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

标签 java encryption serialization nullpointerexception

我正在尝试加密 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/

相关文章:

java - 如何用Java加密用PHP解密?

php - iOS : kCCDecodeError -4304

JSONMarshal : Is it possible to serialize/desrialize into/from 'plain' JSON (without metadata)?

c++ - 使用 boost::serialization 将派生类指针序列化为 vector 的问题

java - 将 GWT 升级到 v2.4 时出错

java - Android 转换字符串

java - 记录 JFR 时出现错误

svn - 通过 gnome-keyring 为 ssh(仅限)客户端颠覆客户端加密密码

java - 组件的定位(如何将几个按钮放置在屏幕中央相同大小)

基于 C++ STL 的二进制序列化数据发送,使用套接字进行网络传输,无需使用库