Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092849
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类:

2009-06-29 15:00:10

server.java ===============

import java.net.*;
import javax.net.ssl.*;
import java.io.*;
import java.security.*;

public class server
{
        static int port = 8266;
        static SSLServerSocket server;

        /*
         *@param port 监听的端口号
         *@return 返回一个SSLServerSocket对象
         */

        private static SSLServerSocket getServerSocket (int thePort)
        {
                SSLServerSocket s = null;
                try
                {
                        String key = "SSLKey"; //要使用的证书名

                        char keyStorePass[] = "123456".toCharArray (); //证书密码

                        char keyPassword[] = "123456".toCharArray (); //证书别称所使用的主要密码


                        KeyStore ks = KeyStore.getInstance("JKS"); //创建JKS密钥库

                        ks.load (new FileInputStream (key), keyStorePass);

                        //创建管理JKS密钥库的X.509密钥管理器

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

                        kmf.init (ks, keyPassword);

                        SSLContext sslContext = SSLContext.getInstance("SSLv3");

                        sslContext.init (kmf.getKeyManagers (), null, null);

                        //根据上面配置的SSL上下文来产生SSLServerSocketFactory,与通常的产生方法不同

                        SSLServerSocketFactory factory = sslContext.getServerSocketFactory();

                        s = (SSLServerSocket) factory.createServerSocket(thePort);

                }
                catch(Exception e)
                {
                        System.out.println(e);
                }

                return (s);
        }

        public static void main(String args[])
        {
                try
                {
                        server = getServerSocket(port);
                        while (true)
                        {
                                SSLSocket socket = (SSLSocket) server.accept();

                                  //将得到的socket交给CreateThread对象处理,主线程继续监听

                                new CreateThread(socket);

                        }
                }
                catch(Exception e)
                {
                        System.out.println ("main方法错误80:" + e);
                }
        }
}

 /*
  *内部类,获得主线程的socket连接,生成子线程来处理
  */


class CreateThread extends Thread
{
        static BufferedReader in;
        static PrintWriter out;
        static Socket s;

        /*
         *构造函数,获得socket连接,初始化in和out对象
         */


        public CreateThread(Socket socket)
        {
                try
                {
                        s = socket;
                        in = new BufferedReader(new InputStreamReader(s.getInputStream()));

                        out = new PrintWriter(s.getOutputStream(), true);
                        start(); //开新线程执行run方法

                }
                catch (Exception e)
                {
                        System.out.println(e);
                }

        }

        public void run() /* start() call run() method */
        {
                try
                {
                        String msg = in.readLine();
                        System.out.println(msg);
                        s.close();
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }
        }
}


client.java ============

import java.net.*;
import javax.net.ssl.*;
import javax.net.*;
import java.io.*;

public class client
{
        static int port = 8266;

        public static void main(String args[])
        {
                try
                {
                        SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();

                        Socket s = factory.createSocket("localhost", port);

                        PrintWriter out = new PrintWriter(s.getOutputStream (), true);
                        out.println ("安全的输出");
                        out.close ();
                        s.close ();
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }
        }
}
===================

1。首先生成证书
keytool –genkey –keystore SSLKey –keyalg rsa –alias SSL

2。然后运行
java server
java -Djavax.net.ssl.trustStore=SSLKey client

阅读(857) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~