gpt4 book ai didi

java - 从 SSL 输入流读取 (Java)

转载 作者:行者123 更新时间:2023-12-01 10:55:19 28 4
gpt4 key购买 nike

我正在尝试使用经过身份验证的 SSL 连接和以下类中定义的许多控制消息在客户端和服务器之间编写一个简单的协议(protocol):

public class KeyExchangeProtcolMsgs {
public static final String reqStr = "#REQ_KM";
public static final String nonceStr = "#NONCE_END";
public static final String ackStr = "#ACK_KM";
}

client.java

        // Connect to the server
cSock = (SSLSocket) fact.createSocket(this.remoteHost, this.remotePort);
// Create the streams to send out data as well as read data
OutputStream out = cSock.getOutputStream();
InputStream in = cSock.getInputStream();
// Generate client nonce
byte[] clientNonceB = CryptographyUtils.generateRandomNumber();
// Send the nonce and the request for a key from the server
// First, send the keying material request to the server
System.out.println("[I] SSL client written " + KeyExchangeProtcolMsgs.reqStr);
out.write(CryptographyUtils.toByteArray(KeyExchangeProtcolMsgs.reqStr)); // <== Successfully written to the server
// Next, send the generated nonce (by the client)
System.out.println("[I] SSL client written nonce ");
System.out.println(new String(clientNonceB, "UTF-8"));
out.write(clientNonceB);
// Finally, send the ending string
System.out.println("[I] SSL client written " + KeyExchangeProtcolMsgs.nonceStr);
out.write(CryptographyUtils.toByteArray(KeyExchangeProtcolMsgs.nonceStr));
// Wait for the response from the server containing the key
int ch = 0;
String responseStr = "";
while ((responseStr.contains(KeyExchangeProtcolMsgs.ackStr) == false) && (responseStr.contains(KeyExchangeProtcolMsgs.nonceStr) == false)) {
ch = in.read();
responseStr = responseStr + (char) ch;
System.out.println("[I] SSL client read " + responseStr);
}
// Display read information from server
System.out.println("[I] SSL client read " + responseStr);
// Check if the server nonce contains the starting and end messages of the protocol

String serverNonceStr = responseStr.substring(KeyExchangeProtcolMsgs.ackStr.length(), responseStr.length() - KeyExchangeProtcolMsgs.nonceStr.length());
// Compute the key by xor-ing the client and server nonce and applying AES
// on the resulting string
clientKeyingMaterial = new SecretKeySpec(CryptographyUtils.xorStrings(clientNonceB, CryptographyUtils.toByteArray(serverNonceStr)), "AES");
return clientKeyingMaterial;

server.java

        System.out.println("[I] SSL server listening");

SSLSocket sslSock = (SSLSocket) sSock.accept();
sslSock.startHandshake();

System.out.println("[I] SSL server starting handshake");

// Process if principal checks out
if (isEndEntity(sslSock.getSession())) {
// Create the streams to send out data as well as read data
OutputStream out = sslSock.getOutputStream();
InputStream in = sslSock.getInputStream();
// Wait and read the client's nonce
int ch = 0;
String requestStr = "";
while ((requestStr.contains(KeyExchangeProtcolMsgs.reqStr) == false) && (requestStr.contains(KeyExchangeProtcolMsgs.nonceStr) == false)) {
ch = in.read();
requestStr = requestStr + (char) ch;
System.out.println("[I] SSL server received " + requestStr);
}

System.out.println("[I] SSL server received " + requestStr);
}

一旦发送 KeyExchangeProtcolMsgs.reqStr/#REQ_KM,服务器端的循环就会退出,但不会等待实际的随机数和结束消息 KeyExchangeProtcolMsgs.nonceStr/#NONCE_END .

为什么在客户端发送最后一条消息之前,服务器端的while循环就退出了?

最佳答案

因为一旦 requestStr 包含 KeyExchangeProtcolMsgs.reqStrrequestStr.contains(KeyExchangeProtcolMsgs.reqStr) 就变为 true,所以requestStr.contains(KeyExchangeProtcolMsgs.reqStr) == false变成了true == false,也就是false,所以整个测试在while 循环变为 false,因此执行会退出 while 循环。

有几种方法可以解决这个问题,最简单的方法是使用两个 while 循环。第一个循环直到 requestStrKeyExchangeProtcolMsgs.reqStr,第二个循环累积随机数直到以 KeyExchangeProtcolMsgs.nonceStr 结束。像这样的事情:

        String requestStr = "";
while (!requestStr.equals(KeyExchangeProtcolMsgs.reqStr)) {
ch = in.read();
requestStr = requestStr + (char) ch;
}

System.out.println("[I] SSL server received " + requestStr);

String nonceStr = "";
while (!requestStr.endsWith(KeyExchangeProtcolMsgs.nonceStr)) {
ch = in.read();
nonceStr = nonceStr + (char) ch;
}

// Whack #NONCE_END from the end to get just the nonce.

nonceStr = nonceStr.substring(0, nonceStr.length() - KeyExchangeProtcolMsgs.nonceStr.length());

System.out.println("[I] SSL server received " + nonceStr);

这尚未经过测试,但应该很接近。

您可以通过保留某种状态指示器,使用单个 while 循环来完成此操作,以便您知道何时累积 reqStr 以及何时累积 nonceStr,但我认为这样拆分更干净。

关于java - 从 SSL 输入流读取 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33637330/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com