Android:将密钥库作为资产打开以生成SSLSocketFactory

我有一个Bouncy Castle密钥库,我想用它连接到SSLSocketFactory.
在“桌面”Java中执行此操作很简单,但是如何在android上执行此操作.

你输入资产还是res / raw似乎没什么区别 – 试图打开它并在实例化KeyStore实例(在这种情况下是java.security.KeyStore)传递给SSLSocketFactorys构造函数时出现问题.

以前有人有过这方面的成功吗?什么是“阅读”并打开它的最佳方式?任何指针或代码片段都是最受欢迎的.

非常感谢

解决方法:

这应该做:

import android.content.Context;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;

import java.io.InputStream;
import java.security.KeyStore;

public class MyHttpClient extends DefaultHttpClient {

  final Context context;

  public MyHttpClient(Context context) {
    this.context = context;
  }

  @Override protected ClientConnectionManager createClientConnectionManager() {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(
        new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme("https", newSslSocketFactory(), 443));
    return new SingleClientConnManager(getParams(), registry);
  }

  private SSLSocketFactory newSslSocketFactory() {
    try {
      KeyStore trusted = KeyStore.getInstance("BKS");
      InputStream in = context.getResources().openRawResource(R.raw.mystore);
      try {
        trusted.load(in, "ez24get".toCharArray());
      } finally {
        in.close();
      }
      return new SSLSocketFactory(trusted);
    } catch (Exception e) {
      throw new AssertionError(e);
    }
  }
}

ez24get将是密钥库的密码.

上一篇:Java – javax.net.ssl.SSLPeerUnverifiedException:peer未经过身份验证


下一篇:android – 在不同的计算机上用密钥库登录apk