简体   繁体   English

通过套接字Java的CipherOutputStream

[英]CipherOutputStream over a socket java

I have this code to send encryted data over a network: 我有以下代码可以通过网络发送加密的数据:

s = new Socket(serverAddress, serverPort);
is = s.getInputStream();
os = s.getOutputStream();

Cipher decryptCipher = Cipher.getInstance("RSA");
decryptCipher.init(Cipher.DECRYPT_MODE, ClientSocket.clientPrivateKey);
cis = new CipherInputStream(is,decryptCipher);

Cipher encryptCipher = Cipher.getInstance("RSA");
encryptCipher.init(Cipher.ENCRYPT_MODE, this.serverPublicKey);
cos = new CipherOutputStream(os,encryptCipher);

This code works, but when I try to use CipherOutputStream to send encrypted data over the network, the data is not sent until I call cos.close() , but if I close the stream I close the network connection. 该代码有效,但是当我尝试使用CipherOutputStream通过网络发送加密的数据时,直到调用cos.close()才发送数据,但是如果我关闭流,则会关闭网络连接。 What is the proper process for sending encrypted data with CipherOutputStream ? 使用CipherOutputStream发送加密数据的正确过程是什么?

The way I interpret the code is that the Cipher is initialized to encrypt one message with RSAES-PKCS1-v1_5 , because according to http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher "RSA" refers to "The RSA encryption algorithm as defined in PKCS #1" which I guess refers the oldest implementation with a padding scheme and that should be RSAES-PKCS1-v1_5 . 我解释代码的方式是,根据http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames ,密码被初始化为使用RSAES-PKCS1-v1_5加密一条消息。 html#Cipher “ RSA”指的是“ PKCS#1中定义的RSA加密算法”,我猜它指的是最古老的填充方案,应该是RSAES-PKCS1-v1_5 If that is correct, there is no way for the stream to produce partial results before the whole message (the whole stream) is read. 如果正确,则在读取整个消息(整个流)之前,流将无法产生部分结果。 Also you should not be able to send long messages with the cipher (with a 2048 bit RSA key that should be less than 256 bytes). 另外,您也不能使用密码发送长消息(2048位RSA密钥应小于256个字节)。

I assume what you are trying to accomplish is to create a secure connection between two endpoints? 我假设您要完成的工作是在两个端点之间创建安全连接? If so then you should not bother with all that low level cryptography and create a TLS connection. 如果是这样,那么您就不必理会所有这些低级别的加密技术并创建TLS连接。 Even though it not trivial to set up it still is much more easier than to build a secure encrypted communication channel from scratch. 尽管设置起来并不容易,但比从头开始建立安全的加密通信通道要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM