JAVA + SSL Socket Programming示例

 

 

要实现SSL,哪么我们就必须需要一对公钥和私钥对,我们可以使用Sun为我们提供的keytool工具来生成公私钥对,命令 如下:

keytool -genkey -keystore mySrvKeystore -keyalg RSA -storepass passw0rd -keypass passw0rd -dname "CN=test, OU=test, O=test, L=test, ST=test, C=test"

 

成功的执行此命令, 我们将会得到一个keystore文件(mySrvKeystore ),密码为passw0rd, 私钥的密码为passw0rd, DN是CN=test, OU=test, O=test, L=test, ST=test, C=test。

 

1. 服务器端代码 (EchoServer.java):

 

用javac编译服务端代码

 javac EchoServer.java

 

2. 客户端代码(EchoClient.java)

 

Compile it by using simple command:

   javac EchoClient.java

Running server and client using SSL

First copy certificate file that you created before into working directory and run server with these parameters (notice that you have to change keyStore name and/or trustStrorePassword if you specified different options creating certificate:

   java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=123456 EchoServer

And now again copy certificate file that you created before into working directory and run client with these parameters (notice that you have to change keyStore name and/or trustStrorePassword if you specified different options creating certificate:

   java -Djavax.net.ssl.trustStore=mySrvKeystore -Djavax.net.ssl.trustStorePassword=123456 EchoClient

If you want SSL debug information just add these parameters when running server and/or client:

    -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol -Djavax.net.debug=ssl

 

Playing with server and client

Now just type any string on client console and press return. The same string has to appear on server console.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值