/********************************************************************
* 项目名称 :rochoc <p>
* 包名称 :rochoc.net.security <p>
* 文件名称 :SSLClient <p>
* 编写者 :LuckyStar <p>
* 编写日期 :2008-2-13 <p>
* 程序功能(类)描述 :安全通讯的客户端 <p>
*
* 程序变更日期 :
* 变更作者 :
* 变更说明 :
********************************************************************/
package rochoc.net.security;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
/**
* 类名:SSLClient <p>
* 类描述: 安全通讯的客户端<p>
* 编写者 :luoc<p>
* 编写日期 :2005-6-30<p>
* 主要public成员变量:<p>
* 主要public方法: <p>
**/
public class SSLClient
{
/**
*构造函数说明: <p>
*参数说明: <p>
**/
public SSLClient()
{
init();
}
/**
* 方法名称:init<p>
* 方法功能:初始化客户端Socket <p>
* 参数说明: <p>
* 返回:void <p>
* 作者:luoc
* 日期:2005-6-30
**/
public void init()
{
//server socket's ip and port
String host="localhost";
int port=2001;
//keystore path and password
String keyf="..\\key\\mystore";
String trustf="..\\key\\srvtrust";
String pass="123456";
//set up a connection
SSLSocketFactory ssf=null;
try
{
//init context
SSLContext ctx=SSLContext.getInstance("TLS");
KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");
TrustManagerFactory tmf=TrustManagerFactory.getInstance("SunX509");
KeyStore ks=KeyStore.getInstance("JKS");
KeyStore tks=KeyStore.getInstance("JKS");
//load keystore
ks.load(new FileInputStream(keyf),pass.toCharArray());
tks.load(new FileInputStream(trustf),pass.toCharArray());
kmf.init(ks,pass.toCharArray());
tmf.init(tks);
ctx.init(kmf.getKeyManagers(),tmf.getTrustManagers(),new SecureRandom());
System.out.println("load keystore success.");
ssf=ctx.getSocketFactory();
//create socket
socket=(SSLSocket)ssf.createSocket(host,port);
System.out.println("create socket success.");
//handshake
socket.startHandshake();
System.out.println("handshake success.");
}catch(Exception e)
{
System.out.println("establish connection error.");
e.printStackTrace();
return;
}
}
/**
* 方法名称:sendMessage<p>
* 方法功能:发送信息 <p>
* 参数说明:@param msg <p>
* 返回:void <p>
* 作者:luoc
* 日期:2005-6-30
**/
public void sendMessage(String msg)
{
try
{
OutputStream out=socket.getOutputStream();
InputStream in=socket.getInputStream();
//send message
System.out.println("send message:["+msg+"]");
out.write(msg.getBytes());
out.write(TranTool.DATA_END);
out.flush();
//receive message
byte [] buff=new byte[1024];
int len=0;
System.out.println("recv len:"+(len=in.read(buff)));
System.out.println("receive from srv:[");
System.out.print(new String(buff,0,len));
System.out.println("] receive end.");
}catch(IOException e)
{
e.printStackTrace();
}finally
{
try
{
socket.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
/*全局变量*/
SSLSocket socket=null;
//测试函数
public static void main(String args[])
{
SSLClient sc=new SSLClient();
String msg="Hello SSL Server.";
if(args.length==1)
{
msg=args[0];
}else
{
System.out.println("Useage:Please input the message you want to send.");
}
sc.sendMessage(msg);
}
}