Fifth Java Lab Report

Source: Internet
Author: User
Tags decrypt stdin

20144303 "Java program design" The fifth experiment one, the experiment request
    • Run TCP code, one-man server, one-person client
    • Download and decrypt the code, compile the running code, one person to encrypt one person to decrypt
    • Integrated code, one person encrypted after sending via TCP, encryption using DES or Aes,aes or DES encryption key to send, using the server's public key encryption, public key algorithm with RSA or DH
Second, the experimental steps

This experiment I was programming with Shiyu, and I was responsible for designing the client part.

First get the native IP address, enter cmd, and then enter Ipconfig.

The client code is as follows:

Package Net;import Java.math.*;import Java.net.*;import java.io.*;PublicClasscomputetcpclient {PublicStaticvoidMainString srgs[]) {try {To create a socket object that connects the specified port of a specific server socket socket =New Socket ("10.0.6.143",4421);Get the network input stream from the server side BufferedReaderin =New BufferedReader (New InputStreamReader (Socket.getinputstream ()));Obtain the network output W stream printwriter from the client to the server-side output dataout =New PrintWriter (New BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ())),true);Create a keyboard input stream so that the client enters information Eredwriter (new OutputStreamWriter (Socket.getoutputstream ()) from the keyboard, true);Create keyboard input stream BufferedReader stdin =New BufferedReader (New InputStreamReader (System.in)); System.Out.print ("Please enter the data to be sent:"); String s = stdin.readline ();Read the data to be sent from the keyboard string cs =new SEnc (). ENC (s); System. out.println ( "The ciphertext sent to the server is:" + cs); new Enc_rsa (). ENC (); System. out.println ( "the encryption key sent to the server is:" + ck); String result = Digestcalc.hash (s); out.println (CK); out.println (CS); //transmitted over the network to the server out.println (result);} catch (Exception e) {system. Out.println (e);} finally {//stdin.close ();  In.close (); //out.close (); //socket.close ();}}           

The server code is as follows:

Package shiyan5; Import java.net.*; Import java.io.*; Import java.security.*; Import javax.crypto.*; Import javax.crypto.spec.*; Import java.security.interfaces.*; Import java.math.*;PublicClasscomputetcpserver{PublicStaticvoidMainString srgs[]) throws Exception {ServerSocket sc =Null Socket socket=Nulltry {sc=New ServerSocket (1452);Create a server socket system.Out.println ("Port number:" + sc.getlocalport ()); System.Out.println ("The server has started ..."); socket = Sc.accept ();Wait for the client to connect to system.Out.println ("Connection established");Get a reference to a network input stream object BufferedReaderin =New BufferedReader (New InputStreamReader (Socket.getinputstream ()));Get a reference to the network output stream object PrintWriterout=New PrintWriter (New BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ())),true);Decrypt the key of Des 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 ();Decrypt ciphertext by using des string aline=In.readline ();Read data sent by the clientByte[] 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 ("The information received from the client is:" +p);Returns results to the client over a network output streamUsing the hash function to detect plaintext integrity 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);} }PublicStaticByte[] Parsehexstr2byte (String hexstr) {if (Hexstr.length () <1)ReturnNullByte[] result =NewByte[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 * 16 + Low);} return result; 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 ();}}          

Code for the DES plus decryption section:

Package net;PublicClassChange {PublicStatic StringParsebyte2hexstr(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 (); }PublicStaticByte[] Parsehexstr2byte (String hexstr) {if (Hexstr.length () <1)ReturnNullByte[] result =new Byte[hexstr.length ()/2]; For (int i = 0;i< hexstr.length ()/2; i++) { int high = Integer.parseint (hexstr.substring (i*2, I *1), 16); int low = Integer.parseint (hexstr.substring (i*1, i*2), result[i] = (byte) ( High * + low); } return result;}}                 

Experimental results:

Third, the problems encountered in the experiment:

The transfer file location is incorrect, resulting in the system not finding the specified file

Key.txt should be saved in the Java project directory

Four, the experiment experience:

Through this experiment, more skillfully mastered the combination of a few groups of code together, just start to get the code, at a loss, but really used to find it is not so difficult. In addition, in the process of finding the IP address of the network, the content of computer network course is also applied, which increases my interest and enthusiasm to study. and learned how the client and the server to connect and transfer information, it is quite interesting, it is a sense of accomplishment

Fifth 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.