从套接字创建密码流时,应用程序死锁 [英] Application deadlocks when creating cipher streams from socket

查看:84
本文介绍了从套接字创建密码流时,应用程序死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在加密和解密两个套接字之间的流时遇到问题.

I'm having problems encrypting and decrypting my streams between two sockets.

ObjectInputStream oIn = new ObjectInputStream(new FileInputStream(new File("key")));
SecretKeySpec spec = (SecretKeySpec) oIn.readObject();
//'key' file was saved previously


Cipher cEncrypt = Cipher.getInstance("AES");
cEncrypt.init(Cipher.ENCRYPT_MODE, spec); 
Cipher cDecrypt = Cipher.getInstance("AES");
cDecrypt.init(Cipher.DECRYPT_MODE, spec); 
//should have no problems here, I tried the ciphers out by encoding and decoding a String, works fine

ObjectOutputStream objectOutputStream= new ObjectOutputStream(new CipherOutputStream(socket.getOutputStream,cEncrypt)); 
objectOutputStream.flush(); 
ObjectInputStream objectInputStream = new ObjectInputStream(new CipherInputStream(socket.getInputStream,cDecrypt));

然后,程序停止.插座两侧的代码相同.在不使用加密流的情况下,该程序可以很好地传输数据.

Then, the program stops. The code is the same on both sides of the socket. Without using ciphered streams, the program transfers data fine.

非常感谢您的帮助!

推荐答案

请参见

由封装的密码缓冲并等待其处理的任何字节都不会被写出.例如,如果封装的密码是块密码,并且使用一种写入方法写入的字节总数小于密码的块大小,则不会写出任何字节.

Any bytes buffered by the encapsulated cipher and waiting to be processed by it will not be written out. For example, if the encapsulated cipher is a block cipher, and the total number of bytes written using one of the write methods is less than the cipher's block size, no bytes will be written out.

基本上,由于使用的是ECB或CBC之类的 block 密码,因此只能以16个字节的块的形式写出数据.您可以使用 stream 密码来避免此问题.有关详细信息,请参见分组密码操作模式.您需要选择CFB,OFB或CTR.例如.当您获得Cipher实例时:

Basically your data can only be written out in chunks of 16 bytes because you're using a block cipher like ECB or CBC. You can avoid this problem by using a stream cipher. See block ciphers modes of operation for details. You'll need to select CFB, OFB or CTR. E.g. when you get your Cipher instance:

Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding");

这篇关于从套接字创建密码流时,应用程序死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆