如何使用密码正确初始化对象流

时间:2012-05-15 15:18:08

标签: java stream encryption

服务器

 decCipher.init(Cipher.DECCRYPT_MODE, secretKey);
     ois = new ObjectInputStream(new CipherInputStream(socket.getInputStream(),decCipher));
     encCipher.init(Cipher.ENCRYPT_MODE, secretKey);
     out = new ObjectOutputStream(new CipherOutputStream(socket.getOutputStream(),encCipher));

客户(不同的申请):

ë

ncCipher.init(Cipher.ENCRYPT_MODE, secretKey);
obj_out = newObjectOutputStream(new CipherOutputStream(socket.getOutputStream(),encCipher));
 decCipher.init(Cipher.DECRYPT_MODE, secretKey);
 obj_in = new ObjectInputStream(new CipherInputStream(socket.getInputStream(),decCipher)

服务器应用程序中的Ois在创建流时抛出异常。怎么解决?当没有包裹密码流时,一切正常。

异常不会在new ObjectInputStream创建时抛出,但会冻结10秒(超时),当达到超时限制时会引发异常。

例外:

at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2298)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2767)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:798)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)

1 个答案:

答案 0 :(得分:0)

您需要使用两个不同的Cipher个对象,一个用于加密,一个用于解密。在将Cipher放入CipherInputStream之后,考虑ENCRYPT_MODE附加到{{1}}对象的模式。