20145311 experimental Four "Java Network programming and security"

Source: Internet
Author: User
Tags decrypt

20145311 experiment Four "Java Network programming and security" program design process experiment content

• Master the writing of the socket program;
• Mastering the use of cryptographic techniques;
• Design of Secure transmission system • Use encryption and decryption code package, compile and run code, one person encrypted, one person decryption;
• Integrated code, one person encrypted and sent via TCP;

Experimental steps

Before that, there was an example of a socket connection: I did an experiment with Baidu.

The following is a two-person partnership for RSA Encryption:

First set up a socket object, used to connect the specified port of a specific server (I am responsible for the server side, Zheng Kaijie is responsible for the client, so that is the input of my side of the IP address), the input parameter is the IP address and port (the port number should be arbitrary, two people the same can be), Note The IP address is the IP address of the server, which is the IP address of the host that is running the server.

At first we always configured the wrong, because the IP address entered is not correct, so this is always the case

This is always the case, the client side how to run, the server has not changed a bit

And then we re-examined it.

Enter the address of the workgroup IPv4 that we are connected to, then we can match it correctly.

The next step is to use the RSA algorithm for encryption and decryption, where we want the RSA algorithm's private key and the public key on the server side and the client, so that in the case of the lock will not find the specified file error

This is the one I put RSA's private key under my project

This allows the code to pass:

Package com.company;

Import java.net.*;
Import java.io.*;
Import java.security.*;
Import javax.crypto.*;
Import javax.crypto.spec.*;
Import java.security.interfaces.*;
Import java.math.*;

public class main{
public static void Main (String srgs[]) throws Exception
{
ServerSocket sc = null;
Socket Socket=null;
Try
{
sc= new ServerSocket (9090);//Create a server socket
SYSTEM.OUT.PRINTLN ("Port number:" + sc.getlocalport ());
SYSTEM.OUT.PRINTLN ("Server has started ...");
Socket = Sc.accept (); Waiting for client connections
SYSTEM.OUT.PRINTLN ("Connection already established");//Get a reference to the network input stream object
BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));//Get a reference to the network output stream object
PrintWriter out=new PrintWriter (New BufferedWriter (OutputStreamWriter ()), true);
Decryption of DES keys using the private key of the server-side RSA
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 ();
BigInteger M=c.modpow (d,n);
Byte[] Keykb=m.tobytearray ();
Use Des to decrypt ciphertext
String aline=in.readline ();//Read data sent by the client
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); Returns results to the client over a network output stream
Detecting plaintext integrity using the hash function
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 success");
}
Out.println ("match success");
Out.close ();
In.close ();
Sc.close ();
} catch (Exception e) {
System.out.println (e);
}
}
Hexadecimal and decimal conversions
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;
}
}

Final Result:

Experiment Summary and experience

This experiment is mainly network programming and cryptography encryption and decryption of a combination of two aspects are closely related to our professional, through the study of the blog, I learned some basic knowledge of network programming, the contact is still somewhat difficult, so I refer to other people write code, The main or the use of some classes is not very skilled, then there is how to encrypt the plaintext to decrypt the ciphertext, how to use the Java language to achieve clear text, ciphertext, keys, etc., or need to learn further.

PSP (Personal software Process)
Steps Time Consuming percentage
Demand analysis 15min 15
Design 20min 20
Code implementation 20min 20
Test 20min 20
Analysis Summary 25min

25

20145311 experimental Four "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.