ssl socket java_Java SSL/TLS Socket实现

通信端无需向对方证明自己的身份,则称该端处于“客户模式”,否则称其处于“服务器模式”,无论是客户端还是服务器端,都可处于“客户模式”或者“服务器模式”

首先生成服务器端认证证书,使用java自带的keytool工具:

b611c10b4dd6905d60b05cadce4bcef5.png

其中:

-genkey:生成一对非对称密钥

-keyalg:加密算法

-keystore:证书存放路径

-alias:密钥对别名,该别名是公开的

相同的方式,生成客户端认证证书,不过命名为client_rsa.key,别名为clientkey

5aa615651a5b9e2eaa143f50bee9cfbc.png

使用jdk1.5,唯一需要引入的包为log4j-1.2.14.jar

客户端认证:

package com.test.client.auth;

import java.io.FileInputStream;

import java.security.KeyStore;

import java.util.Properties;

import javax.net.ssl.KeyManager;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManager;

import javax.net.ssl.TrustManagerFactory;

import com.test.server.config.Configuration;

public class Auth {

private static SSLContext sslContext;

public static SSLContext getSSLContext() throws Exception{

Properties p = Configuration.getConfig();

String protocol = p.getProperty("protocol");

String sCertificateFile = p.getProperty("serverCertificateFile");

String sCertificatePwd = p.getProperty("serverCertificatePwd");

String sMainPwd = p.getProperty("serverMainPwd");

String cCertificateFile = p.getProperty("clientCertificateFile");

String cCertificatePwd = p.getProperty("clientCertificatePwd");

String cMainPwd = p.getProperty("clientMainPwd");

//KeyStore class is used to save certificate.

char[] c_pwd = sCertificatePwd.toCharArray();

KeyStore keyStore = KeyStore.getInstance("JKS");

keyStore.load(new FileInputStream(sCertificateFile), c_pwd);

//TrustManagerFactory class is used to create TrustManager class.

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");

char[] m_pwd = sMainPwd.toCharArray();

trustManagerFactory.init(keyStore);

//TrustManager class is used to decide weather to trust the certificate

//or not.

TrustManager[] tms = trustManagerFactory.getTrustManagers();

KeyManager[] kms = null;

if(Configuration.getConfig().getProperty("authority").equals("2")){

//KeyStore class is used to save certificate.

c_pwd = cCertificatePwd.toCharArray();

keyStore = KeyStore.getInstance("JKS");

keyStore.load(new FileInputStream(cCertificateFile), c_pwd);

//KeyManagerFactory class is used to create KeyManager class.

KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");

m_pwd = cMainPwd.toCharArray();

keyManagerFactory.init(keyStore, m_pwd);

//KeyManager class is used to choose a certificate

//to prove the identity of the client side.

kms = keyManagerFactory.getKeyManagers();

}

//SSLContext class is used to set all the properties about secure communication.

//Such as protocol type and so on.

sslContext = SSLContext.getInstance(protocol);

sslContext.init(kms, tms, null);

return sslContext;

}

}

客户端主程序:

package com.test.client;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.InetSocketAddress;

import java.net.SocketAddress;

import java.util.Properties;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSocket;

import javax.net.ssl.SSLSocketFactory;

import org.apache.log4j.Logger;

import com.test.client.auth.Auth;

import com.t

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值