前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android 安装p12证书,如何在Android中使用p12证书(客户端证书)

android 安装p12证书,如何在Android中使用p12证书(客户端证书)

作者头像
全栈程序员站长
发布2022-09-13 15:51:33
2.6K0
发布2022-09-13 15:51:33
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

我试图在android中使用客户端证书。 我得到了一个.p12文件,我想用它来对服务器进行身份验证。如何在Android中使用p12证书(客户端证书)

我正在使用portecle将.p12文件转换为.bks文件,但我似乎没有得到它的工作。

下面的代码:

package com.pa1406.SECURE;

import java.io.InputStream;

import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.TrustManagerFactory;

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 android.content.Context;

public class HttpsClient extends DefaultHttpClient {

final Context context;

public HttpsClient(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 truststore = KeyStore.getInstance(“BKS”);

InputStream in = context.getResources().openRawResource(R.raw.keystore);

try {

truststore.load(in, “qwerty1234”.toCharArray());

} finally {

in.close();

}

return new SSLSocketFactory(truststore);

} catch (Exception e) {

throw new AssertionError(e);

}

}

}

我能做些什么来实现这一目标?

UPDATE:

package com.pa1406.SECURE;

import java.io.InputStream;

import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManagerFactory;

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 android.content.Context;

public class HttpsClient extends DefaultHttpClient {

final Context context;

public HttpsClient(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 {

// setup truststore to provide trust for the server certificate

// load truststore certificate

InputStream clientTruststoreIs = context.getResources().openRawResource(R.raw.truststore);

KeyStore trustStore = null;

trustStore = KeyStore.getInstance(“BKS”);

trustStore.load(clientTruststoreIs, “qwerty1234”.toCharArray());

System.out.println(“Loaded server certificates: ” + trustStore.size());

// initialize trust manager factory with the read truststore

TrustManagerFactory trustManagerFactory = null;

trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

trustManagerFactory.init(trustStore);

// setup client certificate

// load client certificate

InputStream keyStoreStream = context.getResources().openRawResource(R.raw.torbix);

KeyStore keyStore = null;

keyStore = KeyStore.getInstance(“BKS”);

keyStore.load(keyStoreStream, “qwerty1234”.toCharArray());

System.out.println(“Loaded client certificates: ” + keyStore.size());

// initialize key manager factory with the read client certificate

KeyManagerFactory keyManagerFactory = null;

keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

keyManagerFactory.init(keyStore, “qwerty1234”.toCharArray());

// initialize SSLSocketFactory to use the certificates

SSLSocketFactory socketFactory = null;

socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS, keyStore, “qwerty1234”,

trustStore, null, null);

return socketFactory;

} catch (Exception e) {

throw new AssertionError(e);

}

}

}

2011-11-29

Bewn

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/148865.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
SSL 证书
腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档