Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SSL套接字-Java和证书?_Java_Android_Sockets_Ssl - Fatal编程技术网

SSL套接字-Java和证书?

SSL套接字-Java和证书?,java,android,sockets,ssl,Java,Android,Sockets,Ssl,我正在尝试在Android应用程序和Python API之间实现SSL套接字 下面的代码 SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault(); Socket s = ssf.createSocket("10.0.2.2", 5001); DataOutputStream myDataOut = new DataOutputStream(s.getOutputStream()); myDataOut.wri

我正在尝试在Android应用程序和Python API之间实现SSL套接字

下面的代码

SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket s = ssf.createSocket("10.0.2.2", 5001);
DataOutputStream myDataOut = new DataOutputStream(s.getOutputStream());
myDataOut.writeUTF("Hello Server");
myDataOut.flush();
myDataOut.close();
s.close();
正如您所看到的,它不起作用:

590.0750 - Establishing connection to ('127.0.0.1', 50888)
590.0970 - Error while connecting
590.0970 - Error information: [SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN] sslv3 alert certificate unknown (_ssl.c:590)
我认为没有,因为我没有指定证书。 请参见客户端的Python工作示例:

import socket, ssl, pprint

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

ssl_sock = ssl.wrap_socket(s,
                           ca_certs="server.crt",
                           cert_reqs=ssl.CERT_REQUIRED)

ssl_sock.connect(('localhost', 5001))

print repr(ssl_sock.getpeername())
print ssl_sock.cipher()
print pprint.pformat(ssl_sock.getpeercert())

ssl_sock.write("Hello from the client...")

如何像在Python中那样在Java中指定证书?

假设我正确理解您的意思,您可能会对这个相关问题感兴趣。它描述了指定认证的方法。

假设我对您的理解是正确的,那么您可能会对这个相关问题感兴趣。它描述了指定证书的方法。

您可以使用jvm参数-Djavax.net.debug=ssl*在Java中调试ssl问题。所以您可以看到客户端和服务器之间发生了什么。在您的情况下,我认为您的服务具有*localhost的证书,并且在您的java程序中使用ip。如果在java中使用ip和证书,请小心,因为必须将主机名用作CN。将ip地址作为DNS别名放在ssl v3扩展中。您可以使用jvm参数-Djavax.net.debug=ssl*,在Java中调试ssl问题。所以您可以看到客户端和服务器之间发生了什么。在您的情况下,我认为您的服务具有*localhost的证书,并且在您的java程序中使用ip。如果在java中使用ip和证书,请小心,因为必须将主机名用作CN。将ip地址作为DNS别名放入ssl v3扩展中。