📄 sslcontextfactory.java
字号:
package net.sf.dz.util;import java.io.File;import java.io.FileInputStream;import java.security.KeyStore;import java.security.SecureRandom;import javax.net.ssl.KeyManager;import javax.net.ssl.KeyManagerFactory;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLException;import javax.net.ssl.TrustManager;import javax.net.ssl.TrustManagerFactory;/** * A utility class to simplify creation of {@link SSLContext SSLContext}. * * @author Copyright © <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2001 * @version $Id: SSLContextFactory.java,v 1.2 2004/02/29 18:16:43 vtt Exp $ */public class SSLContextFactory { /** * Create an SSL context object with default options. * * The SSL context is created with a protocol <code>TLS</code>, keystore * located at <code>~/.keystore</code>, and a given keystore password. * * @param password Keystore password. */ public static SSLContext createContext(String password) throws Throwable { return createContext("TLS", System.getProperty("user.home") + File.separator + ".keystore", password); } /** * Create an SSL context object. * * @param protocol Secure protocol. Values that are known to work are: * <code>SSLv3</code>, <code>TLS</code>. * * @param keyStoreName Keystore file name. * * @param password Keystore password. */ public static SSLContext createContext(String protocol, String keyStoreName, String password) throws SSLException { try { SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); SSLContext ctx = SSLContext.getInstance(protocol); if ( password == null ) { // Whatever... password = ""; } char[] passwordArray = new char[password.length()]; for ( int idx = 0; idx < password.length(); idx++ ) { passwordArray[idx] = password.charAt(idx); } FileInputStream keyStoreFile = new FileInputStream(keyStoreName); KeyStore ks = KeyStore.getInstance("JKS"); ks.load(keyStoreFile, null); String keyManagementAlgorithm="SunX509"; KeyManagerFactory km = KeyManagerFactory.getInstance(keyManagementAlgorithm); km.init(ks , passwordArray); KeyManager[] keyManagerSet = km.getKeyManagers(); for (int i = 0; i < keyManagerSet.length; i++) { //System.err.println("KeyManager " + keyManagerSet[i]); } TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(keyManagementAlgorithm); tmFactory.init(ks); TrustManager[] trustManagerSet = tmFactory.getTrustManagers(); for (int i=0; i < trustManagerSet.length; i++) { //System.err.println("TrustManager " + trustManagerSet[i]); } ctx.init(keyManagerSet, trustManagerSet, random); return ctx; } catch ( Throwable t ) { SSLException ex = new SSLException("Can't create secure connection (SSLContext)"); ex.initCause(t); throw ex; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -