SslSocket server and client simple example Java environment

 //客户端代码如下
1
import java.io.*; 2 import java.net.InetSocketAddress; 3 import java.net.Socket; 4 5 import javax.net.ssl.SSLSocket; 6 import javax.net.ssl.SSLSocketFactory; 7 8 public class SslClient { 9 private SSLSocket s; 10 private DataOutputStream out; 11 private DataInputStream in; 12 13 public SslClient() throws IOException { 14 } 15 16 public void sendMessage(Socket s, String msg) { 17 try { 18 19 out = new DataOutputStream(s.getOutputStream()); 20 String jsonString = msg; 21 out.write(jsonString.getBytes()); 22 } catch (IOException e) { 23 } 24 25 } 26 27 public String receiveMessage(Socket s) { 28 try { 29 30 in = new DataInputStream(s.getInputStream()); 31 BufferedReader socketIn = new BufferedReader(new InputStreamReader(in));// 接受到 32 String res = socketIn.readLine(); 33 if (res == null || res.equals("null")) { 34 res = ""; 35 } 36 return res; 37 } catch (Exception e) { 38 return ""; 39 } 40 41 } 42 is 43 is public String Talk (IP String, int Port, the trustStore String, String the trustStorePassword, int socketOutTime, String MSG) 44 is throws Exception { 45 the try { 46 is int connectTimeout socketOutTime * = 1000; // set the timeout 47 System.setProperty ( " the javax.net.ssl.trustStore ", the trustStore); // set the trusted key repository 48 System.setProperty (" javax.net.ssl.trustStorePassword ", the trustStorePassword); // set the password trusted key repository 49 = Sslsf the SSLSocketFactory (the SSLSocketFactory) the SSLSocketFactory.getDefault (); // use SSLSocket factory to create a Secure Sockets 50 S = (SSLSocket) sslsf.createSocket (); 51 is s.connect ( new new the InetSocketAddress (IP, Port), connectTimeout) ; 52 is s.startHandshake (); 53 is 54 is System.out.println ( "transmitted to the server:" + MSG); 55 the sendMessage (S, MSG); 56 is // send string 57 is string Result = receiveMessage (S); 58 System.out.println ( "server returns:" + Result); 59 out.close(); 60 in.close(); 61 if (s != null) { 62 s.close(); 63 } 64 return result; 65 66 } catch (Exception e) { 67 e.printStackTrace(); 68 out.close(); 69 in.close(); 70 if (s != null) { 71 s.close(); 72 } 73 return ""; 74 } 75 76 } 77 78 public static void main(String[] args) throws Exception { 79 SslClient c = new SslClient(); 80 String responseMsg = c.talk("xxx.xxx.xxx.xxx", 8088, "D:\\xxxx\\shfqtruststore.jks", "123456", 5, "test\n"); 81 System.out.println(responseMsg); 82 83 } 84 85 }
 //服务端代码如下
1
import java.io.BufferedReader; 2 import java.io.DataOutputStream; 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.security.KeyManagementException; 7 import java.security.KeyStore; 8 import java.security.KeyStoreException; 9 import java.security.NoSuchAlgorithmException; 10 import java.security.UnrecoverableKeyException; 11 import javax.net.ssl.KeyManagerFactory; 12 import javax.net.ssl.SSLContext; 13 import javax.net.ssl.SSLServerSocket; 14 import javax.net.ssl.SSLServerSocketFactory; 15 import javax.net.ssl.SSLSocket; 16 17 public class SSLServer { 18 public static void startSSLServer() throws IOException, java.security.cert.CertificateException { 19 int port = 8089;// 监听端口 20 String keyFile = "D:\\xxxx\\shfqkeystore.jks";// 密钥库文件 21 is String keyFilePass = "123456"; // cryptographic key database 22 is String keypass = "123456"; // key alias password 23 is the SSLServerSocket SSLSocket = null ; // secure connection socket 24 the KeyStore KS; // adhesion keystore 25 a KeyManagerFactory KMF; // key management facility 26 is the SSLContext SSLC = null ; // secure connection 27 // initializing a secure connection key 28 the try { 29 KS = KeyStore.getInstance ( "the JKS" ); 30 ks.load(new FileInputStream(keyFile), keyFilePass.toCharArray()); 31 kmf = KeyManagerFactory.getInstance("SunX509"); 32 kmf.init(ks, keyPass.toCharArray()); 33 sslc = SSLContext.getInstance("SSLv3"); 34 sslc.init(kmf.getKeyManagers(), null, null); 35 } catch (KeyManagementException ex) { 36 ex.printStackTrace(); 37 } catch (UnrecoverableKeyException ex) { 38 ex.printStackTrace(); 39 } the catch (KeyStoreException EX) { 40 ex.printStackTrace (); 41 is } the catch (NoSuchAlgorithmException EX) { 42 is ex.printStackTrace (); 43 is } 44 is // factory to create a secure connection with the secure socket connection 45 the SSLServerSocketFactory sslssf = sslc.getServerSocketFactory (); 46 is SSLSocket = (the SSLServerSocket) sslssf.createServerSocket (Port); // create and enter a listening 47 System.out.println ( "listening ..." ); 48 the while ( to true ) { 49 = Ssocket the SSLSocket (the SSLSocket) sslsocket.accept (); // accept client connections 50 System.out.println ( "Server Connection ....." ); 51 is // The following examples socket communication code with the code 52 is the BufferedReader = socketIn new new the BufferedReader ( new new the InputStreamReader (ssocket.getInputStream ())); 53 is the DataOutputStream socketOut = new new the DataOutputStream (ssocket.getOutputStream ()); 54 is String S = null ; 55 System.out.println ( "Please the wait Client 'Message S .. " ); 56 is S = socketIn.readLine(); 57 System.out.println("Client Message: " + s); 58 59 // s = userIn.readLine(); 60 s = "接收成功"; 61 System.out.print("Server Message: " + s); 62 socketOut.write(s.getBytes()); 63 ssocket.close(); 64 } 65 66 } 67 68 public static void main(String[] args) { 69 try { 70 startSSLServer(); 71 } catch (Exception e) { 72 e.printStackTrace(); 73 } 74 } 75 }

Certificates which uses, reference can   https://blog.csdn.net/shfqbluestone/article/details/21242323  , 1-6 to generate the article.

When bloggers run the above examples using JDK version 1.7, other versions should be little difference.

Guess you like

Origin www.cnblogs.com/sguo/p/11585702.html