SSL冠军的挑战

时间:2012-04-09 19:02:24

标签: android ssl openssl ssl-certificate

  

可能重复:
  SSL connection failure between java server and android client

如果你的人非常擅长SSL!你可以花一点宝贵的时间让我知道我在设置android和java服务器之间的ssl连接时出错了。我厌倦了这种连接设置。最终得到“错误版本的密钥库”不知道我哪里出错了。

我无法擅长SSL,但仍在努力。这是迄今为止大部分人未能回答的主要问题之一。请问有没有人有时间指导?在此链接http://blog.callistaenterprise.se/2011/11/24/creating-self-signed-certificates-for-use-on-android/

之后创建了BKS格式信任库和密钥库

我连续第三次提出这个问题,因为我没有得到回应。这就是为什么我把这篇文章作为对所有那些发现自己是ssl编码员的人的挑战。如果你太好了,请分享你的知识。

这是android代码:

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.clienttruststore);
        Log.d(TAG,"here...");
        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.client);
        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
        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);                
            SSLSocketFactory socketFactory = (SSLSocketFactory) ssl_context.getSocketFactory();
            socket = (SSLSocket) socketFactory.createSocket(ipadd.getText().toString().trim(), Integer.parseInt(port.getText().toString().trim()));
            dataOut = new DataOutputStream(socket.getOutputStream());
            dataIn = new DataInputStream(socket.getInputStream());
            msgin.setText("Connected");
            Log.d(TAG, "Completed..");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            msgin.setText("Not connected");
            Log.d(TAG, "ERROR: " + e.getMessage());
        }
    }
};

Java服务器部分:

    SSLServerSocketFactory mySSLServerFac = null;
    SSLServerSocket mySSLServerSocket = null;
    SSLSocket mySSLSocket = null;
    String msg = null;
    System.out.print("I'm here\n");
    try {
        mySSLServerFac = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
        mySSLServerSocket = (SSLServerSocket) mySSLServerFac.createServerSocket(9989);
        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());
            java.util.Scanner sc = new java.util.Scanner(System.in);
            output.writeUTF(sc.nextLine());
            msg = input.readUTF().toString();
            System.out.println(msg);
        }while(msg != "exit");
        System.out.println(msg);                
    } catch (Exception e) {
        e.printStackTrace();
    }

堆栈跟踪:

04-10 00:09:02.065: DEBUG/SSLActivity(276): Started..
04-10 00:09:02.065: DEBUG/SSLActivity(276): Trust store - Initializing
04-10 00:09:02.075: DEBUG/SSLActivity(276): here...
04-10 00:09:02.085: DEBUG/SSLActivity(276): ERROR: Wrong version of key store.
04-10 00:09:02.095: WARN/System.err(276): java.lang.NullPointerException
04-10 00:09:02.116: WARN/System.err(276):     at com.pkg.sslmsg.SSLMsgActivity$1.onClick(SSLMsgActivity.java:97)
04-10 00:09:02.116: WARN/System.err(276):     at android.view.View.performClick(View.java:2408)
04-10 00:09:02.125: WARN/System.err(276):     at android.view.View$PerformClick.run(View.java:8816)
04-10 00:09:02.135: WARN/System.err(276):     at android.os.Handler.handleCallback(Handler.java:587)
04-10 00:09:02.135: WARN/System.err(276):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 00:09:02.135: WARN/System.err(276):     at android.os.Looper.loop(Looper.java:123)
04-10 00:09:02.145: WARN/System.err(276):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-10 00:09:02.155: WARN/System.err(276):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 00:09:02.155: WARN/System.err(276):     at java.lang.reflect.Method.invoke(Method.java:521)
04-10 00:09:02.175: WARN/System.err(276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-10 00:09:02.175: WARN/System.err(276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-10 00:09:02.186: WARN/System.err(276):     at dalvik.system.NativeStart.main(Native Method)
04-10 00:09:02.196: DEBUG/SSLActivity(276): ERROR: null

将此视为请求,请指导我。我试过在整个地方寻找解决方案但最终只是失望。如果有人引导我,那将是很有帮助的!

在最后一次尝试中保持很多希望!如果你知道的话,请帮忙。虽然你可能觉得它很愚蠢。我只是一个学习者。

提前致谢。

0 个答案:

没有答案