Beijing Institute of Electronic Technology (BESTI)
Real Inspection report
Course: Java Programming Class: 1352 Name: Ma Qiyang No.: 20135234
Score: Instructor: Lou Jia Peng Experimental Date: 2015.6.10
Experiment level: Preview degree: Experiment time: 15:30---17:20
Instrument Group: 34 Compulsory/Elective: Elective experiment number: 04
Experiment Name: Network programming (simple server and client)
Experimental purposes and requirements:
1. Master socket Basic use method, learn to establish socket connection;
2. Master simple server and client programs, to achieve client and server communication;
3. Mastering the Java Network programming method;
Experimental instrument:
Name |
Model |
Number |
Notebook PC |
DELL |
1 |
|
|
|
First, Experimental Content
1, the operation of the textbook TCP code, pairing, one-man server, one-person client;
2, the use of encryption and decryption code package, compile and run code, client-side encryption, server decryption;
3, the client encrypted plaintext after the cipher sent through TCP;
4, encryption using Des,des encryption key to send to the server, using the server's public key encryption, public key algorithm using RSA, verify the integrity of the sending information use MD5
Lab Staff:
Server: 20135223 He Weizin http://www.cnblogs.com/20135223heweiqin/
Client: 20135234 Ma Qiyang
Second, Experimental Steps
1. The team member uses the computer 1 to create the local area network, acts as the client;
2. Team member two use the computer to connect to the LAN and query their own IP address, acting as a server, and then run the server code, that is, open the server;
3. The group of members in the client combination code, first need to connect to the server, according to the local IP address to modify the code in the IP address and port. Then create the key, according to the server port number request connection, after successful connection, the team members always send data, read the data from the keyboard and encrypt, check the connection status.
Third, Client Code
File Name:ComputeTCPClient.java
Import java.net.*;
Import java.io.*;
Import java.security.*;
Import javax.crypto.*;
Import javax.crypto.spec.*;
Import java.security.spec.*;
Import javax.crypto.interfaces.*;
Import java.security.interfaces.*;
Import java.math.*;
public class Computetcpclient {
public static void Main (String srgs[]) throws exception{
try {
Keygenerator kg=keygenerator.getinstance ("Desede");
Kg.init (168);
Secretkey K=kg.generatekey ();
Byte[] ptext2=k.getencoded ();
String Kstr=parsebyte2hexstr (KB);
To create a socket object that connects to a specified port on a specific server
Socket socket = new Socket ("127.28.133.94", 4421);
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);
Create a keyboard input stream so that clients can enter information from the keyboard
BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in));
FileInputStream f3=new FileInputStream ("Skey_rsa_pub.dat");
ObjectInputStream b2=new ObjectInputStream (F3);
Rsapublickey pbk= (Rsapublickey) b2.readobject ();
BigInteger e=pbk.getpublicexponent ();
BigInteger N=pbk.getmodulus ();
System.out.println ("e=" +e);
System.out.println ("n=" +n);
Byte ptext2[]=kstr.getbytes ("UTF8");
BigInteger m=new BigInteger (PTEXT2);
BigInteger C=m.modpow (e,n);
System.out.println ("c=" +c);
String cs=c.tostring ();
OUT.PRINTLN (CS); Transfer over the network to the server
System.out.print ("Please enter data to be sent:");
String S=stdin.readline (); Read the data to be sent from the keyboard
Cipher cp=cipher.getinstance ("Desede");
Cp.init (Cipher.encrypt_mode, k);
Byte ptext[]=s.getbytes ("UTF8");
Byte ctext[]=cp.dofinal (ptext);
String str=parsebyte2hexstr (Ctext);
Out.println (str); Transfer over the network to the server
String x=s;
MessageDigest m2=messagedigest.getinstance ("MD5");
M2.update (X.getbytes ());
byte a[]=m2.digest ();
String result= "";
for (int i=0; i<a.length; i++) {
Result+=integer.tohexstring (0X000000FF & A[i]) |
0XFFFFFF00). substring (6);
}
SYSTEM.OUT.PRINTLN (result);
OUT.PRINTLN (result);
/*s=result;
FileInputStream f3=new FileInputStream ("Skey_rsa_pub.dat");
ObjectInputStream b2=new ObjectInputStream (F3);
Rsapublickey pbk= (Rsapublickey) b2.readobject ();
BigInteger e=pbk.getpublicexponent ();
BigInteger N=pbk.getmodulus ();
System.out.println ("e=" +e);
System.out.println ("n=" +n);
Byte ptext2[]=s.getbytes ("UTF8");
BigInteger m=new BigInteger (PTEXT2);
BigInteger C=m.modpow (e,n);
System.out.println ("c=" +c);
String cs=c.tostring ();
OUT.PRINTLN (CS); Transfer to server via network */
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);
}
finally{
Stdin.close ();
In.close ();
Out.close ();
Socket.close ();
}
}
public static String Parsebyte2hexstr (byte buf[]) {
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < buf.length; i++) {
String hex = integer.tohexstring (Buf[i] & 0xFF);
if (hex.length () = = 1) {
Hex = ' 0 ' + hex;
}
Sb.append (Hex.touppercase ());
}
return sb.tostring ();
}
public static byte[] Parsehexstr2byte (String hexstr) {
if (Hexstr.length () < 1)
return null;
Byte[] result = new Byte[hexstr.length ()/2];
for (int i = 0;i< hexstr.length ()/2; i++) {
int high = Integer.parseint (Hexstr.substring (i*2, i*2+1), 16);
int low = Integer.parseint (Hexstr.substring (i*2+1, i*2+2), 16);
Result[i] = (byte) (high * + low);
}
return result;
}
}
Four. The experimental process
Iv. Experience of Experiment
This experiment greatly expanded our horizons, so that I learned that writing a program is not just a single computer operation of the thing, can also rise to the network level, causing my great interest in Java. In the experiment, I learned how to create clients and servers, and use the Out.println () and In.readline () functions to connect and transfer data. Understand how to encrypt the data and then transfer it.
Statistics for PSP time:
steps |
Time (min) |
percent |
Requirements analysis |
|
10% |
Design |
|
20% |
Code implementation |
- |
40% |
Test |
+ |
10% |
Analysis Summary |
30 |
20% |
20135234 Ma Qiyang Experiment Four