Experimental content
1. Master the socket program writing;
2. Mastering the use of cryptographic technology;
3. Design Safety
4. Summary calculation and verification of the communication content
Experimental steps
1. Secure Transmission of information:
|
Sender a —————— > receiver B
A when encrypting, use B's public key
b When decrypting, use the private key of B |
Sender A pairs the information (clear text) with Des key encryption, using RSA to encrypt the preceding des key information, and eventually the mixed information is passed. Use the hash function to authenticate the plaintext as well.
After receiving the information, the receiver B decrypts the DES key information with RSA, and then decrypts the information of the key information obtained by RSA decryption, and finally we can get the information we want (plaintext). The hash function is used to verify the clear text, which is equal to the hash value sent over, and the validation is passed.
2. Experiment Code:
Client code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
Package client;
File Name:ComputeTCPClient.java
Import java.net.*;
Import java.io.*;
Import java.security.*;
public class Main
{
public static void Main (String srgs[])
{
Try
{
Testdes DES = new Testdes ();
To create a socket object that connects to a specified port on a specific server
Socket socket = new Socket ("127.0.0.1", 8002);
Get the network input stream from the server side
BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
Obtain a network output stream from the client to the server-side output data
PrintWriter out=new PrintWriter (New BufferedWriter (OutputStreamWriter ()), true);
ObjectOutputStream O1 = new ObjectOutputStream (Socket.getoutputstream ());
PrintWriter out2=new PrintWriter (New BufferedWriter (OutputStreamWriter ()), true);
Create a keyboard input stream so that clients can enter information from the keyboard
BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in));
System.out.print ("Please enter data to be sent:");
String Str=stdin.readline (); Read the data to be sent from the keyboard
String str2;//Check Code
Mydigest MD = new Mydigest ();
Md.getdigest ("SHA-1");
STR2 = md.todigeststring (str, "SHA-1");
System.out.println ("Check code is:" +STR2);
byte[] cipher = Des.encrypt (str);
Out.println (str); Transferring characters to the server over the network
Out.println (STR2);//Transmit calibration information to the server
System.out.println ("Test:" +cipher);
O1.writeobject (cipher);//transmit encrypted information to the server
SYSTEM.OUT.PRINTLN (cipher);
/* In order to demonstrate, the plaintext and ciphertext are transmitted over the network, the actual only ciphertext, the key through the secret channel alone transmission * *
Str=in.readline ();//Read results from the network input stream
SYSTEM.OUT.PRINTLN ("The result received from the server is:" +STR); Results returned by the output server
}
catch (Exception e)
{
System.out.println (e);
}
}
}
Server code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
Package server;
/**
*
* @author rockstone
*/
//File Name:ComputeTCPServer.java
Import java.net.*;
Import java.io.*;
public class main
{
public static void Main (String srgs[])
{
ServerSocket sc = null;
Socket Socket=null;
Try
{
sc= new ServerSocket (8002);//Create a server socket
System.out.println ("Port number:" + sc.getlocalport ());
SYSTEM.OUT.PRINTLN ("Server has started ...");
Socket = sc.accept ();//wait for Client connection
System.out.println ("Connection already established");
//Get a reference to the network input stream object
BufferedReader in = new Buf Feredreader (New InputStreamReader (Socket.getinputstream ()));
ObjectInputStream in1 = new ObjectInputStream (Socket.getinputstream ());
////obtains a reference to the network output stream object
PrintWriter out=new printwriter (New BufferedWriter ( Socket.getoutputstream ())), true);
String Aline=in.readline ();//Read data sent by the client
String aline2 = In.readline ();//Read check code
byte[] c= (byte[]) In1.readobject ();//Read ciphertext
Mydigest MD = new Mydigest ();
Mydigest.getdigest ("SHA-1");
String Mdresult = mydigest.todigeststring (Aline, "SHA-1");//Checksum
if (Aline2.equals (Mdresult))
{
SYSTEM.OUT.PRINTLN ("Correct transmission of information");
SYSTEM.OUT.PRINTLN ("Check information is:" +mdresult);
}
Else
SYSTEM.OUT.PRINTLN ("Validation code inconsistent!") ");
Server decryption
Testdes show = new Testdes ();
Show.decryption (C);
System.out.println ("received from the client information:" +aline); Returns results to the client over a network output stream
Out.println (Aline);
Out.close ();
In.close ();
Sc.close ();
}
catch (Exception e)
{
System.out.println (e);
}
}
}
Experimental test:
Client:
Server:
Problems encountered in the experiment and the solving methods
Question 1. Two computers have been unable to connect to each other
At first, I didn't know how to do this experiment with two computers.
Answer: Use the other computer IP on the line. Connect a computer to a network then WiFi to another computer, and then connect two computers again, finally connected successfully.
Question 2. After the display is connected, the client sends a message to the server, but displays an exception.
A: After changing the port to a port 8080, the connection sends the message again, successfully.
Experiment Experience:
This experiment let me understand that Java is not just a stand-alone "game", can also rise to the network level, causing my great interest in Java. During the experiment, I learned how to create clients and servers, and use the Out.println () and In.readline () functions to connect and transfer data. The difficulty of the experiment is how to decrypt and then transmit the data. The experiment gained a lot.
Java experiment Four application of TCP client and server