java 如何进行ssl socket编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6786945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to do ssl socket programming
提问by Qaiser Mehmood
I am doing socket communication through follwing IP address it working but no i want to do communication in ssl mode but how can I change InetAddress serverAddr = InetAddress.getByName("192.168.1.2");
to SSL.
我正在通过以下 IP 地址进行套接字通信,但我不想在 ssl 模式下进行通信,但如何更改InetAddress serverAddr = InetAddress.getByName("192.168.1.2");
为 SSL。
public class TCPClient implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName("192.168.1.2");
Log.d("TCP", "C: Connecting...");
Socket socket = new Socket(serverAddr,12345);
String message = "Hello from Client android emulator";
try {
Log.d("TCP", "C: Sending: '" + message + "'");
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(message);
Log.d("TCP", "C: Sent.");
Log.d("TCP", "C: Done.");
} catch(Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
}
回答by zacheusz
Create SSLSocketinstead of Socket. Rest is the same.
创建SSLSocket而不是 Socket。休息是一样的。
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("192.168.1.2", 12345);
You may want to add aditional SSL properties. You have to do it ealier:
您可能想要添加其他 SSL 属性。你必须更早地做到:
To authenticate the server, the client's trust store must contain the server's certificate. Client SSL with server authentication is enabled by the URL attribute ssl or the property ssl set to peerAuthentication. In addition, the system properties javax.net.ssl.trustStore and javax.net.ssl.trustStorePassword need to be set.:
要对服务器进行身份验证,客户端的信任库必须包含服务器的证书。带有服务器身份验证的客户端 SSL 由 URL 属性 ssl 或设置为 peerAuthentication 的属性 ssl 启用。另外需要设置系统属性javax.net.ssl.trustStore和javax.net.ssl.trustStorePassword:
System.setProperty("javax.net.ssl.trustStore","clientTrustStore.key");
System.setProperty("javax.net.ssl.trustStorePassword","qwerty");
If the server does client authentication, the client will need a key pair and a client certificate:
如果服务器进行客户端身份验证,客户端将需要一个密钥对和一个客户端证书:
System.setProperty("javax.net.ssl.keyStore","clientKeyStore.key");
System.setProperty("javax.net.ssl.keyStorePassword","qwerty");
回答by PixelsTech
Basically you need to use SSLSocket which is for SSL communication in Java.
基本上你需要使用 SSLSocket,它用于 Java 中的 SSL 通信。
When creating the SSLSocket, you first need to configure the trust store which is to verify the server certificate.
创建 SSLSocket 时,首先需要配置信任库,用于验证服务器证书。
Then you need to get the SSLSocket and connect to the server and then start to do handshake with the server.
然后你需要获取 SSLSocket 并连接到服务器,然后开始与服务器进行握手。
Once the handshake complete successfully, you can start to exchange application data with server normally like other plain socket connection.
一旦握手成功完成,您就可以开始像其他普通套接字连接一样正常地与服务器交换应用程序数据。
A HTTPS client and HTTPS server demo in Javaprovides a demo on how to create SSL server and SSL client in Java. It's quite useful.
Java 中的 HTTPS 客户端和 HTTPS 服务器演示提供了有关如何在 Java中创建 SSL 服务器和 SSL 客户端的演示。它非常有用。
回答by S.L. Barth - Reinstate Monica
Java has the SSLSocket class.
Java 有 SSLSocket 类。
http://download.oracle.com/javase/1.4.2/docs/api/javax/net/ssl/SSLSocket.html
http://download.oracle.com/javase/1.4.2/docs/api/javax/net/ssl/SSLSocket.html
Hope this helps, haven't used it myself (yet).
希望这会有所帮助,我自己还没有使用过(还没有)。