4th experiment
Lab Report
Course: Java class: 1353 Name: Wang can also study number: 20135326
Score: Instructor: Lou Jia Peng Experimental Date: 2015.6.9
Experiment level: Preview degree: Experiment time: 15:30~18:00
Instrument Group: 26 Compulsory/elective: Elective experiment number: 04
Experiment Name: Network programming and security
Experimental purposes and requirements:
1. Mastering the Java Network programming method;
2. Master the method of Java security programming;
3. Can be used in a comprehensive range of technologies.
Experimental instrument:
|
Name |
Model |
Number |
|
Computer |
|
2 |
|
|
|
Experimental content:
1 Writing network communication programs (TCP-based)
2 Encrypt the communication content using symmetric encryption algorithm
3 using asymmetric algorithms to distribute the keys used in symmetric encryption
4 Summary calculation and verification of the communication content
5 Other security measures
Related code
Client:
Package net;
Import java.math.*;
Import java.net.*;
Import java.io.*;
public class Tcpused {
public static void Main (String srgs[]) {
try {
To create a socket object that connects to a specified port on a specific server
Socket socket = new Socket ("10.0.6.143", 2224);
Get the network input stream from the server side
BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
Obtain a network output w stream that outputs data from the client to the server
PrintWriter out = new PrintWriter (new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ())), true);
Create a keyboard input stream so that the client enters information Eredwriter (new OutputStreamWriter (Socket.getoutputstream ()) from the keyboard, true);
Creating a keyboard input stream
BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in));
System.out.print ("Please enter data to be sent:");
String s = stdin.readline (); Read the data to be sent from the keyboard
String cs = new SEnc (). ENC (s);
SYSTEM.OUT.PRINTLN ("Ciphertext sent to the server:" + cs);
String T=read.read ();
String ck = new Enc_rsa (). ENC ();
SYSTEM.OUT.PRINTLN ("The encryption key sent to the server is:" + ck);
String result = Digestcalc.hash (s);
Out.println (CK);
OUT.PRINTLN (CS); Transfer over the network to the server
OUT.PRINTLN (result);
} catch (Exception e) {
System.out.println (e);
} finally {
Stdin.close ();
In.close ();
Out.close ();
Socket.close ();
}
}
}
Server:
Package net;
Import java.net.*;
Import java.io.*;
public class Tcpuse {
public static void Main (String srgs[]) throws Exception {
ServerSocket sc = null;
Socket socket = NULL;
try {
sc = new ServerSocket (2224);//Create a server socket
SYSTEM.OUT.PRINTLN ("Port number:" + sc.getlocalport ());
SYSTEM.OUT.PRINTLN ("Server has started ...");
Socket = Sc.accept (); Waiting for client connections
SYSTEM.OUT.PRINTLN ("Connection already established");
Get a reference to a network input stream object
BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
Get a reference to a network output stream object
PrintWriter out = new PrintWriter (new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ())), true);
String key = In.readline ();
SYSTEM.OUT.PRINTLN ("The encryption key received from the client is:" + key);
byte[] keykb = new Dec_rsa (). Dec (key);
String Ctext = In.readline ();//Read data sent by the client
System.out.println ("The ciphertext received from the client is:" + ctext);
String result=sdec.des (Ctext, KEYKB);
String ha = In.readline ();
String sa = digestcalc.hash (result);
Boolean q = Compare.compare (SA, ha);
SYSTEM.OUT.PRINTLN ("The program is complete:" + Q);
Out.close ();
In.close ();
Sc.close ();
} catch (Exception e) {
System.out.println (e);
}
}
}
Client:
Service side:
Link View
Statistical time
|
Steps |
Time-consuming (min) |
Percentage |
|
Demand analysis |
10 |
10% |
|
Design |
20 |
20% |
|
Code implementation |
40 |
60g |
|
Test |
10 |
10% |
|
Analysis Summary |
20 |
20%
|
Second, the problems encountered in the experiment and the solutions
Denied access when the client cannot link after the server is created
Workaround: Adjust the end name to keep the same
Lab Staff:
Server: 20135226 kun Huang
Client: 20135326 King can also http://www.cnblogs.com/baka/
Third, the experimental experience
This test encountered some difficulties, especially in the programming aspect, the others are OK
Java Fourth Experiment