首页

基于dcm4che2的SSLUtils工具类获取ssl、security的SSLContext、TrustManagerFactory、KeyStore对象处理

标签:dcm4che2,security,ssl,工具类,KeyManagerFactory,SSLContext,TrustManagerFactory,KeyStore     发布时间:2018-01-23   

一、前言

关于dcm4che-audit-2.0.25.jar开源包org.dcm4che2.audit.util.SSLUtils安全工具类获取java.security.KeyStore、javax.net.ssl.SSLContext、javax.net.ssl.TrustManagerFactory、javax.net.ssl.KeyManagerFactory实例化对象,具体参见源码说明。

二、源码说明

package org.dcm4che2.audit.util;@b@@b@import java.io.BufferedInputStream;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.security.KeyManagementException;@b@import java.security.KeyStore;@b@import java.security.KeyStoreException;@b@import java.security.NoSuchAlgorithmException;@b@import java.security.SecureRandom;@b@import java.security.UnrecoverableKeyException;@b@import java.security.cert.CertificateException;@b@import javax.net.ssl.KeyManagerFactory;@b@import javax.net.ssl.SSLContext;@b@import javax.net.ssl.TrustManagerFactory;@b@@b@public class SSLUtils@b@{@b@  public static SSLContext getSSLContext(KeyStore keyStore, char[] password, KeyStore trustStore, SecureRandom random)@b@    throws NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, KeyStoreException@b@  {@b@    SSLContext ctx = SSLContext.getInstance("TLS");@b@    ctx.init(kmf(keyStore, password).getKeyManagers(), tmf(trustStore).getTrustManagers(), random);@b@@b@    return ctx;@b@  }@b@@b@  private static KeyManagerFactory kmf(KeyStore ks, char[] password)@b@    throws NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException@b@  {@b@    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());@b@@b@    kmf.init(ks, password);@b@    return kmf;@b@  }@b@@b@  private static TrustManagerFactory tmf(KeyStore ks) throws NoSuchAlgorithmException, KeyStoreException@b@  {@b@    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());@b@@b@    tmf.init(ks);@b@    return tmf;@b@  }@b@@b@  public static KeyStore loadKeyStore(String fileName, char[] password, String type)@b@    throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException@b@  {@b@    return loadKeyStore(new File(fileName), password, type);@b@  }@b@@b@  public static KeyStore loadKeyStore(File file, char[] password, String type)@b@    throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException@b@  {@b@    InputStream in = new BufferedInputStream(new FileInputStream(file));@b@    try {@b@      KeyStore localKeyStore = loadKeyStore(in, password, type);@b@@b@      return localKeyStore; } finally { in.close();@b@    }@b@  }@b@@b@  public static KeyStore loadKeyStore(InputStream in, char[] password, String type)@b@    throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException@b@  {@b@    KeyStore ks = KeyStore.getInstance(type);@b@    ks.load(in, password);@b@    return ks;@b@  }@b@}
<<热门下载>>