Experiment five Java network programming and security

Source: Internet
Author: User

Lesson: Java Programming Experiment class: 1352 name: Yu Jiacen No.: 20135206

Score: Instructor: Lou Jia Peng Experiment Date and time: 2015.06.09

Seat number: compulsory/elective: Elective experiment number: 05

Experiment Name: Agile Development and XP experiment

Experimental instrument:

Name

Model

Number

Computer

2

Laboratory Building

2

Experimental PARTNER: Wanzihui 20135206http://www.cnblogs.com/midori/

Experimental content:

1. First run the textbook on the TCP code, one-person server, one-person client.

2. Download the decryption code, compile the running code first, one person encrypts one person to decrypt.

3. Then integrate the code, one person encrypted after passing through the TCP send, encryption using AES or DES encryption key keys sent, using the server's public key encryption.

Public key algorithms use RSA or DH

Integrity verification of sending information using MD5 or SHA3

Steps

Take

Percentage

Demand analysis

50min

25%

Design

30min

15%

Code implementation

50min

25%

Test

20min

10%

Analysis Summary

50min

25%

Experimental steps:

1. Set up the socket object, connect to the server to specify the port, enter the IP address in the specified location, and ensure that the two people enter the same port to achieve the interconnection

2.BufferedReader: Get the network input stream

PrintWriter: Get the network output stream

BufferedReader: Creating a keyboard input stream

3. Use the RSA algorithm given by the teacher to encrypt the key of DES, encrypt the plaintext with the encryption algorithm given by the teacher, and transfer the ciphertext to the server. The hash function algorithm is used to calculate the value of hash function and transfer it to the server.

4. Read the results from the network input stream and output

5. Run the server first, then run the client, and after the connection is successful, the input data is sent to the server

The following are the procedures for running and transferring data for a client:

Experiment Code:

Import java.net.*;
Import java.io.*;
Import java.security.*;
Import javax.crypto.*;
Import javax.crypto.spec.*;
Import java.security.spec.*;
Import javax.crypto.interfaces.*;
Import java.security.interfaces.*;
Import java.math.*;
public class Computetcpclient {
public static void Main (String srgs[]) throws exception{
try {
Keygenerator kg=keygenerator.getinstance ("Desede");
Kg.init (168);
Secretkey K=kg.generatekey ();
Byte[] ptext2=k.getencoded ();


To create a socket object that connects to a specified port on a specific server
Socket socket = new Socket ("172.28.202.3", 8001);
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);
Create a keyboard input stream so that clients can enter information from the keyboard
BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in));

Use the public key of the server-side RSA to encrypt the key of Des
FileInputStream f3=new FileInputStream ("Skey_rsa_pub.dat");
ObjectInputStream b2=new ObjectInputStream (F3);
Rsapublickey pbk= (Rsapublickey) b2.readobject ();
BigInteger e=pbk.getpublicexponent ();
BigInteger N=pbk.getmodulus ();
BigInteger m=new BigInteger (PTEXT2);
BigInteger C=m.modpow (e,n);
String cs=c.tostring ();
OUT.PRINTLN (CS); Transfer over the network to the server

System.out.print ("Please enter data to be sent:");
String S=stdin.readline (); Read the data to be sent from the keyboard
Cipher cp=cipher.getinstance ("Desede");
Cp.init (Cipher.encrypt_mode, k);
Byte ptext[]=s.getbytes ("UTF8");
Byte ctext[]=cp.dofinal (ptext);
String str=parsebyte2hexstr (Ctext);
Out.println (str); Transfer over the network to the server

Pass the hash value of the client plaintext to ciphertext
String x=s;
MessageDigest m2=messagedigest.getinstance ("MD5");
M2.update (X.getbytes ());
byte a[]=m2.digest ();
String result= "";
for (int i=0; i<a.length; i++) {
Result+=integer.tohexstring (0X000000FF & A[i]) |
0XFFFFFF00). substring (6);
}
SYSTEM.OUT.PRINTLN (result);
OUT.PRINTLN (result);
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 e) {
System.out.println (e);
}
finally{
}

}

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

Experiment Experience:

More proficient in the combination of a few groups of code, just start to get the code, at a loss, but really used to find it is not so difficult

Learned how clients connect and transfer information to and from a server. At first, we couldn't even get to the wrong IP address, and the real IP address should be found through the command line input ipconfig, and the port must be consistent.

After the connection, the message to each other, the feeling is quite interesting, a sense of accomplishment

Experiment five Java network programming and security

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.