简体   繁体   English

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

[英]Java Client/Server SSL Socket Chat

Ok pals, so I have created a java chat using TCP client/server socket. 好的,我使用TCP客户端/服务器套接字创建了Java聊天室。 I create the server, and then i create clients that connect to the server so that they can communicate between each other through the server! 我创建服务器,然后创建连接到服务器的客户端,以便它们可以通过服务器相互通信! The code: Server: 代码:服务器:

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 

    }
}

And the client: 和客户:

 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!`

So now, I want to use SSL protection(so i can see the changes in wireshark). 所以现在,我想使用SSL保护(这样我就可以看到Wireshark中的更改)。 I used OpenSSL(it is required for my assingment) to create a root CA and device certificates.I put the files on the src folder on netbeans, and copied the code to two new classes, SSLServer and SSLClient, and experimented a bit on the SSL part! 我使用OpenSSL(这是我的要求)来创建根CA和设备证书。我将文件放在netbeans的src文件夹中,然后将代码复制到两个新类SSLServer和SSLClient,然后在SSL部分! So: 所以:

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();                
}
}

Same thing for the client. 对客户来说也是一样。 So I am a bit stuck on the SSLContext part! 所以我在SSLContext方面有些卡住! I read many threads here but still.. Is the part below SSLContext correct? 我在这里读了很多线程,但是仍然.. SSLContext下面的部分正确吗? And how do I use the certificates in SSLContext? 以及如何在SSLContext中使用证书? EDIT: Maybe this will work? 编辑:也许这将工作? :

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM