我一直在试图写一个小文件服务器。我知道它的文件传输很好,但现在我试图添加加密奇怪的事情正在发生。我试图使用密码输入/输出流来使用DES加密来发送文件。该文件似乎完全由服务器传输,但我无法让客户端正确接收它。Java通过套接字发送加密文件
无论我传输什么类型的文件,客户端都不会离开我用来接收文件的循环。即使如此,我已经设法接收.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();
在右边的指针方向会超级。
正如一个附注,DES是一种**对称加密算法**,也就是说,在处理时没有**公钥**(或私钥)的概念DES。变量的名称“publicKey”和“serverPublicKey”具有误导性,因为它们实际上代表**共享密钥**。 – 2011-05-19 00:16:41
您不关闭输出流服务器端。是EOF -1或0它应该是-1 – 2011-05-19 00:26:10