The Java Development Kit (JDK) has good support for encryption and security. One of the advantages is its built-in support for socket communications. Therefore, it is easy to establish a secure data flow between the server and the customer.
Flow
The Java streams is a powerful programming tool. The Java.io package provides a number of standard stream types, and it is easy to create your own stream types. A useful feature of a stream is the same simple process as a linked list. Link FileReader and BufferedReader. Similar concepts are used when using client/server applications.
Key words
For validation, keywords are important to run keygen to produce a keyword. We use the synchronization method, so the client and server must use the same keyword.
Secure Socket
We start with a simple class that provides the encryption we have on the normal socket object. The constructor creates the variable and initializes the password:
Outcipher = cipher.getinstance (algorithm);
Outcipher.init (Cipher.encrypt_mode, key);
Incipher = cipher.getinstance (algorithm);
Incipher.init (Cipher.decrypt_mode, key);
Because the socket is two-way communication, we use two passwords. Encrypt the output data and decrypt the input data. We use getInputStream () and Getoutputstream (), two methods to encrypt the packaged data stream that decrypts common input and output.
The Javax.crypto package in JCE contains both CipherInputStream and CipherOutputStream stream types. They receive stream objects and password objects for input and output.
Socket Server
Start writing our socket server class. Secretsocketserver opens ServerSocket on a port, and when a connection is received, a thread is created using Sockethandler to manipulate the connection.
Socket handle
Locate the keyword by keygen and create a Secretsocket object.
Key key = Keygen.getsecretkey ();
THIS.SS = new Secretsocket (s, key);
All socket processing is done through secretsocket instead of socket objects. Then we use the following code:
in = Ss.getinputstream ();
Remember, in Secretsocket, getInputStream is a combination of cipherinputstream and InputStream. Because Sockethandler is an executable interface, we generate a run () method for it. This method is just waiting for the data in the socket:
boolean bool = true;
while (bool) {
bool = listen();
}
listen方法用来监听socket 。
int aByte;
while ((aByte = in.read()) >= 0) {
system.out.println((char)aByte);
}
Socket Customer
Now let's take a look at the client. The client's work is similar to that of the server side, but it is reversed. First, we create a socket to connect to the server. Use keygen to find the keyword and create a Secure socket (secretsocket). Then we use its outputstream to send the data to the server:
Key key = KeyGen.getSecretKey();
Socket s = new Socket("localhost", 4444);
SecretSocket ss = new SecretSocket(s, key);
OutputStream os = ss.getOutputStream();
os.write("Hello World!".getBytes());
os.flush();
os.close();
s.close();
Summarize
Through the Java stream and the linked list in Jce, we can easily encrypt the network communication based on socket.