2013-10-07 61 views
0

我要加密类型EnteredDetails的ArrayList(的Java bean),并将其序列化到file.I我下面这个链接,AES-128位加密:http://www.code2learn.com/2011/06/encryption-and-decryption-of-data-using.html鉴于最终块未正确填充

使用的方法aes类的加密必须将arrarylist转换为字节数组,使用方法encrypt将其加密并使用fileoutputstream将其写入文件。

现在在反序列化方法中,我使用fileinputstream读取加密的字节数组,使用解密方法解密字节数组,并使用objectinputstream从解密的字节数组中取回我的数组列表。

这是我的Serialize方法:

public void serialize() { 
    try { 
     ByteArrayOutputStream b = new ByteArrayOutputStream(); 
     ObjectOutputStream o = new ObjectOutputStream(b); 
     o.writeObject(ar5.userDetails); 
     AES_Encryption en = new AES_Encryption(); 
     byte[] data=en.encrypt(b.toByteArray()); 
     FileOutputStream fos = new FileOutputStream("user.txt"); 
     fos.write(data); 
     b.close(); 
     fos.close(); 
     o.close(); 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 

编辑Deserialize方法:

 ArrayList<EnteredDetails> load() { 
    try { 
     File file=new File("user.txt"); 
     FileInputStream fis = new FileInputStream("user.txt"); 
     //   System.out.println("after fisssssss"); 
     //   ObjectInputStream ois = new ObjectInputStream(fis); 
     //   byte [] d =(byte []) ois.readObject(); 
     byte fileContent[] = new byte[(int)file.length()]; 
     AES_Encryption enc = new AES_Encryption(); 
     byte[] data = enc.decrypt(fileContent); 
     ByteArrayInputStream b = new ByteArrayInputStream(data); 
     ObjectInputStream ois2 = new ObjectInputStream(b); 
     ArrayList<EnteredDetails> al = (ArrayList<EnteredDetails>) ois2.readObject(); 
     fis.close(); 
     b.close(); 
     ois2.close(); 
     return al; 
    } catch (Exception e) { 
     System.out.println("exception in method load deseialize class " +     e.getMessage()); 
     return null; 
    } 
} 

错误:由于最后一个块未正确填充

回答

0

你必须以相反的顺序来应用过滤器。

在读取对象流之前,您必须解密加密的数据。

+0

我编辑了desrialize方法,但现在它给出了错误 鉴于最终块没有正确填充 – sugam

+0

你应该关闭你的流,当你完成它们。 –

+0

我已经关闭它们,但错误仍然存​​在。 – sugam

相关问题