2013-07-25 56 views
-1

我在序列化cipherinputstream对象时遇到问题。我总是得到这个异常每当我试图做到这一点,这里是我的代码片段如何序列化和反序列化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对象转换为字节或字节数组。

+0

U是否实现了序列化? –

+0

你想实现什么?你想写密码本身还是真的是对象'CipherInputStream'? –

+0

对象CipherInputStream –

回答

-1

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

class A implements serializable { 

    //class variables and methods 


} 
+0

我刚刚编辑了代码,仍然有相同的错误 –