要实现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.