Experiment five Java network programming and security

Source: Internet
Author: User
Tags using git

First, the contents of the experiment

1. Master the socket program writing;

2. Mastering the use of cryptographic technology;

3. Design a secure transmission system.

Second, the experimental steps

1. Secure transmission based on Java sockets

2. Implementation of client and server based on TCP, pair programming one person is responsible for the client, one person is responsible for the server

3. Using Git for version control

4. Select the symmetric algorithm for data encryption and decryption.

5. Select the asymmetric algorithm to distribute the key for the symmetric encryption key.

6. Select the appropriate hash algorithm for integrity verification.

7. Select the appropriate algorithm to sign/verify the hash value.

Third, design ideas

At first, my teammates and I saw this experimental topic is helpless, one is not very familiar with the IO stream file read and file storage location, two, even in class understand the client and server encryption and decryption between the message and the process of transmission is not know how to apply Java code to implement it.

In the experimental class, we consulted the teacher's approximate operation, and carefully study the book on the knowledge of Io stream and the application of the hash function to verify the correctness of the process of implementation, began to operate.

My trooped partner is Song Ning: http://home.cnblogs.com/blog/

1. First, the code that runs the server and the client.

Client

    public static void Main (string[] args) throws exception{        inetaddress addr=inetaddress.getbyname ("192.168.252.1");        System.out.println ("addr=" +addr);        Socket socket=new socket (addr,8080);        try{            System.out.println ("socket=" +socket);            BufferedReader in=new BufferedReader (New InputStreamReader (Socket.getinputstream ()));            PrintWriter out=new PrintWriter (New BufferedWriter (OutputStreamWriter ()), true);            for (int i=0;i<10;i++) {                out.println ("Howdy" +i);                String str=in.readline ();                System.out.println (str);                            }

2. Enter the plaintext "Hello world!" in the client-side program The DES key is then generated using the random key generator and the key application IO stream is stored in the file Keykb1.dat.

            String s= "Hello world!";                        Keygenerator kg=keygenerator.getinstance ("Desede");            Kg.init (168);             Secretkey K=kg.generatekey ();            Byte[] kb=k.getencoded ();            FileOutputStream  fk=new fileoutputstream ("Keykb1.dat");            Fk.write (KB);            for (int i=0;i<kb.length;i++) {                 System.out.print (kb[i]+ ",");//Print extension key            }

3. Then print the plaintext, convert to UTF8 format, and encrypt the plaintext with the secret key.

Cipher cp=cipher.getinstance ("Desede");            Cp.init (Cipher.encrypt_mode, k);            Byte ptext[]=s.getbytes ("UTF8");            for (int i=0;i<ptext.length;i++) {                System.out.print (ptext[i]+ ",");            }                        System.out.println ("");            Byte ctext[]=cp.dofinal (ptext);            for (int i=0;i<ctext.length;i++) {                 System.out.print (Ctext[i] + ",");            }

4. Pass the ciphertext to the server

            FileOutputStream f2=new FileOutputStream ("SEnc.dat");            F2.write (Ctext);

5. Server application random key generator generates the public and private key of the server

Keypairgenerator KPG = keypairgenerator.getinstance ("RSA");                Kpg.initialize (1024x768);                KeyPair KP = Kpg.genkeypair ();                PublicKey Pbkey = Kp.getpublic ();                Privatekey Prkey = Kp.getprivate ();                FileOutputStream f1 = new FileOutputStream (                        "Skey_rsa_pub.dat");                ObjectOutputStream B1 = new ObjectOutputStream (F1);                B1.writeobject (Pbkey);                FileOutputStream F2 = new FileOutputStream (                        "Skey_rsa_priv.dat");                ObjectOutputStream b2 = new ObjectOutputStream (F2);                B2.writeobject (Prkey);

6. Client creates a public key for the server and passes the key encryption to the server

FileInputStream f=new FileInputStream ("Skey_rsa_pub.dat");            ObjectInputStream b=new ObjectInputStream (f);            Rsapublickey  pbk= (Rsapublickey) b.readobject ();            BigInteger e=pbk.getpublicexponent ();            BigInteger N=pbk.getmodulus ();            System.out.println ("e=" +e);            System.out.println ("n=" +n);            Byte ptext1[]=s.getbytes ("UTF8");            BigInteger m=new BigInteger (PTEXT1);            BigInteger C=m.modpow (e,n);            System.out.println ("c=" +c);            String cs=c.tostring ();            BufferedWriter out1=               New BufferedWriter (New OutputStreamWriter (                new FileOutputStream ("Enc_rsa.dat"));            Out1.write (Cs,0,cs.length ());            Out1.close ();

7. The server uses the server's private key to decrypt the key of the DES that the client transmits

 BufferedReader in1= New BufferedReader (New InputStreamReader new FileInputStream ("ENC_RSA.D                At "));                String Ctext=in1.readline ();                BigInteger c=new BigInteger (ctext);                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 ();                System.out.println ("Plainkey is");                for (int i=0;i<mt.length;i++) {System.out.print ((char) mt[i]); }                

8. The server uses the secret key generated by the previous step to decrypt the ciphertext generated by Des

                FileInputStream fsd=new FileInputStream ("SEnc.dat");                int num=fsd.available ();                Byte[] Ctextd=new Byte[num];                          Fsd.read (CTEXTD);                FileInputStream  fsd2=new fileinputstream ("Keykb1.dat");                int num2=fsd2.available ();                Byte[] Keykb=new byte[num2];                          Fsd2.read (KEYKB);                Secretkeyspec k=new  secretkeyspec (keykb, "Desede");               Cipher cp=cipher.getinstance ("Desede");               Cp.init (Cipher.decrypt_mode, k);               byte []ptext=cp.dofinal (CTEXTD);               String P=new string (Ptext, "UTF8");               SYSTEM.OUT.PRINTLN ("The server received the message" +p);

9. Server, client application hash function verification and decryption correctness

                    SYSTEM.OUT.PRINTLN (result); */               String ha = In.readline ();               String sa = hash (p);                   if (ha.equals (SA))                       System.out.println ("correct");                                                  } finally {                System.out.println ("closing.");                Socket.close ();            }                    } finally {            s.close ();        }
            String x=s;             MessageDigest md5=messagedigest.getinstance ("MD5");             Md5.update (X.getbytes ());             byte smd5[]=md5.digest ();             String result= "";             for (int i=0; i<smd5.length; i++) {                result+=integer.tohexstring (0x000000ff & Smd5[i]) |                     0XFFFFFF00). substring (6);             }             SYSTEM.OUT.PRINTLN (result);

Iv. Results of the experiment

Client

Server

Two pictures verify that the hash value is the same, and that the decryption succeeds.

V. Problems encountered

Two people to test, the server and the client has successfully connected, but the server does not display the results, when they are connected, the server results will appear, but will be overwritten by the client results, but can quickly intercept the results, and the results are correct.

Vi. Solutions

So far, debugging many times did not find a workable solution, check the code tomorrow to consult the teacher.

Seven, experimental analysis

The experiment from the whole class will not do, to a little research, a little study, the final make not perfect, but the result of the correct code is still very rewarding. I learned the IO stream and the storage and reading of the files. And in this experiment, I don't know much about every single statement that a teacher has to pack into our cryptographic algorithms, but the ability to integrate them into a single piece of code is mastered, and the most important thing to learn about Java is to master the methods and architectures, which are not fully understood or applied to the specific functions and statements.

Viii. Time of statistics

time-consuming

TD valign= "Top" >

steps

percentage

demand analysis

 3h

 27.3%

design

/ td>
 1h 9.1%

generation Code implementation

 2h 18.2%

test

 4h 36.4%
< P align= "Center" > Analysis summary

 1h  9.1%

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.