1.
以阿里云的https的獲取方式
得到xxx.pem文件和 xxx.key文件
2.
因為android 在ssl這塊不支持 JKS 所以我們要生成BKS文件
由pem和key文件 生成BKS文件的步驟
2.1 由pem和key文件先生成JKS文件
參考:https://blog.csdn.net/hfismyangel/article/details/83792992
2.1.1.生成pfx文件
openssl pkcs12 -export -out device2.pfx -in device2.pem -inkey device2.key
關于(openssl 文件一般在linux服務器上有該命令芦缰,如果在windows上的話需要自己下載)
2.1.2.得到pfx之后世杀,生成JKS 韧拒,第一條生成之后皮官,說我的規(guī)范已過時缸濒,所以第二條升級最新版的規(guī)范
keytool -importkeystore -srckeystore device2.pfx -destkeystore device2.jks -srcstoretype PKCS12 -deststoretype JKS
keytool -importkeystore -srckeystore device2.jks -destkeystore device2.jks -deststoretype pkcs12
2.1.3.
得到jks 使用軟件
https://nchc.dl.sourceforge.net/project/portecle/v1.11/portecle-1.11.zip
然后導入jks文件 然后生成BKS文件
3.
使用
portecle-1.11.zip 需要配置一下
jdk:
C:\Program Files\Java\jdk1.8.0_181\jre\lib\security
修改文件內(nèi)容如下:
? security.provider.11=org.bouncycastle.jce.provider.BouncyCastleProvider
C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext
如果要使用本地bks生成
則:添加
? ??
jar包下載位置:
鏈接:https://pan.baidu.com/s/1LEe9EWVgUgpJKRaqaJYNQw
提取碼:7m70
復制這段內(nèi)容后打開百度網(wǎng)盤手機App此虑,操作更方便哦
最后關于代碼的實現(xiàn):
//因為我有兩個簽名證書阔拳,而且需要區(qū)分IP纱烘,如果IP段 <8 則使用device1.bks 否則使用IP段 >=8 則使用device2.bks
public static SSLContext sslCreate() {
? ? ? ? SSLContext sslContext = null;
? ? ? ? try {
? ? ? ? ? ? // load up the key store
? ? ? ? ? ? String STORETYPE = "BKS";
? ? ? ? ? ? String STOREPASSWORD = "123123";
? ? ? ? ? ? String KEYPASSWORD = "123123";
? ? ? ? ? ? KeyStore ks = KeyStore.getInstance(STORETYPE);
? ? ? ? ? ? Log.i("TAG", "Paramter.localIp:" + Paramter.localIp);
? ? ? ? ? ? int netSegement = Integer.parseInt(Paramter.localIp.split("\\.")[2]);
? ? ? ? ? ? InputStream ksInputStream = null;
? ? ? ? ? ? Log.i("TAG", "netSegement :" + netSegement);
? ? ? ? ? ? if (netSegement < 8) {
? ? ? ? ? ? ? ? ksInputStream = MainService.context.getResources().openRawResource(R.raw.device1);? /// 證書存放地址
? ? ? ? ? ? } else if (netSegement >= 8) {
? ? ? ? ? ? ? ? ksInputStream = MainService.context.getResources().openRawResource(R.raw.device2);? /// 證書存放地址
? ? ? ? ? ? }
? ? ? ? ? ? ks.load(ksInputStream, STOREPASSWORD.toCharArray());
? ? ? ? ? ? KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
//? ? ? ? ? ? ? ? KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
? ? ? ? ? ? kmf.init(ks, KEYPASSWORD.toCharArray());
? ? ? ? ? ? TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
//? ? ? ? ? ? ? ? TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
? ? ? ? ? ? tmf.init(ks);
? ? ? ? ? ? sslContext = SSLContext.getInstance("TLS");
? ? ? ? ? ? sslContext.init(kmf.getKeyManagers(), null, null);
? ? ? ? } catch (
? ? ? ? ? ? ? ? UnrecoverableKeyException e) {
? ? ? ? ? ? Log.e("TAG", "UnrecoverableKeyException :" + e.getMessage());
? ? ? ? } catch (
? ? ? ? ? ? ? ? NoSuchAlgorithmException e) {
? ? ? ? ? ? Log.i("TAG", "NoSuchAlgorithmException :" + e.getMessage());
? ? ? ? } catch (
? ? ? ? ? ? ? ? KeyStoreException e) {
? ? ? ? ? ? Log.e("TAG", "KeyStoreException :" + e.getMessage(), e);
? ? ? ? } catch (
? ? ? ? ? ? ? ? IOException e) {
? ? ? ? ? ? Log.i("TAG", "IOException :" + e.getMessage());
? ? ? ? } catch (
? ? ? ? ? ? ? ? CertificateException e) {
? ? ? ? ? ? Log.i("TAG", "CertificateException :" + e.getMessage());
? ? ? ? } catch (
? ? ? ? ? ? ? ? KeyManagementException e) {
? ? ? ? ? ? Log.i("TAG", "KeyManagementException :" + e.getMessage());
? ? ? ? }
? ? ? ? return sslContext;
? ? }
最后設置到websocket上
svs.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext));