20135121 Dermot W.J. Zhang experiment Four

Source: Internet
Author: User

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.

First, the contents of the experiment

1, Write network communication program (based on TCP)

2. Encrypt the communication content using symmetric encryption algorithm

3. Use asymmetric algorithms to distribute the keys used in symmetric encryption

4. Summary calculation and verification of the communication content

Code:

Client:

Package net;

Import java.math.*;

Import java.net.*;

Import java.io.*;

public class Computetcpclient {

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", 4421);

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 Computetcpserver {

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.close ();

Sc.close ();

} catch (Exception e) {

System.out.println (e);

}

}

}

Client:

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. The problem is discussed and verified, the cipher after DES encryption can not use the above two functions for transmission, using a change class on the Web, the problem is resolved, the code is as follows:

Package net;

public class Change {

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;

}

}

2, in the process of interconnection between two machines, unable to connect, after understanding the code port and IP address settings have problems, the IP address is set to the local computer, at the same time network connection, two machines to achieve interconnection.

Third, the experimental experience

This experiment has made me realize that writing a program is not just a single computer operation, it 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. The biggest difficulty in the experiment is how to encrypt the data and then transfer it.

Statistics for PSP (Personal software Process) time

Steps

Time-consuming (min)

Percentage

Demand analysis

10

10%

Design

20

20%

Code implementation

40

40%

Test

10

10%

Analysis Summary

20

20%

20135121 Dermot W.J. Zhang experiment Four

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.