20172328 "Program design and data structure" experimental Five cryptography Algorithm application report

Source: Internet
Author: User
Tags md5 asymmetric encryption

20172328 "Program design and data structure" experimental Five cryptography Algorithm application report

Program: Program design and data structure
Class: 1723
Name: Li Xinyu
Study No.: 20172328
Lab Teacher: Wang Zhiqiang teacher
Date of experiment: June 18, 2018
Compulsory Elective: compulsory

First, the requirements of the experiment content
    • Lab 1:
    1. Reference Http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA
    2. The function Convert.java of the expression of infix expression to suffix in the pair realization
    3. The knot pair implements the function of evaluating the suffix expression in an expression obtained from the above function, calling Comparision.java
    4. Upload test code run results and code cloud Links
    • Lab 2:
    1. Pay attention to the end of the responsibility, will pass the test to prove that no problem
    2. Java Socket-based client/server function, transmission mode with TCP
    3. The client lets the user enter infix expression, then converts the function of the infix expression call Convert.java to the suffix expression, and sends the suffix expression over the network to the server.
    4. The server receives the suffix expression, calls the function of the Comparision.java to compute the value of the suffix expression, and sends the result to the client
    5. The client displays the results sent by the server.
    6. Upload test results and code cloud Links
    • Lab 3:
    1. Pay attention to the end of the responsibility, will pass the test to prove that no problem
    2. Java socket-based client/server function, transmission mode with TCP
    3. The client lets the user enter infix expression, then converts the function of the infix expression call Convert.java to the suffix expression, encrypts the suffix expression with 3DES or AES algorithm and sends the ciphertext to the server via the network .
    4. After the server receives the suffix expression expression, decrypts (and the client negotiates the key, which can be saved in an array), and then calls the function of the Comparision.java to compute the value of the suffix expression and sends the result to the client
    5. The client displays the results sent by the server.
    6. Upload test results and code cloud Links
    • Lab 4:
    1. Pay attention to the end of the responsibility, will pass the test to prove that no problem
    2. Java socket-based client/server function, transmission mode with TCP
    3. The client lets the user enter infix expression, then converts the function of the infix expression call Convert.java to the suffix expression, and uses the suffix expression to encrypt the ciphertext to the server through the network using 3DES or AES algorithm.
    4. Client and server use DH algorithm for key exchange of 3DES or AES algorithm
    5. After the server receives the suffix expression expression, decrypts, and then comparision.java the function to calculate the suffix expression value, sends the result to the client
    6. The client displays the results sent by the server.
    7. Upload test results and code cloud Links
    • Lab 5:
    1. Pay attention to the end of the responsibility, will pass the test to prove that no problem
    2. Java socket-based client/server function, transmission mode with TCP
    3. The client lets the user enter infix expression, and then converts the function of the infix expression call Convert.java to the suffix expression, and the suffix expression is encrypted with 3DES or AES algorithm to send the MD5 of ciphertext and plaintext to the server by the network.
    4. Client and server use DH algorithm for key exchange of 3DES or AES algorithm
    5. After the server receives the suffix expression expression, decrypts, decrypts the MD5 value of the plaintext, compares it to the MD5 from the client, and then calls the Comparision.java function to compute the value of the suffix expression and sends the result to the client
    6. The client displays the results sent by the server.
    7. Upload test results and code cloud Links
Second, the experimental process and results
    • Experiment 1: Prior to the experiment in the arithmetic has been achieved, the direct use of the results.

    • Experiment 2: And the knot pair small partner division, the client locates the correct server IP address, and matches the same port.
      The client is responsible for letting the user enter infix expression and go to the suffix expression to the server, the server reads through the byte stream, obtains the corresponding suffix expression, then evaluates, returns the result to the client.

    • Experiment 3: First refer to Lou Teacher's blog, in-depth understanding of the use of symmetric key and how to write the key to the file and then use the key pair suffix expression to encrypt, and then send the ciphertext to the server, the server is decrypted by the same key, and then use the method of suffix expression evaluation to return the results to the client.

    • Experiment 4: Each party establishes its own key and private key, the client obtains its own private key and the other party's public key to encrypt the same suffix expression, encrypt it to the server, the server decrypts it with its private key and the other's public key, then evaluates the decrypted suffix expression and returns it to the client.
    • A few things to note:
    • If A and B want to secretly transfer large amounts of data, a party (such as a) to create a public private key pair, public key publicity, the other party (such as B) to create a symmetric key, and then use A's public key to encrypt the symmetric key, passed to A,a after receiving with their own private key decryption, to obtain a symmetric key, A symmetric key can be used to encrypt communication between A and B later.
    • Use key contracts to exchange symmetric keys. The standard algorithm for executing a key contract is the DH algorithm (Diffie-hellman algorithm).
    • In Java, the Keyagreement class implements the key contract, which uses the Init () method to pass in its private key and uses the Dophase () method to pass in the other's public key, which can then be used to generate the shared information using the Generatesecret () method.

    • Experiment 5: Simply select the specified string and use Java to compute the message digest for the specified string. The points to note are:
    • The MessageDigest class is also a factory class whose constructors are protected and do not allow you to create objects directly using new Messagedigist (), and you must generate MessageDigest objects through their static methods getinstance (). The arguments passed in specify the algorithm used to calculate the message digest, commonly known as "MD5", "SHA" and so on.
    • m.update(x.getBytes("UTF8" ));
      X is the string that needs to be evaluated, the parameter that is passed in by update is a byte type or an array of byte types, and for strings you need to use the GetBytes () method to generate an array of strings.

Iii. the problems encountered during the experiment and the process of solving them
    • 1. I met Badpaddingexception when I was doing a third small experiment. At that time to find out: If the argument passed to the decryption Dofinal method is a normal string produced by a byte array, it will throw
      Javax.crypto.badpaddingexception:data must start with zero

    • Problem 1: Check the byte array passed to the decryption clause is a problem, let pass in the correct encrypted array.
    • 2. In doing the experiment 3 o'clock the first idea that I discuss with a pair of partners is the established key, let the server generate the key, and then through other means to tell the client, in the client with scanner let the user enter the key, and then in the client to the infix expression into a suffix expression and then encrypted and then sent to the server, Let the server decrypt and then compute the value of the suffix expression and then send the value to the client . But it does not take into account that every key run is different, and it is too long to be entered. In writing this code, the error we encountered is how to program the client input of a string key type key, because we want to encrypt the plaintext, we need key type keys.
    • Problem 2: The problem solved by Guo Kai classmate, with the code
      ```
      keygenerator kg = keygenerator.getinstance ("DES");
      Key key = Kg.generatekey ();
      byte[] keybytes = key.getencoded ();
      Secretkey key = new Secretkeyspec (keybytes, "DES");

```

    • 3. In doing experiment 4 using the DH algorithm encryption, encountered a lot of problems, one of which is the following only copy forgot to think about the command line problem!

    • Resolution 3: The KEY_DH file is used to generate files Apub.dat and Apri.dat, the former holds the public key of a, the latter holds the private key of a, Bpub.dat and Bpri.dat, the former holds the public key of B, the latter holds the private key of B. In this way, arg[0] and arg[1] can be replaced with the corresponding documents.

    • 4. Incorrect secret key length! To be honest, see when there really is no solution to the clue.

    • Problem 4 solved: Asked Wenbin Wang classmate, after the point is because I directly create the key has 128 bits, but the next 3DES algorithm only need 24-bit key (this I did not find (_), and then the method is not to match my secret key length algorithm, or shorten my key length, Meet the 24-bit requirements.
    • Into my secret key length of the algorithm, with the AES algorithm to try, (after all, the information has been said 若是“AES”,可以是128, 192或256位 ), the results appear:

    • Shorten my secret key length: Wenbin Wang classmate reminds me can see Secretkeyspec jdk document, sure enough to find the winning magic weapon, thump thump thump thump!

So it was solved.

Others (sentiment, thinking, etc.)

This experiment has been expanded in cryptography, so I always have a little understanding of the algorithms for symmetric ciphers, block ciphers, and asymmetric ciphers.

International spy and anti-spy struggle of the focus, will always be mutual levy and decipher the password, so say the password involves mathematics, communication science, informatics, Ji Co, logic and other courses, is high-tech knowledge and technology system integration, is a very deep knowledge ah.

Iv. references
    • Cryptography Content
    • [Des algorithm] (https://baike.so.com/doc/5447735-5686103.html
    • Symmetric encryption algorithm-des and Desede algorithm
    • -DH algorithm for asymmetric encryption algorithm
    • Several solutions to Javax.crypto.BadPaddingException:Blocktype anomalies

20172328 "Program design and data structure" experimental Five cryptography Algorithm application 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.