Fourth experimental report on Java programming

Source: Internet
Author: User

Beijing Institute of Electronic Technology (besti)

Lab Report

Course: Java programming class: 1352 name: He Weizin No.:20135223

Score: Instructor: Lou Jia Peng Experimental Date:2015.6.10

Experiment level: Preview degree: Experiment time:15:30---17:20

Instrument Group: 23 Compulsory / elective: Elective experiment Number: 05

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 14z-5423

1

First, the contents of the experiment

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

Second, the experimental steps

Pair network programming

Group members: He Weizin Ma Qiyang: http://www.cnblogs.com/mqy123/

    1. The crew creates a local area network with Computer 1, acts as a client,

2 members two use the computer to connect to the local area network 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, the experimental code

(i) Client code

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 ("192.168.1.109", 8642);
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;
}
}

(ii) Server code

Packet 20135223

Import java.net.*;

Import java.io.*;

Import java.security.*;
Import java.security.spec.*;
Import javax.crypto.*;
Import javax.crypto.spec.*;
Import javax.crypto.interfaces.*;
Import java.security.interfaces.*;
Import java.math.*;
public class computetcpserver{
public static void Main (String srgs[]) throws Exception {
ServerSocket sc = null;
Socket Socket=null;
try {
sc= new ServerSocket (8642);//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 (OutputStreamWriter ()), true);

String Aline2=in.readline ();
BigInteger c=new BigInteger (aline2);
FileInputStream f=new FileInputStream ("Skey_rsa_priv.dat");
ObjectInputStream b=new ObjectInputStream (f);
Rsaprivatekey prk= (Rsaprivatekey) b.readobject ();
BigInteger d=prk.getprivateexponent ();
BigInteger N=prk.getmodulus ();
System.out.println ("d=" +d);
System.out.println ("n=" +n);
BigInteger M=c.modpow (d,n);
System.out.println ("m=" +m);
Byte[] Keykb=m.tobytearray ();
String Aline3=new string (MT, "UTF8");
String aline3=parsebyte2hexstr (byte buf[]);

String aline=in.readline ();//Read data sent by the client
FileInputStream f2=new FileInputStream ("Keykb1.dat");
int num2=f2.available ();
Byte[] Keykb=new byte[num2];
F2.read (KEYKB);
Byte[] Ctext=parsehexstr2byte (Aline);
Key k=new Secretkeyspec (keykb, "Desede");
Cipher cp=cipher.getinstance ("Desede");
Cp.init (Cipher.decrypt_mode, k);
byte []ptext=cp.dofinal (Ctext);

String P=new string (Ptext, "UTF8");
System.out.println ("received from the client information:" +p); Returns results to the client over a network output stream

/*string Aline2=in.readline ();
BigInteger c=new BigInteger (aline2);
FileInputStream f=new FileInputStream ("Skey_rsa_priv.dat");
ObjectInputStream b=new ObjectInputStream (f);
Rsaprivatekey prk= (Rsaprivatekey) b.readobject ();
BigInteger d=prk.getprivateexponent ();
BigInteger N=prk.getmodulus ();
System.out.println ("d=" +d);
System.out.println ("n=" +n);
BigInteger M=c.modpow (d,n);
System.out.println ("m=" +m);
Byte[] Mt=m.tobytearray ();
String Aline3=new string (MT, "UTF8"); */

String Aline3=in.readline ();
String x=p;
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);

if (aline3.equals (result)) {
System.out.println ("match success");
}

Out.println ("match success");
Out.close ();
In.close ();
Sc.close ();
} catch (Exception e) {
System.out.println (e);
}
}
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

Four. Problems encountered

Issue one: Although the server has been started, establish a link, but cannot be equipped with the client successfully

The problem: the IP address in the experiment code is set according to the IP address on this computer

How to FIX: Modify IP Address

Five, the Experiment experience

1. Pair programming, realize the two PC information transmission, enhance the team awareness and cooperation ability;

2. basic understanding of simple server and client programs, how to implement client and server communication;

3. Basic understanding of how to encrypt the data, and then transfer;

The significance of this experiment has stimulated my interest in Java program development and found that as long as you learn by heart, you will find a lot of interesting things.

Fourth experimental report on Java programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.