如何通过普通Java Sockets实现(SSL/TLS)安全通信

发布于 2024-12-26 20:39:04 字数 139 浏览 4 评论 0原文

如何确保普通套接字通信的安全?我编写了一个只能连接到专用 SSL 端口(5223)(使用 SSLSocket)的 Jabber 客户端。正常的方法是连接到 Jabber 标准端口 5222 并请求 starttls。我怎样才能做到这一点?

How can I secure ordinary socket communication? I wrote a Jabber client that can only connect to dedicated SSL ports(5223) (using SSLSocket). The normal way is to connect to Jabber standard port 5222 and request starttls. How can I achieve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归途 2025-01-02 20:39:04

我不完全确定您在问什么,但您可以使用 SSLSocketFactory.createSocket() 方法。这是使用诸如 starttls 之类的东西“升级”套接字的一种方法。但这样做是很困难的。几乎可以肯定,您的 XMPP 库将提供对此类功能的高级访问。

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. This is one way to "upgrade" a socket using something like starttls. But this is doing things the hard way. Almost certainly your XMPP library will provide high-level access to this kind of functionality.

柒夜笙歌凉 2025-01-02 20:39:04

期望管理:我还没有在 Jabber 上尝试过这个。但它适用于 GMail 的 SMTP 服务器,所以也许...

以下是将套接字升级为 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);

Expectation Management: I haven't tried this with Jabber. But it works with GMail's SMTP server, so maybe...

Here's how to upgrade a socket to an SSL socket, where socket is the original (non-TLS) connection:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文