Java Network Programming and security

Source: Internet
Author: User
Tags stdin

First, the contents of the experiment:

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

My pairing partner is 20135116 Wang Yuyi (blog address is http://www.cnblogs.com/wwy-20135116/ )

I am responsible for the client, Wang Wei is responsible for the server

Second, the experimental steps

1. Secure Transmission of information:

      

Sender a —————— > receiver B

A when encrypting, use B's public key

b When decrypting, use the private key of B

Sender A pairs the information (clear text) with Des key encryption, using RSA to encrypt the preceding des key information, and eventually the mixed information is passed. Use the hash function to authenticate the plaintext as well.

After receiving the information, the receiver B decrypts the DES key information with RSA, and then decrypts the information of the key information obtained by RSA decryption, and finally we can get the information we want (plaintext). A hash function is used to verify the clear text, and the Sent

Import java.net.*;

Import java.io.*;

Import javax.crypto.*;

Import java.security.*;

Import Javax.crypto.Cipher;

public class MyClient {

/**

* @param args the command line arguments

*/

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

Generated with the secret key

Keygenerator kg=keygenerator.getinstance ("Desede");

Kg.init (168);

Secretkey K=kg.generatekey ();

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

ObjectOutputStream b=new ObjectOutputStream (f);

B.writeobject (k);

Encrypted input

BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in));

System.out.print ("Please enter data to be sent:");

String Srt=stdin.readline ();

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

ObjectInputStream a=new ObjectInputStream (d);

Key e= (Key) A.readobject ();

Cipher cp=cipher.getinstance ("Desede");

Cp.init (Cipher.encrypt_mode, E);

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

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

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

}

System.out.println ("");

Byte ctext[]=cp.dofinal (ptext);

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

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

}

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

F2.write (Ctext);

Send

try {

To create a socket object that connects to a specified port on a specific server

Socket socket = new Socket ("192.168.253.1", 4431);

Socket SOCKET1 = new socket ("192.168.253.1", 4430);

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);

PrintWriter out1=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));

System.out.print ("read files ... \ n");

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

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

String Str=stdin.readline (); Read the data to be sent from the keyboard

String str;

Out.println (F1); Transfer over the network to the server

Out1.println (F3);

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

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 v) {

System.out.println (v);

}

finally{

Stdin.close ();

In.close ();

Out.close ();

Socket.close ();

}

}

}

Experiment:

Iv. Summary of the experiment

This experiment was done with my pair of partners. At first we did not have any experimental ideas, and then we through the teacher and made to experiment with the students to comb clear the clue, clear the experimental steps and experimental requirements. the combination of code for us is a big challenge, not only test our careful degree, but also examine the DES algorithm and RSA encryption, in the network connection, transmission, feedback also has a lot of difficult places, encountered problems often need to surf the internet a lot of information to find the right solution, Compile successfully after continuous debugging, improvement, debugging and improvement of the code.

Through this experiment, I and pairs of partners on the DES Algorithm and RSA encryption more proficient, in the face of difficulties we also know how to look at some of the "Big God" blog looking for answers, which for us is not only a challenge, but also a breakthrough in the process of self.

Java Network Programming and security

Related Article

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.