我想在服务器和客户端之间传输一个大文件,我知道如何做到这一点:对于客户端,代码如下:
OutputStream output = sk.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[1024*1024];
int bytesRead = 0;
while((bytesRead = fileInputStream.read(buffer))>0)
{
output.write(buffer,0,bytesRead);
}
fileInputStream.close();
服务器端:
InputStream input = sk.getInputStream();
file = new File(filename);
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024*1024];
int bytesReceived = 0;
while((bytesReceived = input.read(buffer))>0) {
out.write(buffer,0,bytesReceived);
System.out.println(bytesReceived);
break;
}
现在的问题是,服务器和客户端都同意使用128位AES密钥,所以我想充分利用会话密钥来保证文件传输过程的安全。我的问题是,我可以在哪里插入AES加密过程在我的客户端和服务器?谢谢你!
我认为您可以使用javax.crypto.CipherInputStream/javax.crypto.CipherOutputStream。使用示例http://www.flexiprovider.de/examples/ExampleCrypt.html