Simple interface encryption method based on MD5 encryption

Keywords: Java

Simple interface encryption method based on MD5 encryption

Recently, when offering the interface (http post) method to third parties, I kept thinking about how to make this encryption simple and effective. Finally, I referred to the signature method of WeChat and implemented the following as a tool class, which I can refer to if necessary:

    -

1. Signature Rules:

  1. The parameter of the numeric value removes data with an empty key, and then sorts it by default using the format of the URL key-value pair (that is, key1=value1&key2=value2...)Stitch into string strA;

  2. 2) strA finally stitches key s to get strSignTemp string, performs MD5 operation on strSignTemp, and converts all characters of the resulting string to uppercase to get signatures value.

    Give an example:
    The request parameters are as follows:

entrustmentOrderId : 1234567890
entrustmentOrderStatus : 3

The first step is to sort the parameters by default using the Collections.sort() method, which consists of the following strings in the format key=value:

strA="entrustmentOrderId=1234567890&entrustmentOrderStatus=3";

Step 2: Split the key and sign it:

strSignTemp=strA+"&key=6b323d7ed39e0008";
signatures=MD5(strSignTemp).toUpperCase();

2. Key Setup

Key value: 6b323d7ed39e0008

3. Signature tool code

package com.test.util;

import java.security.MessageDigest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class MD5Util {

    /**
     * 
     * @Description: Generate MD5
     * @author: tianpengw
     * @param message
     * @return
     */
    public static String getMD5(String message) {
        String md5 = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5"); // Create an md5 algorithm object
            byte[] messageByte = message.getBytes("UTF-8");
            byte[] md5Byte = md.digest(messageByte); // Get an MD5 byte array, 16*8=128 bits
            md5 = bytesToHex(md5Byte); // Convert to 16-bit string
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5;
    }

    /**
     * 
     * @Description: Binary to Hexadecimal
     * @author: tianpengw
     * @param bytes
     * @return
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuffer hexStr = new StringBuffer();
        int num;
        for (int i = 0; i < bytes.length; i++) {
            num = bytes[i];
            if (num < 0) {
                num += 256;
            }
            if (num < 16) {
                hexStr.append("0");
            }
            hexStr.append(Integer.toHexString(num));
        }
        return hexStr.toString().toUpperCase();
    }

    /**
     * 
     * @Description: Signature: Request parameter ordering followed by key value, and MD5 encryption to return uppercase results
     * @author: tianpengw
     * @param params Parameter Content
     * @param key key value
     * @return
     */
    public static String signatures(Map<String, Object> params, String key){
        String signatures = "";
        try {
            List<String> paramsStr = new ArrayList<String>();
            for (String key1 : params.keySet()) {
                if(null != key1 && !"".equals(key1)){
                    paramsStr.add(key1);
                }
            }
            Collections.sort(paramsStr);
            StringBuilder sbff = new StringBuilder();
            for (String kk : paramsStr) {
                String value = params.get(kk).toString();
                if ("".equals(sbff.toString())) {
                    sbff.append(kk + "=" + value);
                } else {
                    sbff.append("&" + kk + "=" + value);
                }
            }
            //Add key value
            sbff.append("&key="+key);
            signatures = getMD5(sbff.toString()).toUpperCase();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return signatures;
    }
}

Nothing in the tool is difficult, this key value can be defined by itself and notified to a third party.

Now that this tool class has been described, what is ambiguous about replying in the comments section

Posted by foolygoofy on Mon, 23 Mar 2020 10:36:11 -0700