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

时间:2015-11-20 04:18:19

标签: 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)

FootballClub Class

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();
    }

NEW EXCEPTION

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)

1 个答案:

答案 0 :(得分:0)

cipher = Cipher.getInstance("DES") 

而不是

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

也可以检查你的括号,然后你在第一种方法中返回一些东西但是它们没有返回类型