如何序列化和反序列化CipherInputStream对象

时间:2013-07-25 09:51:13

标签: java exception serialization

我在序列化cipherinputstream对象时遇到问题。每当我尝试这样做时,我总是会遇到此异常,这是我的代码片段

public class Crypto implements java.io.Serializable
{

public Crypto(String filename)
{

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
SecretKeySpec secretkey = new SecretKeySpec(key(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretkey);
CipherInputStream cipt = new CipherInputStream(new FileInputStream(new File(filename)), cipher)

ByteArrayOutputStream baos = new ByteArrayOutputStream();

  ObjectOutputStream obj = null;

           try
           {
                obj =   new ObjectOutputStream(baos);
                obj.writeObject(cipt);
                byte[] bv = baos.toByteArray();
                System.out.println(bv);

           }
           catch(Exception b)
           {
           b.printStackTrace();
           }
           finally
           {
            obj.close();
            baos.close();
           }
      }
     }

例外:

java.io.NotSerializableException: javax.crypto.CipherInputStream.
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)

任何人都可以帮助我这样做。我的目标是将cipherinputstream对象转换为字节或字节数组。

1 个答案:

答案 0 :(得分:-1)

可能U不会实现serialiazable标记类。假设A类是需要序列化的那个,那么:

class A implements serializable {

  //class variables and methods


}