Beijing Institute of Electronic Technology (BESTI)
Real Inspection report
Lesson: Java Programs and Design class: 1352
Name: Shang Jia
Study No.: 20135202 20135236
Score: Instructor: Lou Jia Peng Experimental Date: 2015.6.9
Experiment level: Preview degree: Experiment time: 15:30-18:00
Instrument Group: Compulsory/Elective: Elective experiment number: 5
Experiment Name:Java Network programming and security
Experimental purposes and requirements: pair programming, to achieve the client and server data transmission and reception, the implementation of encryption and decryption and verify the value of the Hash function.
Experimental instrument:
Name |
Model |
Number |
Pc |
TOSHIBA |
1 |
Eclipse |
LUNA |
1 |
"Experimental Content"
1. Use the TCP code on the book to implement the server and client.
2. Client-to-server connectivity
3. Enter plaintext in the client, using DES algorithm encryption, des secret key is encrypted with the public key of the server in the RSA public key password, calculates the hash function value of the plaintext, and transmits it to the client
4. The client uses the RSA public key password in the server's private key to decrypt the DES, the secret key, with the secret key to decrypt the ciphertext, obtains the clear text. Calculates the value of the hash function for the clear text, checks whether it is consistent with the transmission, and if so, indicates a successful match.
"Experimental Steps"
I'm programming with 20135236 Jia pairs, and I'm designing parts of the client.
Jia's Blog home page: http://www.cnblogs.com/javajy/
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 IP address.
How do I check the IP address of the host? You can enter ipconfig on the command line. As shown in the following:
Then use the BufferedReader object to obtain the network input stream from the server, use the PrintWriter object to obtain the network output stream from the client to the server, and create the keyboard input stream with the BufferedReader object so that the client can enter information from the keyboard. The above is written according to the client code of TCP.
The next step is to use the RSA algorithm to encrypt the key of DES, encrypt the server's public key. Sends the encrypted secret key to the server.
The DES algorithm is then used to encrypt the plaintext and 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.
Finally, the results are read from the network input stream, and the results returned from the server are output.
Throws an exception in the exception section because it inherits from the exception class, so the output is thrown directly.
After the code is written, run the server, run the client, and the client is started after the server has started, and the connection will display "already established", and then you can send the data from the client input to the server.
Client code:
Importjava.net.*;ImportJava.io.*;ImportJava.security.*;ImportJavax.crypto.*;ImportJavax.crypto.spec.*;ImportJava.security.spec.*;Importjavax.crypto.interfaces.*;Importjava.security.interfaces.*;Importjava.math.*; Public classtcp_client { Public Static voidMain (String srgs[])throwsException {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 serverSocket socket =NewSocket ("192.168.253.1", 8030);//here is the server's IP address and port number, the port number should be careful and the server consistent. //get the network input stream from the server sideBufferedReader in =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //obtain a network output stream from the client to the server-side output dataPrintWriter out =NewPrintWriter (NewBufferedWriter (NewOutputStreamWriter (Socket.getoutputstream ())),true); //Create a keyboard input stream so that clients can enter information from the keyboardBufferedReader stdin =NewBufferedReader (NewInputStreamReader (system.in)); //RSA algorithm that encrypts des keys using the server-side public keyFileInputStream F3 =NewFileInputStream ("Skey_rsa_pub.dat"); ObjectInputStream B2=NewObjectInputStream (F3); Rsapublickey pbk=(Rsapublickey) b2.readobject (); BigInteger e=pbk.getpublicexponent (); BigInteger N=Pbk.getmodulus (); BigInteger m=NewBigInteger (PTEXT2); BigInteger C=M.modpow (e, N); String CS=c.tostring (); OUT.PRINTLN (CS); //transfer encrypted secret keys to the server over the network//encrypt plaintext with des to get ciphertextSystem.out.print ("Please enter data to be sent:"); String s= Stdin.readline ();//read the data to be sent from the keyboardCipher CP = Cipher.getinstance ("Desede"); Cp.init (Cipher.encrypt_mode, k); bytePtext[] = s.getbytes ("UTF8"); byteCtext[] =cp.dofinal (Ptext); String Str=parsebyte2hexstr (Ctext); Out.println (str); //transferring ciphertext to the server over the network//Send the client plaintext hash value to the serverString x =s; MessageDigest m2= Messagedigest.getinstance ("MD5"); M2.update (X.getbytes ()); byteA[] =m2.digest (); String result= ""; for(inti = 0; i < a.length; i++) {result+ = Integer.tohexstring ((0x000000ff & A[i]) | 0xffffff00). SUBSTRING (6); } System.out.println (Result); OUT.PRINTLN (result);//passing the hash function value of plaintext to the server over the networkStr= In.readline ();//read results from the network input streamSYSTEM.OUT.PRINTLN ("The result received from the server is:" + str);//results returned by the output server } Catch(Exception e) {System.out.println (e);//Output Exception } finally { } } Public StaticString Parsebyte2hexstr (bytebuf[]) {StringBuffer SB=NewStringBuffer (); for(inti = 0; i < buf.length; i++) {String hex= Integer.tohexstring (Buf[i] & 0xFF); if(hex.length () = = 1) {hex= ' 0 ' +Hex; } sb.append (Hex.touppercase ()); } returnsb.tostring (); } Public Static byte[] Parsehexstr2byte (String hexstr) {if(Hexstr.length () < 1) return NULL; byte[] result =New byte[Hexstr.length ()/2]; for(inti = 0; I < Hexstr.length ()/2; i++) { intHigh = Integer.parseint (Hexstr.substring (i * 2, I * 2 + 1), 16); intLow = Integer.parseint (Hexstr.substring (i * 2 + 1, I * 2 + 2), 16); Result[i]= (byte) (High * 16 +Low ); } returnresult; }}
"Experimental Experience"
- The understanding of the experiment process, the understanding of the knowledge point in the experiment instruction book.
This experiment is mainly to practice in the existing code on how to implement the function in the same program, the teacher before the experiment has given the TCP code and cryptographic algorithm encryption and decryption method, which has brought great convenience to our programming, so we have to do is familiar with the meaning of the code represented, To be self-made and able to master the use.
2. Problems encountered during the experiment and solutions.
In this experiment, I mainly encountered two problems, one is how to integrate the code together, and the other is how to implement the server and client connection. At the beginning of our IP address is in the Baidu search box input IP This method, how will show the connection time-out, and later found that this method to find out the IP is wrong, and then we get the correct IP address through the command line, finally successful implementation of the connection.
Experiment five Java network programming and security