Java Programming Experiment Five

Source: Internet
Author: User

Lab Report

Course: Java class: 1353 Name: Li Haiyin No.: 20135329

Score: Instructor: Lou Jia Peng Experimental Date: 2015.6.12

Experiment level: Preview degree: Experiment time: 15:30~18:00

Instrument Group: Compulsory/Elective: Elective experiment number: 05

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

Teammate Guo Hao http://www.cnblogs.com/20135327leme/ is responsible for the client side of the work, while helping me to solve the various problems encountered in the server.

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

Experimental steps

Server:

Import java.net.*;

Import java.io.*;

public class Server {

public static void Main (String srgs[]) throws Exception {

ServerSocket sc = null;

Socket socket = NULL;

try {

sc = new ServerSocket (4421);//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.cl

OSE ();

Sc.close ();

} catch (Exception e) {

System.out.println (e);

}

}

}

============================================================

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.*;

Import java.io.*;

public class enc_rsa{

public static void Main (String args[]) throws exception{

String s= "Guohao";

FileInputStream f=new FileInputStream ("Skey_rsa_pub.dat");

ObjectInputStream b=new ObjectInputStream (f);

Rsapublickey pbk= (Rsapublickey) b.readobject ();

BigInteger e=pbk.getpublicexponent ();

BigInteger N=pbk.getmodulus ();

System.out.println ("e=" +e);

System.out.println ("n=" +n);

Byte ptext[]=s.getbytes ("UTF8");

BigInteger m=new BigInteger (ptext);

BigInteger C=m.modpow (e,n);

System.out.println ("c=" +c);

String cs=c.tostring ();

BufferedWriter out=

New BufferedWriter (New OutputStreamWriter (

New FileOutputStream ("Enc_rsa.dat"));

Out.write (Cs,0,cs.length ());

Out.close ();

}

Public String Enc () throws exception{

TODO auto-generated Method Stub

String s= "Guohao";

FileInputStream f=new FileInputStream ("Skey_rsa_pub.dat");

ObjectInputStream b=new ObjectInputStream (f);

Rsapublickey pbk= (Rsapublickey) b.readobject ();

BigInteger e=pbk.getpublicexponent ();

BigInteger N=pbk.getmodulus ();

System.out.println ("e=" +e);

System.out.println ("n=" +n);

Byte ptext[]=s.getbytes ("UTF8");

BigInteger m=new BigInteger (ptext);

BigInteger C=m.modpow (e,n);

System.out.println ("c=" +c);

String cs=c.tostring ();

BufferedWriter out=

New BufferedWriter (New OutputStreamWriter (

New FileOutputStream ("Enc_rsa.dat"));

Out.write (Cs,0,cs.length ());

Out.close ();

Return CS;

}

}

Import java.security.*;

public class digestcalc{

public static void Main (String args[]) throws exception{

String X=args[0];

MessageDigest m=messagedigest.getinstance ("MD5");

M.update (X.getbytes ());

byte s[]=m.digest ();

String result= "";

for (int i=0; i<s.length; i++) {

Result+=integer.tohexstring (0X000000FF & S[i]) |

0XFFFFFF00). substring (6);

}

SYSTEM.OUT.PRINTLN (result);

}

public static string hash (string s) throws Exception {

TODO auto-generated Method Stub

String x=s;

MessageDigest m=messagedigest.getinstance ("MD5");

M.update (X.getbytes ());

byte s1[]=m.digest ();

String result= "";

for (int i=0; i<s1.length; i++) {

Result+=integer.tohexstring (0X000000FF & S1[i]) |

0XFFFFFF00). substring (6);

}

return result;

}

}

 

Code implementation

)

steps

Time (min)

percent

Requirements analysis

17.14%

Design

8.57%

(

34.29%

Test

+

34.29%

Analysis Summary

Ten

 5.71%

 

Second, the problems encountered in the experiment and the solutions

1. Send after des encrypted ciphertext is the way to convert it to a string, where the client is using the ToString () function, the server is using the GetBytes () function, after the transmission of ciphertext error. In the last discussion, binary to hexadecimal output cipher text is used. The code is as follows:

public static String bytestohexstring (byte[] src) {    StringBuilder StringBuilder = new StringBuilder ("");       if (src = = NULL | | Src.length <= 0) {           return null;       }       for (int i = 0; i < src.length; i++) {       &nbs p;   int v = src[i] & 0xff;           String HV = Integer.tohe Xstring (v);           if (Hv.length () < 2) {                stringbuilder.append (0);          }          stringbuilder.append (HV);       }       return stringbuilder.tostring ();  }  

2. Two computers have been unable to connect

In the lab we started with our own IPV4 address from the bedroom LAN and then showed the connection timeout.

We think it may be that the two computers are not in the same network without a successful link, try to use a computer to connect to the network and WiFi to another computer, and then connect two computers again, finally connected successfully.

Do not know why, it may be because of the local area network!

Java Programming Experiment Five

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.