java p12 ssl_Java通过 p12 建立ssl链接

本文展示了如何使用Java处理P12文件来建立SSL连接,包括加载P12输入流、设置密钥库类型和密码、初始化SSLContext等步骤。在解决过程中,由于初始密码错误,通过PEM和KEY重新生成了正确的P12文件,最终成功建立了SSL连接。
摘要由CSDN通过智能技术生成

public static void main(String[] args) throws UnrecoverableKeyException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException {

ResourceLoader resourceLoader = new DefaultResourceLoader();

InputStream keyStore = resourceLoader.getResource("test.p12").getInputStream();

String   keyStorePassword = "111111";

String  keyPassword = "111111";

String   KeyStoreType= "PKCS12";

String   KeyManagerAlgorithm = "SunX509";

String   SSLVersion = "SSLv3";

new Test01().getHttpsURLConnection(null, keyStore, keyStorePassword, keyPassword, KeyStoreType, KeyManagerAlgorithm, SSLVersion);

}

public HttpURLConnection getHttpsURLConnection(URL url, InputStream  keystore,

String   keyStorePass,String  keyPassword, String  KeyStoreType

,String KeyManagerAlgorithm, String  SSLVersion)

throws NoSuchAlgorithmException, KeyStoreException,

CertificateException, FileNotFoundException, IOException,

UnrecoverableKeyException, KeyManagementException {

System.setProperty("javax.net.debug","ssl,handshake,record");

SSLContext sslcontext = SSLContext.getInstance(SSLVersion);

KeyManagerFactory kmf =  KeyManagerFactory.getInstance(KeyManagerAlgorithm);

KeyStore ks = KeyStore.getInstance(KeyStoreType);

ks.load(keystore, keyStorePass.toCharArray());

kmf.init(ks, keyPassword.toCharArray());

TrustManagerFactory tmf = TrustManagerFactory

.getInstance(TrustManagerFactory.getDefaultAlgorithm());

tmf.init(ks);

TrustManager[] tm = tmf.getTrustManagers();

sslcontext.init(kmf.getKeyManagers(), tm, null);

SSLSocketFactory sslSocketFactory = sslcontext.getSocketFactory();

HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);

//HttpsURLConnection httpsURLConnection = ( HttpsURLConnection)url.openConnection();

return null;

}

最开始拿到p12的时候,给的密码是错的,试了好久,最后断定是p12文件的密码不正确,

于是通过pem和key重新生成了一个p12文件,密码自己设置后,就成功了。

原文:http://my.oschina.net/u/555639/blog/524821

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值