最尊贵的客人,欢迎莅临壹品红官网,我们为您在网站建设上提供便捷放心的服务!

会员中心  |   手机访问  |   关于壹品红  |   付款帐号  |   联系我们
  • 自助建网站(省钱,便捷)
  • 购买功能强大的建站系统,懂电脑操作就可以制作精美网站
    模板成品站(提供管理员和FTP帐号密码,可在线修改也可下载源代码修改,适合展示型网站,可安装在自有服务器)
    模板云建站(提供管理员帐号密码,在线编辑修改,有微信公众号,会员,商务,分销等功能,方便升级)

  • 聘请设计师(专业,高效)
  • 壹品红提供建站全套服务,你只需要提供资料与制作要求
    获取报价

网站文库

壹品红

返回列表>>
网站文库

使用加密协议传输对象


对于Socket中一些常用的应用就需要加密传输的数据,此时就需要用到SSLSocket了。

还是一样需要一个实现了java.io.Serializable接口的简单Java对象:

package com.googlecode.garbagecan.test.socket.ssl; public class User implements java.io.Serializable { private static final long serialVersionUID = 1L; private String name; private String password; public User() { } public User(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

package com.googlecode.garbagecan.test.socket.ssl; import java.io.BufferedInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; import javax.net.ServerSocketFactory; import javax.net.ssl.SSLServerSocketFactory; public class MyServer { private final static Logger logger = Logger.getLogger(MyServer.class.getName()); public static void main(String[] args) { try { ServerSocketFactory factory = SSLServerSocketFactory.getDefault(); ServerSocket server = factory.createServerSocket(10000); while (true) { Socket socket = server.accept(); invoke(socket); } } catch (Exception ex) { ex.printStackTrace(); } } private static void invoke(final Socket socket) throws IOException { new Thread(new Runnable() { public void run() { ObjectInputStream is = null; ObjectOutputStream os = null; try { is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); os = new ObjectOutputStream(socket.getOutputStream()); Object obj = is.readObject(); User user = (User)obj; System.out.println("user: " + user.getName() + "/" + user.getPassword()); user.setName(user.getName() + "_new"); user.setPassword(user.getPassword() + "_new"); os.writeObject(user); os.flush(); } catch (IOException ex) { logger.log(Level.SEVERE, null, ex); } catch(ClassNotFoundException ex) { logger.log(Level.SEVERE, null, ex); } finally { try { is.close(); } catch(Exception ex) {} try { os.close(); } catch(Exception ex) {} try { socket.close(); } catch(Exception ex) {} } } }).start(); } }

SSL Client类和SSL Server类类似,只是将其中获取Socket的方式有所变化,其余的代码也和不使用加密方式一样。

package com.googlecode.garbagecan.test.socket.ssl; import java.io.BufferedInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; import javax.net.SocketFactory; import javax.net.ssl.SSLSocketFactory; public class MyClient { private final static Logger logger = Logger.getLogger(MyClient.class.getName()); public static void main(String[] args) throws Exception { for (int i = 0; i < 100; i++) { Socket socket = null; ObjectOutputStream os = null; ObjectInputStream is = null; try { SocketFactory factory = SSLSocketFactory.getDefault(); socket = factory.createSocket("localhost", 10000); os = new ObjectOutputStream(socket.getOutputStream()); User user = new User("user_" + i, "password_" + i); os.writeObject(user); os.flush(); is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); Object obj = is.readObject(); if (obj != null) { user = (User)obj; System.out.println("user: " + user.getName() + "/" + user.getPassword()); } } catch(IOException ex) { logger.log(Level.SEVERE, null, ex); } finally { try { is.close(); } catch(Exception ex) {} try { os.close(); } catch(Exception ex) {} try { socket.close(); } catch(Exception ex) {} } } } }

代码写完了,下面就需要产生keystore文件了,运行下面的命令

keytool -genkey -alias mysocket -keyalg RSA -keystore mysocket.jks

在提示输入项中,密码项自己给定,其它都不改直接回车,这里我使用的密码是“mysocket”。

运行Server

java -Djavax.net.ssl.keyStore=mysocket.jks -Djavax.net.ssl.keyStorePassword=mysocket com.googlecode.garbagecan.test.socket.ssl.MyServer

运行Client

java -Djavax.net.ssl.trustStore=mysocket.jks -Djavax.net.ssl.trustStorePassword=mysocket com.googlecode.garbagecan.test.socket.ssl.MyClient

推荐教程

其它教程

壹品红欢迎您?  获取报价方案   或者   申请合作加盟

  • 正规诚信

    提供公司正规发票 >
    签订正式合同 >

  • 程序开源

    可提供网站源代码 >
    后台功能强大 >

  • 效果保障

    大量现成网站样板可选 >
    可提供效果图 >

  • 服务周到

    售后问题及时响应 >
    到期提前通知 >