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
Second, the experimental steps:
Server code:
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 (4420);//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 a network input stream object
BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
Get a reference to a network output stream object
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 ();//Read data sent by the client
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); Returns results to the client over a network output stream
/*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[] Mt=m.tobytearray ();
String Aline3=new string (MT, "UTF8"); */
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);
}
}
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;
}
}
Operation Result:
The classmate responsible for the client is Liu Hao: http://www.cnblogs.com/lhc-java/
1, first set up a socket object, used to connect the specified port of a specific server, the input parameters are IP address and port, note that the IP address is the IP address of the server, that is, the host computer running the server's IP address.
2, the BufferedReader object obtains from the server the network input stream, obtains the network output stream from the client to the server output data with the PrintWriter object, uses the BufferedReader object to create the keyboard input stream, So that the client can enter information from the keyboard. The above is written according to the client code of TCP. 3, First use RSA algorithm to encrypt the key of DES, encrypt the server's public key. Sends the encrypted secret key to the server.
4, using DES algorithm to encrypt plaintext, the messaging to the server. The hash function value of the plaintext is then computed and transmitted to the server.
(The above used encryption algorithm, secret key, hash function calculation process are used by the teacher to provide code.) )
5. Read the results from the network input stream and output the results returned from the server.
6, throws the exception part, because inherits is the exception class, therefore the direct output throws the exception.
7, the code is written well, first run the server, and then run the client, the "server has started" Start the client, the connection will be successful display "has established a connection", and then can be sent from the client input data to the server.
Third, the problems encountered in the experiment:
When the encryption key and ciphertext files are packaged and sent, the server cannot differentiate between two data streams and redefine a new port, sending two files from two different ports respectively.
Four, the experiment experience:
This laboratory I and my companion to accomplish together, although des RSA encryption code is ready-made, but in the re-integration is still a certain difficulty, and team cooperation is consistent, coordination. Once the problem occurs, two people must make a more or less adjustment, in the feedback process of the problem is eventually overcome by us, get the desired results, successfully completed the experiment.
Experiment five TCP transmission and encryption