我在桌面java应用程序上有一个成功工作的服务器套接字
System.setProperty("javax.net.ssl.keyStoreType","PKCS12");
System.setProperty("javax.net.ssl.keyStore", PATH_TO_PKCS12);
System.setProperty("javax.net.ssl.keyStorePassword","password");
SSLServerSocketFactory sslServerSocketFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
mListenSocket=(SSLServerSocket)sslServerSocketFactory.createServerSocket(1240);
成功地与android上的客户端socket通信
InputStream certificate = mContext.getResources().openRawResource(R.raw.PKCS12_CERTIFICATE);
String tfmAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
KeyStore trustStore = KeyStore.getInstance("PKCS12");
trustStore.load(certificate, "password".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tfmAlgorithm);
tmf.init(trustStore);
SSLContext slc = SSLContext.getInstance("SSL");
slc.init(null,tmf.getTrustManagers(),new SecureRandom());
SSLSocketFactory fs = slc.getSocketFactory();
mSocket = fs.createSocket();
mSocket.connect(new InetSocketAddress(mIp, 1240), 10000);
问题
到目前为止,还没有问题。现在我需要将桌面java服务器套接字安装到android应用程序中。我所做的是:
ServerSocket mListenSocket;
mHost = "127.0.0.1";
mHostPort = 1241;
InputStream certificate = MainActivity.context.getResources().openRawResource(R.raw.PKCS12_CERTIFICATE);
String tfmAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
KeyStore trustStore = KeyStore.getInstance("PKCS12");
trustStore.load(certificate, "password".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tfmAlgorithm);
tmf.init(trustStore);
SSLContext slc = SSLContext.getInstance("SSL");
slc.init(null,tmf.getTrustManagers(),new SecureRandom());
SSLServerSocketFactory factory = slc.getServerSocketFactory();
mListenSocket = factory.createServerSocket(port);
处理流
Socket mSocket = mListenSocket.accept();
InputStream is;
is = mSocket.getInputStream();
我可以从我的android应用程序连接这个新的android服务器。我可以接收包,但是当我从套接字获取流时(这一行是=msocket.getinputstream();),我握手出错了
javax.net.ssl.SSLHandshakeException: Handshake failed
.pkcs12文件相同。“密码“是一样的。在android上创建ssl服务器套接字时是否遗漏了什么?
暂无答案!
目前还没有任何答案,快来回答吧!