Experiment five Java Network Programming and Security Experiment report 20135232 Wang Yue

Source: Internet
Author: User
Tags decrypt

Beijing Institute of Electronic Technology (BESTI)

Real Inspection report

Lesson: Java Programs and Design class: 1352

Name: Wangyue

Study No.: 20135232

Score: Instructor: Lou Jia Peng Experimental Date: 2015.6.9

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

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

Experiment Name: Java Network programming and security

Experimental purposes and requirements: pair programming, to achieve the client and server data transmission and reception, the implementation of encryption and decryption and verify the value of the hash function.

Experimental instrument:

Name

Model

Number

Pc

TOSHIBA

1

Eclipse

LUNA

1

"Experimental Content"

1. Use the TCP code on the book to implement the server and client.

2. Client-to-server connectivity

3. Enter plaintext in the client, using DES algorithm encryption, des secret key is encrypted with the public key of the server in the RSA public key password, calculates the hash function value of the plaintext, and transmits it to the client

4. The client uses the RSA public key password in the server's private key to decrypt the DES, the secret key, with the secret key to decrypt the ciphertext, obtains the clear text. Calculates the value of the hash function for the clear text, checks whether it is consistent with the transmission, and if so, indicates a successful match.

"Experimental Steps"

I'm in charge of the server section.

My pairing partner Bérénice Angremy's Blog home page: http://www.cnblogs.com/heiguirenxi/

First set up a socket object, used to connect the specified port of a specific server, the input parameters are IP address and port, note that the IP address is the IP address of the server, that is, the host computer running the server IP address.

How do I check the IP address of the host? You can enter ipconfig on the command line. As shown in the following:

Then use the BufferedReader object to obtain the network input stream from the server, use the PrintWriter object to obtain the network output stream from the client to the server, and create the keyboard input stream with the BufferedReader object so that the client can enter information from the keyboard. The above is written according to the client code of TCP.

The next step is to use the RSA algorithm to encrypt the key of DES, encrypt the server's public key. Sends the encrypted secret key to the server.

The DES algorithm is then used to encrypt the plaintext and the messaging to the server.

The hash function value of the plaintext is then computed and transmitted to the server.

The above used encryption algorithm, secret key, hash function calculation process are used by the teacher to provide code.

Finally, the results are read from the network input stream, and the results returned from the server are output.

Throws an exception in the exception section because it inherits from the exception class, so the output is thrown directly.

After the code is written, run the server, run the client, and the client is started after the server has started, and the connection will display "already established", and then you can send the data from the client input to the server.

Server code:

Import Java.io.BufferedInputStream;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;

Import Java.security.SecureRandom;

Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESKeySpec;
public class Servertest {
int port = 8821;

void Start () {
Socket s = null;
try {
ServerSocket ss = new ServerSocket (port); Create a ServerSocket socket object and bind to port 8821
while (true) {
Select the file to transfer
String FilePath = "c:\\users\\ tianyu clear \\Desktop\\ server \\jiami.txt";
File fi = new file (FilePath); Creates a new File instance by converting the given pathname string to an abstract path name
System.out.println ("File Length:" + (int) fi.length ());

s = ss.accept ();
SYSTEM.OUT.PRINTLN ("Establish socket link");
DataInputStream dis = new DataInputStream (New Bufferedinputstream (S.getinputstream ())); DataInputStream: Creates a datainputstream with the specified underlying inputstream;
Dis.readbyte (); Returns the next byte of this input stream, expressed as a signed 8-bit byte.

DataInputStream fis = new DataInputStream (new Bufferedinputstream (New FileInputStream (FilePath)));
DataOutputStream PS = new DataOutputStream (S.getoutputstream ());//Create a new data output stream to write data to the specified underlying output stream


Ps.writeutf (Fi.getname ());
Ps.flush ();
Ps.writelong ((Long) fi.length ());
Ps.flush ();

int buffersize = 8192;//buffer, 1k
byte[] buf = new Byte[buffersize];

while (true) {
int read = 0;
if (FIS! = null) {
Read = Fis.read (BUF);
}

if (read = =-1) {
Break
}
Ps.write (buf, 0, read);
}
Ps.flush ();//until the socket expires, resulting in incomplete data.
Fis.close ();
S.close ();
System.out.println ("File transfer complete");
}

} catch (Exception e) {
E.printstacktrace ();
}

"Experimental Experience"

This experiment is mainly to practice in the existing code on how to implement the function in the same program, the teacher before the experiment has given the TCP code and cryptographic algorithm encryption and decryption method, which has brought great convenience to our programming, so we have to do is familiar with the meaning of the code represented, To be self-made and able to master the use.

"Problems encountered during the experiment and solutions"

In this experiment, I mainly encountered two problems, one is how to integrate the code together, and the other is how to implement the server and client connection. We got the correct IP address through the command line, and finally the connection was successfully realized.

Experiment five Java Network Programming and Security Experiment report 20135232 Wang Yue

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.