0
我正在与Cipher
在底层socket
和dataoutputstream
之间进行连接。现在根据CipherOutputStream
的说明文件,为了让最后一个块加密掉,我们需要调用close()
。所以在try-with-resource中使用它也会关闭底层的流,我不想因为我需要它们进行进一步的操作。
那么是否有任何方法或实施,这将有助于我在没有dataoutputstream
和socket
的情况下关闭cipheroutputstream
。我听说FilterOutputStream
,但我不知道如何与他们合作。
我的代码:关闭没有DataOutputStream和套接字的CipherOutputStream
public class ClientHandler implements Runnable {
Socket msock;
DataInputStream dis;
DataOutputStream dos;
String ip;
private miniClientHandler(Socket msock) {
this.msock = msock;
}
@Override
public void run() {
try {
dis = new DataInputStream(msock.getInputStream());
dos = new DataOutputStream(msock.getOutputStream());
ip = msock.getInetAddress().getHostAddress();
String msg = dis.readLine();
System.out.println(msg);
if (msg.equals("Download")) {
String file2dl = dis.readLine(); //2
File file = new File(sharedDirectory.toString() + "\\" + file2dl);
dos.writeLong(file.length()); //3+
//AES-128 bit key initialization.
byte[] keyvalue = "AES128BitPasswd".getBytes();
SecretKey key = new SecretKeySpec(keyvalue, "AES");
//Initialize the Cipher.
Cipher encCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
encCipher.init(Cipher.ENCRYPT_MODE, key);
//Get the IV from cipher.
IvParameterSpec spec = null;
try {
spec = encCipher.getParameters().getParameterSpec(IvParameterSpec.class);
} catch (InvalidParameterSpecException ex) {
Logger.getLogger(PeersController.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] iv = spec.getIV();
dos.write(iv, 0, iv.length); //4+
//Encryption Mechanism.
try (FileInputStream fis = new FileInputStream(file)) {
try (CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
int read;
byte[] buffer = new byte[1024 * 1024];
while ((read = fis.read(buffer)) != -1) {
cos.write(buffer, 0, read); //5+ due to dos as underlying parameter of cos.
}
}
}
String message = dis.readLine();
System.out.println(message);
if (message.equals("Fetching Done")) {
System.out.println("Fetching Done!");
} else if (message.equals("Fetching Drop")) {
System.out.println("Fetching Denied!");
}
}
} catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
System.out.println(e.getMessage());
}
}
}
现在,因为COS被包裹在试穿与资源,当while循环结束,它会关闭流和潜在的dos
流已进一步包裹msock
导致整个套接字关闭,剩下的代码没用了。
其余代码:
String message = dis.readLine();
System.out.println(message);
if (message.equals("Fetching Done")) {
System.out.println("Fetching Done!");
} else if (message.equals("Fetching Drop")) {
System.out.println("Fetching Denied!");
}
你必须运行你自己的'Cipher'对象,然后调用'更新()'和'doFinal()'就可以了,而不是使用密码输出流,但你为什么不使用SSL? – EJP
@EJP只是在正常的大学项目上工作。而且,到现在为止,还没有任何关于SSL的经验。无论如何,我会研究它。你能否提供一些片段实施,以便我可以将其标记为最终答案。 – androizer