Fourth Java Lab Report

Source: Internet
Author: User

Beijing Institute of Electronic Technology (BESTI)

Lab Report

Course Name: Java Programming Experiment class: 1352 Name: Hong Military Study No.: 20135219

Score: Instructor: Lou Jia Peng Experimental Date: 2015.06.09

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

Instrument Group: 19 Compulsory/elective: Elective experiment number: 04

Lab name: Familiarity with the Java development environment

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.

Experimental instrument:

Name Model Number
Pc Mac 1

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 steps:

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:

File Name£ºcomputetcpclient.java
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");
Secretkey K=kg.generatekey ();
Byte[] ptext2=k.getencoded ();
String Kstr=parsebyte2hexstr (KB);


Socket socket = new Socket ("192.168.155.4", 4421);
Socket socket = new Socket ("127.0.0.1", 4421);

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

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

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

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 ();
System.out.println ("e=" +e);
System.out.println ("n=" +n);
Byte ptext2[]=kstr.getbytes ("UTF8");
BigInteger m=new BigInteger (PTEXT2);
BigInteger C=m.modpow (e,n);
System.out.println ("c=" +c);
String cs=c.tostring ();
OUT.PRINTLN (CS);
System.out.print ("Please enter data to be sent:");
String S=stdin.readline ();
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);

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 ();
SYSTEM.OUT.PRINTLN ("The result received from the server is" +STR);
}
catch (Exception e) {
System.out.println (e);
}
finally{
Stdin.close ();
In.close ();
Out.close ();
Socket.close ();
}

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

Server:

File Name£ºcomputetcpserver.java
Import java.net.*;
Import java.io.*;
Import java.security.*;
Import java.security.spec.*;
Import javax.crypto.*;
Import javax.crypto.spec.*;
Import javax.crypto.interfaces.*;
Import java.security.interfaces.*;
Import java.math.*;
public class computetcpserver{
public static void Main (String srgs[]) throws Exception {
ServerSocket sc = null;
Socket Socket=null;
try {
sc= new ServerSocket (4421);
SYSTEM.OUT.PRINTLN ("Port number:" + sc.getlocalport ());
SYSTEM.OUT.PRINTLN ("Server has started ...");
Socket = Sc.accept ();
SYSTEM.OUT.PRINTLN ("Connection already established");

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

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

String Aline2=in.readline ();
BigInteger c=new BigInteger (aline2);
FileInputStream f=new FileInputStream ("Skey_rsa_priv.dat");
ObjectInputStream b=new ObjectInputStream (f);
Rsaprivatekey prk= (Rsaprivatekey) b.readobject ();
BigInteger d=prk.getprivateexponent ();
BigInteger N=prk.getmodulus ();
System.out.println ("d=" +d);
System.out.println ("n=" +n);
BigInteger M=c.modpow (d,n);
System.out.println ("m=" +m);
Byte[] Keykb=m.tobytearray ();
String Aline3=new string (MT, "UTF8");
String aline3=parsebyte2hexstr (byte buf[]);

String Aline=in.readline ();
FileInputStream f2=new FileInputStream ("Keykb1.dat");
int num2=f2.available ();
Byte[] Keykb=new byte[num2];
F2.read (KEYKB);
Byte[] Ctext=parsehexstr2byte (Aline);
Key k=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, "UTF8");
System.out.println ("received from the client information:" +p);

String Aline3=in.readline ();
String x=p;
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);

if (aline3.equals (result)) {
System.out.println ("Match succeeded!");
}

Out.println ("Match succeeded!");
Out.close ();
In.close ();
Sc.close ();
} catch (Exception e) {
System.out.println (e);
}
}
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;
}
}

Experimental results:

The problems encountered in the experiment and their solutions:

1. When the DES or AES algorithm is added to the socket file to decrypt the input and output, I never know how to combine the cipher algorithm with the original program to encrypt the acquisition input.

After the research on the cipher algorithm, using many libraries and methods from Java, using the bit array to store the results, but also to make the conversion, and finally complete the encryption and decryption and transmission work.

As follows:

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

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

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

2. Client and server connection to the socket host understanding is not enough, before the connection failed, and then abandoned the book Getbyname method, set up 127.0.0.1 native address, can also realize the communication between the two machines.

Third, the experiment Experience:

Steps

Time-consuming (min)

Percentage

Demand analysis

20

15%

Design

20

15%

Code implementation

60

66g

Test

20

15%

Analysis Summary

10

8%

Fourth Java Lab Report

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.