gpt4 book ai didi

Java 客户端/服务器 SSL 套接字聊天

转载 作者:行者123 更新时间:2023-12-01 12:25:03 24 4
gpt4 key购买 nike

好的, friend 们,我已经使用 TCP 客户端/服务器套接字创建了一个 java 聊天。我创建服务器,然后创建连接到服务器的客户端,以便它们可以通过服务器相互通信!代码: 服务器:

public ServerChat() throws Exception{
ServerSocket soc=new ServerSocket(5217);


while(true)
{
Socket CSoc=soc.accept();
//and then the code for handling the messages, we don't need that

}
}

客户:

 Server soc=new Socket("127.0.0.1",5217);
Thread t=new Thread(this);
t.start();
//i Have ommited many parts of the code, but you know, i focus on the connection part of the problem!`

所以现在,我想使用SSL保护(这样我就可以看到wireshark中的变化)。我使用 OpenSSL(这是我的分配所必需的)来创建根 CA 和设备证书。我将文件放在 netbeans 上的 src 文件夹中,并将代码复制到两个新类 SSLServer 和 SSLClient,并在SSL 部分!所以:

public SSLServer() throws Exception{
SSLContext sslContext=????//supposed to add the files somehow here?
SSLServerSocketFactory factory=(SSLServerSocketFactory)slContext.getServerSocketFactory();
SSLServerSocket sslserversocket=(SSLServerSocket) factory.createServerSocket(1234);
while(true)
{
SSLSocket sslsocket=(SSLSocket)sslserversocket.accept();
}
}

对于客户来说也是同样的事情。所以我有点卡在 SSLContext 部分!我在这里读了很多主题,但仍然..SSLContext 下面的部分正确吗?如何在 SSLContext 中使用证书?编辑:也许这会起作用? :

KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("keystoreFile"), "keystorePassword".toCharArray());

KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
kmf.init(ks, "keystorePassword".toCharArray());

TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
tmf.init(ks);

SSLContext sc = SSLContext.getInstance("TLS");
TrustManager[] trustManagers = tmf.getTrustManagers();
sc.init(kmf.getKeyManagers(), trustManagers, null);

SSLServerSocketFactory ssf = sc.getServerSocketFactory();
SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(serverport);
SSLSocket c = (SSLSocket) s.accept();

最佳答案

无论如何,我发现了一些对我有帮助的链接!如果有人感兴趣,

importing an existing x509 certificate and private key in Java keystore to use in ssl

http://www.agentbob.info/agentbob/79-AB.html

关于Java 客户端/服务器 SSL 套接字聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26430425/

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