Java通过套接字发送加密文件

发布于 2024-11-08 00:31:26 字数 1315 浏览 0 评论 0原文

我一直在尝试编写一个小型文件服务器。我已经达到了文件传输正常的地步,但现在我尝试添加加密,奇怪的事情发生了。我正在尝试使用密码输入/输出流来使用 DES 加密发送文件。该文件似乎完全由服务器传输,但我无法让客户端正确接收它。

无论我传输哪种类型的文件,客户端都不会离开我用来接收文件的循环。即便如此,我还是成功地收到了 .pdf 和 .doc 文件,这两个文件似乎都没有任何错误,并且可以完美打开。但当我发送图像时,结局似乎没有顺利完成。图像打开,但末端从未显示,只是显示一个灰色区域。

我认为这些问题是相关的,但我不知道如何解决它们。

这是我用来在服务器端发送文件的代码:

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
CipherOutputStream cipherOut = new CipherOutputStream(outToClient, cipher);
byte[] fileBuffer = new byte[BUFFER_SIZE];
InputStream fileReader = new BufferedInputStream(new FileInputStream(aFile));
int bytesRead;
while((bytesRead = fileReader.read(fileBuffer)) != EOF){
    cipherOut.write(fileBuffer, 0, bytesRead);
}
cipherOut.flush();

以及在客户端接收文件的代码:

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, serverPublicKey);
CipherInputStream cipherIn = new CipherInputStream(inFromServer, cipher);

byte[] fileBuffer = new byte[BUFFER_SIZE];
FileOutputStream fileWriter = new FileOutputStream(newFileName);
int bytesRead;
while((bytesRead = cipherIn.read(fileBuffer)) != EOF){
    fileWriter.write(fileBuffer, 0, bytesRead);
}
fileWriter.flush();
fileWriter.close();

任何指向正确方向的指针都是超级的。

I've been trying to write a small file server. I got it to the point where a file transfer's just fine, but now that I've tried to add encryption strange things are happening. I'm trying to use cipher input/output streams to send the file using DES encryption. The file seems to be transferred completely by the server, but I can't get the client to receive it properly.

No matter what sort of file I transfer, the client never leaves the loop I'm using to receive the file. Even so, I've managed to receive .pdf and .doc files, neither of which seem to have any errors, and open perfectly. When I send an image though, the end doesn't seem to go through properly. The image opens, but the end never displays, just a greyed-out area instead.

I figure these issues are related, but I'm at a loss how to fix them.

Here's the code I'm using to send the file on the server side:

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
CipherOutputStream cipherOut = new CipherOutputStream(outToClient, cipher);
byte[] fileBuffer = new byte[BUFFER_SIZE];
InputStream fileReader = new BufferedInputStream(new FileInputStream(aFile));
int bytesRead;
while((bytesRead = fileReader.read(fileBuffer)) != EOF){
    cipherOut.write(fileBuffer, 0, bytesRead);
}
cipherOut.flush();

And the code to receive it on the client side:

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, serverPublicKey);
CipherInputStream cipherIn = new CipherInputStream(inFromServer, cipher);

byte[] fileBuffer = new byte[BUFFER_SIZE];
FileOutputStream fileWriter = new FileOutputStream(newFileName);
int bytesRead;
while((bytesRead = cipherIn.read(fileBuffer)) != EOF){
    fileWriter.write(fileBuffer, 0, bytesRead);
}
fileWriter.flush();
fileWriter.close();

Any pointers in the right direction would be super.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻许诺言 2024-11-15 00:31:26
  while((bytesRead = cipherIn.read(fileBuffer)) != EOF){

只是继续读取,直到“bytesRead”的数量给你 EOF,但这不会发生,因为你没有关闭另一端的套接字(至少我不能看到你的代码)。

我明白了

cipherOut.flush() ;

,但这不会关闭套接字。如果它只是“超出范围”,则在垃圾收集器回收该对象之前它不会被关闭。

  while((bytesRead = cipherIn.read(fileBuffer)) != EOF){

Is just going to keep reading until the number of 'bytesRead' gives you EOF, but that's not going to happen because you're not closing the socket(at least not that I can see if your code) on the other end.

I see

cipherOut.flush() ;

but that's not going to close the socket. If it's just 'falling out of scope' it won't be closed until the garbage collector reaps the object.

春花秋月 2024-11-15 00:31:26

您不使用块密码关闭 CipherOutputStream 服务器端

刷新可能会导致某些字节无法发送,直到块被填充或执行关闭,这将导致填充生效并允许接收者找到 EOF

you don't close the CipherOutputStream server side

with block cyphers a flush might cause some bytes not to be send until the block is filled or a close is executed which will cause the padding to come in effect and will allow the receiver to find the EOF

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文