All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cmonbaby.http.https.other.SslContextFactory Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.cmonbaby.http.https.other;

import android.content.Context;
import android.util.Log;

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

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

/**
 * 

Author: Simon *

QO: 8950764 *

Email: [email protected] *

WebSize: https://www.cmonbaby.com *

Version: 1.0.0 *

Date: 2020/12/28 *

Description: */ public class SslContextFactory { /** * 取bks文件的sslSocketFactory * * @param context 上下文 * @param certificates 证书 * @param password 证书密码 * @return SSLSocketFactory */ public static SSLSocketFactory getSSLSocketFactory(Context context, int certificates, String password) { final String CLIENT_AGREEMENT = "TLS";//使用协议 final String CLIENT_TRUST_KEYSTORE = "BKS"; SSLContext sslContext = null; try { //取得SSL的SSLContext实例 sslContext = SSLContext.getInstance(CLIENT_AGREEMENT); //取得TrustManagerFactory的X509密钥管理器实例 TrustManagerFactory trustManager = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); //取得BKS密库实例 KeyStore tks = KeyStore.getInstance(CLIENT_TRUST_KEYSTORE); try (InputStream is = context.getResources().openRawResource(certificates)) { tks.load(is, password.toCharArray()); } //初始化密钥管理器 trustManager.init(tks); //初始化SSLContext sslContext.init(null, trustManager.getTrustManagers(), null); } catch (Exception e) { e.printStackTrace(); Log.e("SslContextFactory", e.getMessage()); } if (sslContext == null) { Log.e("SslContextFactory", "sslContext is null!"); throw new RuntimeException("SslContextFactory sslContext is null!"); } return sslContext.getSocketFactory(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy