20145331 Experiment five Java network programming and security

Source: Internet
Author: User
Tags decrypt using git

Experiment five Java Network programming and security experiment content

1. Master the writing of the socket program;

2. Mastering the use of cryptographic technology;

3. Design the Safe transmission system

4. trooped Partner: 20145333 Zhao Jiaxin Blog Address: http://home.cnblogs.com/u/5301z/

5. Division of Labor: Self-responsible for the service side, partners responsible for the client

Experimental requirements

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. Choosing an asymmetric algorithm for key distribution of symmetric encryption keys

Experimental steps

1, information security transmission:

2. Sender a —————— > receiver B

3, a encryption, with the public key of B

4, b decryption, with B's private key

5, sender A pair of information (clear text) using DES key encryption, the use of RSA encryption of the preceding des key information, and eventually the mixed information to pass. Use the hash function to authenticate the plaintext as well.

6. After receiving the information, the receiver B decrypts the DES key information with RSA, and then decrypts the information with the key information obtained by RSA decryption, and finally we can get the information we want (clear text). The hash function is used to verify the clear text, which is equal to the hash value sent over, and the validation is passed.

Set up a Socket object

Getinetaddress (): Gets the IP address of the remote server.

Getport (): Gets the port of the remote server.

Getlocaladdress (): Gets the client's local IP address.

Getlocalport (): Gets the client's local port.

getInputStream (): Gets the input stream. This method throws IOException if the socket has not yet been connected, or has been closed, or the input stream has been closed through the Shutdowninput () method.

Getoutputstream (): Gets the output stream, which throws IOException if the socket has not yet been connected, or has been closed, or the output stream has been closed through the Shutdownoutput () method.

Experiment Code:
Package Server;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.*;p Ublic class Server {public static void main (String args[]) throws Exception {ServerSocket link = nu   ll   Socket socket = NULL;       try {link = new ServerSocket (8080);//Create Server Socket SYSTEM.OUT.PRINTLN ("Port number:" + link.getlocalport ());       SYSTEM.OUT.PRINTLN ("Server has started ..."); Socket = Link.accept ();       Wait for Client connection System.out.println ("Connection already established");       Get a reference to the network input stream object BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); Obtain a reference to the network output stream object PrintWriter out = new PrintWriter (New BufferedWriter (Socket.getoutputstream ())       ), true);       Decrypt the key of DES with the private key of the server-side RSA String line = In.readline ();       BigInteger cipher = new BigInteger (line); 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 ()//mod n BigInteger m = Cipher.modpow (d, N);//m=d (mod n) System.out.println ("D       = "+ D);       System.out.println ("n=" + N);       System.out.println ("m=" + M);       byte[] keykb = M.tobytearray (); Use des to decrypt ciphertext String ReadLine = In.readline ();//Read the data sent by the client fileinputstream F2 = new FileInputStream ("Keykb1       . Dat ");       int num2 = f2.available ();       byte[] Ctext = Parsehexstr2byte (ReadLine);       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");//Code Conversion SYSTEM.OUT.PRINTLN ("received from client information:" + P); Print decryption result//Use hash function to detect clear text integrity String Aline3 = In.readline ();       String x = p;       MessageDigest m2 = messagedigest.getinstance ("MD5");//Use MD5 algorithm to return the MessageDigest object that implements the specified digest algorithm 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). subst       Ring (6);       } System.out.println (Result);       if (aline3.equals (Result)) {System.out.println ("match succeeded");       } out.println ("Match succeeded");       Out.close ();       In.close ();   Link.close ();   } catch (Exception e) {System.out.println (e); }}//binary conversion to 16 binary, prevents byte[] data loss caused by conversion of number to string type public static String Parsebyte2hexstr (byte buf[]) {stringbuffer sb = new St   Ringbuffer ();       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 ());//Convert lowercase letters in a string into uppercase letters and then add them to the string} return Sb.tostring ();}Convert 16 binary to binary 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;}}

• IP Address:

Experimental experience

This experiment, with Java encryption and decryption, let me have a deeper understanding of Java, but it is very difficult for me to actually compile the code, but also need to build on.

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