Java Experiment Report (experiment five)

Source: Internet
Author: User
Tags decrypt

Course: Java Programming Class: 1351 Name: Wang Wei No.: 20135116

Score: Instructor: Lou Jia Peng Experimental Date: 2015.6.9

Experiment level: Preview degree: Experiment time: 15:20-18:00

Instrument Group: 11 Compulsory/Elective: Experiment Number: 5

Experiment name: TCP transmission and encryption and decryption

Experimental content:

1. Run the textbook on TCP code, pair, one-person server, one-person client;

2. The use of encryption and decryption code package, compile and run code, one person encryption, one person decryption;

3. Integrated code, one person encrypted after sending via TCP;

Note: Encryption uses AES or Des/aes or DES encryption key keys and sends, using the server's public key cryptography/Public key algorithm using RSA or dh/to verify the integrity of sending information using MD5 or SHA3;

4. Use Git for version control.

5. Complete the Blog

Experimental instrument:

Name

Model

Number

Pc

Macbook Air (Win7 system)

1

Pair of partners: 20135109 Gao Yi Tong,

Blog Address: http://www.cnblogs.com/gyt0520/p/4570277.html

I am responsible for the server, she is responsible for client

one , the code

Server:

Import java.net.*;

Import java.io.*;

Import java.security.*;

Import javax.crypto.*;

Import javax.crypto.spec.*;

public class MyServer {

/**

* @param args the command line arguments

*/

public static void Main (String args[]) {

ServerSocket sc = null;

ServerSocket SC1 = null;

Socket Socket=null;

Socket Socket1=null;

try {

sc= new ServerSocket (4431);//Create a server socket

sc1= new ServerSocket (4430);//Create a server socket

SYSTEM.OUT.PRINTLN ("Port number:" + sc.getlocalport ());

SYSTEM.OUT.PRINTLN ("Server 1 has started ...");

SYSTEM.OUT.PRINTLN ("Port number:" + sc1.getlocalport ());

SYSTEM.OUT.PRINTLN ("Server 2 has started ...");

Socket = Sc.accept (); Waiting for client connections

SYSTEM.OUT.PRINTLN ("Connection already established");

Socket1 = Sc1.accept (); Waiting for client connections

System.out.println ("1 connections already established");

Get a reference to a network input stream object

BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));

BufferedReader in1 = new BufferedReader (New InputStreamReader (Socket1.getinputstream ()));

Get a reference to a network output stream object

System.out.print ("Received \ n");

PrintWriter out=new PrintWriter (New BufferedWriter (OutputStreamWriter ()), true);

String aline=in.readline ();//Read data sent by the client

String Aline1=in1.readline ();

System.out.print ("write to file ... \ n");

byte [] bytes = Aline.getbytes ("GB2312");

byte [] bytes1 = Aline1.getbytes ("GB2312");

Aline = new String (bytes, "GB2312");

Aline1 = new String (bytes1, "GB2312");

FileOutputStream s=new FileOutputStream ("Key1.dat");

S.write (bytes);

FileOutputStream s1=new FileOutputStream ("SEnc.dat");

S1.write (BYTES1);

System.out.print ("Write file \ n");

Generate decryption key

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

ObjectInputStream b=new ObjectInputStream (f);

Key k= (Key) B.readobject ();

Byte[] kb=k.getencoded ();

FileOutputStream f2=new FileOutputStream ("Keykb1.dat");

F2.write (KB);

System.out.print ("Generate decryption key \ n");

Print the contents of the key encoding

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

System.out.print (kb[i]+ ",");

}

Decrypt

FileInputStream a=new FileInputStream ("SEnc.dat");

int num=a.available ();

Byte[] Ctext=new Byte[num];

A.read (Ctext);

FileInputStream f1=new FileInputStream ("Keykb1.dat");

int num2=f1.available ();

Byte[] Keykb=new byte[num2];

F1.read (KEYKB);

Secretkeyspec e=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, "GB2312");

System.out.print ("Decrypt ... \ n");

SYSTEM.OUT.PRINTLN (P);

Return

System.out.println ("received from the client information:" +p); Returns results to the client over a network output stream

OUT.PRINTLN (P);

Out.close ();

In.close ();

Sc.close ();

} catch (Exception e) {

System.out.println (e);

}

}

}

Second, problems encountered and how to solve:

When you integrate a program in a package, you are often prone to redefining and variable name occupancy, and you only need to log out of a duplicate definition or change the volume name, but be aware that later references also require changing the variable name

Third, the experimental impression:

Through this experiment, my partner and I have been working hard to piece together pieces of code and run them. The process is complex, but with the help of our classmates and our own constant attempts, there is still some progress. To tell the truth, for me this is not too proficient in the programming of kindergarten-level scholars, network connectivity, transmission, feedback has a lot of difficult places, encountered problems often feel impossible, usually even in the internet to read a lot of information is useless, must seek the help of the gods to solve, therefore, Every time we experiment, the result is the wisdom of many people.

PSP Time

Steps

Take

Percentage

Demand analysis

1h

16.7%

Design

1h 16.7%

Code implementation

2h 33.2%

Test

1h 16.7%

Analysis Summary

1h 16.7%

Java Lab report (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.