2013-05-06 109 views
1

我想序列化一个对象(在这种情况下是一个简单的字符串),加密它,并将其写入文件。加密似乎可行,但解密总是失败。我试着摸索,但我似乎无法找出我在做什么错..使用DES加密和解密Java文件

// Create a new key to encrypt and decrypt the file 
byte[] key = "password".getBytes(); 

// Get a cipher object in encrypt mode 
Cipher cipher = null; 
try { 
    DESKeySpec dks = new DESKeySpec(key); 
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); 
    SecretKey desKey = skf.generateSecret(dks); 
    cipher = Cipher.getInstance("DES"); 
    cipher.init(Cipher.ENCRYPT_MODE, desKey); 
} catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException ex) { 
    System.err.println("[CRITICAL] Incryption chiper error"); 
} 

// Encrypt the file 
try { 
    new ObjectOutputStream(new CipherOutputStream(new FileOutputStream("test"), cipher)).writeObject("test text"); 
} catch (IOException e) { 
    System.err.println("[CRITICAL] Error encrypting data: " + e.getMessage()); 
    e.printStackTrace(); 
} 

// Get a cipher object in decrypt mode 
try { 
    DESKeySpec dks = new DESKeySpec(key); 
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); 
    SecretKey desKey = skf.generateSecret(dks); 
    cipher = Cipher.getInstance("DES"); 
    cipher.init(Cipher.DECRYPT_MODE, desKey); 
} catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException ex) { 
    System.err.println("[CRITICAL] Incryption chiper error"); 
} 

// Decrypt the file 
try { 
    // This is the line that throws the exception 
    System.out.println((String) new ObjectInputStream(new CipherInputStream(new FileInputStream("test"), cipher)).readObject()); 
} catch (IOException | ClassNotFoundException e) { 
    System.err.println("[CRITICAL] Error decrypting data: " + e.getMessage()); 
    e.printStackTrace(); 
} 

以下异常运行上面的代码的结果:

[CRITICAL] Error decrypting data: null 
java.io.EOFException 
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2304) 
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3042) 
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2843) 
at java.io.ObjectInputStream.readString(ObjectInputStream.java:1617) 
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1338) 
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369) 
at Server.DataPersistence.main(DataPersistence.java:203) 

有谁有什么想法?

谢谢!

回答

5

我的猜测是,当您尝试打开并重新读取数据返回到您的程序时,没有任何内容写入文件。在尝试再次读取文件之前,尝试在输出流上调用flush();然后close();

+0

+1。在这种情况下(CipherStream),它需要是'close()'。否则数据将不完整。 – Thilo 2013-05-06 00:36:27

+0

谢谢!在对象的outputStream上调用'flush()',然后'close()'做到了诀窍!我假设它递归地关闭所有其他流? – rthur 2013-05-06 08:25:41

+0

java中的流遵循装饰模式。在最外层流上进行的调用通过在每层停止的组合对象结构向下传播,以完成装饰器可能需要为该调用执行的任何操作,然后结束到最低层。 – cmbaxter 2013-05-06 10:45:50