gpt4 book ai didi

java - 使用 SSL 和 Bouncy CaSTLe 进行 Android 到服务器的通信

转载 作者:搜寻专家 更新时间:2023-11-01 07:52:29 25 4
gpt4 key购买 nike

我知道这不是那么困难,但非常不幸的是,从昨天开始我就被困在这里并与之抗争,我遵循了这个 Mutual Authentication in Android 教程,将 keystore 放在资源中并尝试通过 SSL 连接到我的服务器,但是得到以下异常

java.lang.RuntimeException: org.spongycastle.jcajce.provider.asymmetric.x509.CertificateFactory$ExCertificateException

我已将我的 sslapptruststore.pfx 文件放在 res/raw/sslapptruststore.pfx 下并使用这段代码

try {

KeyStore clientCert = KeyStore.getInstance("PKCS12");
clientCert.load(getResources().openRawResource(R.raw.sslapptruststore), "123456".toCharArray());// this line causes exception

HttpClient httpClient = null;
HttpParams httpParams = new BasicHttpParams();
SSLSocketFactory sslSocketFactory = new SSLSocketFactory(clientCert, null, null);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", sslSocketFactory, 8443));
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, registry), httpParams);


HttpPost httpPost = new HttpPost(
"https://192.168.1.113:8443/CertProvider");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("csr", csr.toString()));

// Url Encoding the POST parameters
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));

// Making HTTP Request
// HttpResponse response = null;
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = "";
response = httpClient.execute(httpPost, responseHandler);
} catch (Exception e) {
Log.e("", e.getMessage());
}

我也搜索过,但其他人正在使用 .bks

感谢任何帮助。

最佳答案

我已经回答了一些与您的问题类似的问题,如下所示:

Read in PKCS12/P12 Client Cert file for Android App

Android volley self signed HTTPS trust anchor for certification path not found

你会发现

    private SSLSocketFactory getSSLSocketFactory_KeyStore(String keyStoreType, int keystoreResId, String keyPassword)
throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {

InputStream caInput = getResources().openRawResource(keystoreResId);

// creating a KeyStore containing trusted CAs

if (keyStoreType == null || keyStoreType.length() == 0) {
keyStoreType = KeyStore.getDefaultType();
}
KeyStore keyStore = KeyStore.getInstance(keyStoreType);

keyStore.load(caInput, keyPassword.toCharArray());

// creating a TrustManager that trusts the CAs in the KeyStore

String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers());

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, wrappedTrustManagers, null);

return sslContext.getSocketFactory();
}

getSSLSocketFactory_Certificate 获取 .cert 文件。

如上面第一个链接所示,在您的项目中您可以调用以下两种方法之一:

如果使用 keystore 文件:

SSLSocketFactory sslSocketFactory = getSSLSocketFactory_KeyStore("PKCS12", R.raw.androidpkcs12, "123456789");

如果使用证书文件:

SSLSocketFactory sslSocketFactory = getSSLSocketFactory_Certificate("PKCS12", R.raw.androidpkcs12_cert);

P/S:如果这些方法在非 Activity 类中,为避免 NPE,您必须将 Context 从您的 Activity 传递给该类(如在上面的第一个链接)。

希望这对您有所帮助!

关于java - 使用 SSL 和 Bouncy CaSTLe 进行 Android 到服务器的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32969952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com