Android BKS证书
Android加载bks格式证书,Ios/Pc加载cer格式证书,一般而言,生成cer格式比较常见,因此需要进行cer转bks操作,操作步骤如下:
首先要下载特定版本的JCE Provider包
http://www.bouncycastle.org/download/bcprov-jdk15on-146.jar
http://www.java2s.com/Code/Jar/b/Downloadbcprovjdk16146jar.htm
或者
http://pan.baidu.com/s/1c1ur13y
转换命令说明:
keytool -importcert -v -trustcacerts -alias 位置1 \
-file 位置2 \
-keystore 位置3 -storetype BKS \
-providerclass org.bouncycastle.jce.provider.BouncyCastleProvider \
-providerpath 位置4 -storepass 位置5
位置1:是个随便取的别名
位置2:cer或crt证书的全地址
位置3:生成后bks文件的位置,建议写全地址
位置4:上面下载JCE Provider包的位置
位置5:生成后证书的密码
转换完整示例
keytool -importcert -v -trustcacerts -alias my12306 -file srca.cer -keystore srca.bks -storetype BKS -providerclass org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath bcprov-jdk15on-146.jar -storepass 123456
Android BKS证书的使用
比如mqtt连接的时候,只需要给MqttConnectOptions设置上SocketFactory即可,
SocketFactory获取如下
public SSLSocketFactory getSSLSocketFactory() {
InputStream inputStream = null;
try {
SSLContext sslContext = null;
SSLSocketFactory sslSockFactory = null;
KeyStore keyStore;
keyStore = KeyStore.getInstance("BKS");
inputStream = EAApplication.getApplication().getAssets().open("test.bks");
keyStore.load(inputStream, "123456".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, null);
sslSockFactory = sslContext.getSocketFactory();
return sslSockFactory;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}