How Java Socket transfers objects using encryption protocols
This article shows you how Java Socket uses encryption protocols to transfer objects. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Previous blog posts mentioned some common uses of Socket, but for some applications with security requirements, they need to encrypt the transmitted data, so SSLSocket is needed at this time.
Again, you need a simple Java object that implements the java.io.Serializable interface:
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;}}
SSLServer class, here you need to use the ServerSocketFactory class to create an instance of the SSLServerSocket class, and then get the SSLSocket instance through SSLServerSocket. Considering the concept of interface-oriented programming in object-oriented, there is no SSLServerSocket and SSLSocket in the code, but their parent classes ServerSocket and Socket are used. After getting the ServerSocket and Socket instances, the rest of the code is the same as not using encryption.
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 ();}}
The SSL Client class is similar to the SSL Server class, except that the way you get the Socket in it has changed, and the rest of the code is the same as not using encryption.
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; iTunes +) {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) {}}
When the code is finished, you need to generate the keystore file. Run the following command
Keytool-genkey-alias mysocket-keyalg RSA-keystore mysocket.jks
In the prompt entry, the password entry is given by yourself, and the rest is not changed to enter directly. The password I use here is "mysocket".
Run Server
Java-Djavax.net.ssl.keyStore=mysocket.jks-Djavax.net.ssl.keyStorePassword=mysocket com.googlecode.garbagecan.test.socket.ssl.MyServer
Run Client
Java-Djavax.net.ssl.trustStore=mysocket.jks-Djavax.net.ssl.trustStorePassword=mysocket com.googlecode.garbagecan.test.socket.ssl.MyClient the above is how Java Socket transmits objects using encryption protocols. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.