Experiment five Java network programming and security

Source: Internet
Author: User

Beijing Institute of Electronic Technology

Lab Report

Course: Mobile Platform Application development Practice class: 201592 Name: Yang Fengxue No.: 20159213

Score: ___________ Instructor: Lou Jia Peng Experimental Date: 2015.10.25

Experiment Name: Java Network programming and Security

Experiment content: 1, Master Socket program writing 2, mastering the use of cryptographic technology 3, design a secure transmission system

My lab partner is Sun Nan http://www.cnblogs.com/20159214sn/, the client is in my charge and she's in charge of the server.

Experimental steps:

1. Secure transmission based on Java socket

2, give TCP to implement the client and server, pair programming one person responsible for the client one person responsible for the server

3. Use Git for version control

4. Choose symmetric algorithm for data encryption

5. Select asymmetric algorithm for key distribution of symmetric encryption key

6, the choice and the Snake hash algorithm integrity verification.

In this experiment, I used DES encryption and RSA decryption.

Client

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 Client {

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 ("192.168.80.1", 8028);

Network input stream

BufferedReader in = new BufferedReader (New InputStreamReader (

Socket.getinputstream ()));

Network output stream

PrintWriter out = new PrintWriter (New BufferedWriter (

New OutputStreamWriter (Socket.getoutputstream ())), true);

Create keyboard input

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

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

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 ();//Read the node 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;

}

}

Communication between the two machines

Experimental summary

In this experiment, a general understanding of the function and usage of the socket, constantly debugging generation to enable two computers to achieve one-way communication, although the code is not written by themselves but can be achieved through the people are very excited, greatly stimulated the desire for knowledge. The problem with this experiment is that each time the client and the server are connected, there is a problem with the connection, and the replacement port, which wastes time and wastes effort. Here are the problems that arise

After Baidu query, let close the running side but also did not solve the problem.

Another problem is that both computers must be in the same network segment to be able to connect successfully.

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.