java服务器和android客户端之间的SSL连接失败[英] SSL connection failure between java server and android client

问题描述

我正在尝试在Java主机和Android客户端之间设置相互身份验证SSL连接.不知道为什么它没有连接.以下是Android客户端应用程序和Java Server的代码.

客户端代码:

private SSLContext createSSLContext(final Context cont){
    SSLContext ssl_cont = null;
    try {
        Log.d(TAG, "TrustStore - Initializing");   
        KeyStore trustStore = KeyStore.getInstance("BKS");
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        InputStream trustStoreStream = cont.getResources().openRawResource(R.raw.myclienttruststore);
        trustStore.load(trustStoreStream, "client".toCharArray());
        trustManagerFactory.init(trustStore);
        Log.d(TAG, "TrustStore - Initialized");

        // Setup keystore
        Log.d(TAG, "KeyStore - Initializing");
        KeyStore keyStore = KeyStore.getInstance("BKS");
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        InputStream keyStoreStream = cont.getResources().openRawResource(R.raw.myclient);
        keyStore.load(keyStoreStream, "client".toCharArray());
        keyManagerFactory.init(keyStore, "client".toCharArray());
        Log.d(TAG, "KeyStore - Initialized");

        ssl_cont = SSLContext.getInstance("TLS");
        ssl_cont.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); 
    } catch (Exception e) {
        // TODO Auto-generated catch block
        alertbox("SSLClient", "ERROR: " + e.getMessage());
        Log.d(TAG, "ERROR: " + e.getMessage());
    }
    return ssl_cont;
}

OnClickListener onConnClick = new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        try {
            // Setup the SSL context to use the truststore and keystore
            Log.d(TAG, "Started..");
            SSLContext ssl_context = createSSLContext(cont);
            Log.d(TAG,"here 1...");
            SSLSocketFactory socketFactory = (SSLSocketFactory) ssl_context.getSocketFactory();
            Log.d(TAG,"here 2...");
            socket = (SSLSocket) socketFactory.createSocket(ipadd.getText().toString().trim(), Integer.parseInt(port.getText().toString().trim()));
            Log.d(TAG,"here 3...");
            dataOut = new DataOutputStream(socket.getOutputStream());
            dataIn = new DataInputStream(socket.getInputStream());
            dataOut.writeUTF("Hello !!");
            msgin.setText("Connected");
            Log.d(TAG, "Completed..");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            msgin.setText("Not connected");
            alertbox("Main", "ERROR: " + e.getMessage());
            Log.d(TAG, "ERROR: " + e.getMessage());
        }
    }
};

服务器代码:

    try {
        mySSLServerFac = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
        mySSLServerSocket = (SSLServerSocket) mySSLServerFac.createServerSocket(9999);
        System.out.println("Listening on 9999\n");
        mySSLSocket = (SSLSocket) mySSLServerSocket.accept();           
        DataInputStream input = new DataInputStream(mySSLSocket.getInputStream());
        DataOutputStream output = new DataOutputStream(mySSLSocket.getOutputStream());      
        do{
            System.out.println("Remote IP Address : " + mySSLSocket.getInetAddress());
            msg = input.readUTF().toString();
            System.out.println(msg);
            java.util.Scanner sc = new java.util.Scanner(System.in);
            output.writeUTF(sc.nextLine());
        }while(msg != "exit");
        System.out.println(msg);                
    } catch (Exception e) {
        e.printStackTrace();
    }

我在服务器上用"没有公共套件"错误.由于我无处可在SSL连接设置中.如果您发现错误或重大问题,请让我帮助.

这里是链接我遵循创建证书和truststore.我创建的信任库和kestore是

我正在使用Android 2.2和BKSProvider 1.46,请告诉我在哪里出错.我必须尽快结束这个项目.

提前感谢.

推荐答案

它已经解决了!问题在于Java主机的信任库,遵循这个帖子.

需要为客户端/服务器指定TrustStore,因为它们使用默认的信任库,导致失败.使用-djavax.net.ssl.truststore = servertruststore.jks -djavax.net.ssl.truststorepassword =服务器上的服务器,并在客户端创建自己的密钥库和信任库允许会话完成.它是-djavax.net.debug = ssl,握手,帮助很多.

整个命令是:java -djavax.net.ssl.keystore = server.jks -djavax.net.ssl.keystorepassword = server -djavax.net.ssl.truststore = servertruststore.jks -djavax.net.ssl. truststorepassword = server sslserver

现在我正在创建sslsession和多线程编程.

其他推荐答案

从堆栈跟踪看起来您捕获的异常不包含消息.

Log.d(TAG, e.getMessage());

它与SSL无关.

本文地址:https://www.itbaoku.cn/post/978636.html

相关标签/搜索