简体   繁体   English

如何通过普通的Java套接字实现(SSL / TLS)安全通信

[英]How to implement (SSL/TLS) secure communication through normal Java Sockets

How can I secure ordinary socket communication? 如何保护普通的socket通信? I wrote a Jabber client that can only connect to dedicated SSL ports(5223) (using SSLSocket). 我写了一个Jabber客户端,只能连接到专用的SSL端口(5223)(使用SSLSocket)。 The normal way is to connect to Jabber standard port 5222 and request starttls . 通常的方法是连接到Jabber标准端口5222并请求starttls How can I achieve that? 我怎样才能做到这一点?

Expectation Management: I haven't tried this with Jabber. 期望管理:我没有用Jabber试过这个。 But it works with GMail's SMTP server, so maybe... 但它适用于GMail的SMTP服务器,所以也许......

Here's how to upgrade a socket to an SSL socket, where socket is the original (non-TLS) connection: 以下是如何将套接字升级到SSL套接字,其中socket是原始(非TLS)连接:

SSLSocket sslSocket = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(
                       socket, 
                       socket.getInetAddress().getHostAddress(), 
                       socket.getPort(), 
                       true);
InputStream inputStream = sslSocket.getInputStream();
OutputStream outputStream = sslSocket.getOutputStream();
// reads from the socket
Scanner scanner = new Scanner(inputStream);
// writes to the socket
OutputStream outputStream = new BufferedOutputStream(outputStream);

I'm not completely sure what you are asking, but you can layer a secure socket on an existing normal (non-secure) socket by using the SSLSocketFactory.createSocket() method. 我不完全确定你在问什么,但你可以使用SSLSocketFactory.createSocket()方法在现有的普通(非安全)套接字上分层安全套接字。 This is one way to "upgrade" a socket using something like starttls. 这是使用starttls之类的东西“升级”套接字的一种方法。 But this is doing things the hard way. 但这是艰难的做事。 Almost certainly your XMPP library will provide high-level access to this kind of functionality. 几乎可以肯定,您的XMPP库将提供对此类功能的高级访问。

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

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