ParamUtil.java 7.73 KB
package com.glsx.libble.util;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.LogUtils;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 * @Package: com.glsx.ble_module.util
 * @ClassName: ParamUtil
 * @Company GLSX
 * @Email yangks@didihu.com.cn
 * @Description: 作用描述:车钥匙蓝牙通信参数工具类
 * @Author: yangks
 * @CreateDate: 2021/4/15 11:35
 * @Version:
 */
public class ParamUtil {
    private final static String CREP = "125ACDF4554BE351";
    private final static byte[] STRUCT_HEAD = {0x7B};//结构体头部
    private final static byte[] STRUCT_TAIL = {0x7D};//结构体尾部

    /**
     * 获取手机MAC地址,不够8字节,末尾补00
     *
     * @return
     */
    public static byte[] getPhoneMac() {
        String mac = DeviceUtils.getMacAddress();
        String[] macBytes = mac.split(":");
        StringBuilder stringBuilder = new StringBuilder();
        for (String b : macBytes) {
            stringBuilder.append(b);
        }
        stringBuilder.append("0000");
        return hexStringToBytes(stringBuilder.toString());
    }

    /**
     * 获取手机MAC地址,6字节,带冒号
     *
     * @return
     */
    public static String getPhoneMac6() {
        return DeviceUtils.getMacAddress().toUpperCase();
    }

    /**
     * 十六进制字符串转十六进制
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null) {
            return new byte[]{};
        }
        return ConvertUtils.hexString2Bytes(hexString);
    }

    /**
     * 获取参数
     * 参数结构十六进制  structHead(标识头--1个字节) + msgHead(消息头--一个字节) + bodyHead(消息体头部--8个字节)
     * + bodyTail(消息体尾部--8个字节) + msgTail(检验码--1个字节) + structTail(识别尾--1个字节)
     * 例如:7B 30 E01F88AB9BFF0000 125ACDF4554BE351 2E 7D
     *
     * @param msgHead
     * @param bodyHead
     * @param bodyTail
     * @param key      加密随机字符串  注册为固定:125ACDF4554BE352,鉴权:随机生成
     * @return
     */
    public static byte[] getParam(byte[] msgHead, byte[] bodyHead, byte[] bodyTail, String key) {
//        LogUtils.d("---消息头--一个字节---" + ToolUtils.bytes2HexString(msgHead));
//        LogUtils.d("---消息体头部--8个字节---" + ToolUtils.bytes2HexString(bodyHead));
//        LogUtils.d("---消息体尾部--8个字节---" + ToolUtils.bytes2HexString(bodyTail));
//        LogUtils.d("---加密key---" + key);
        byte[] body = addBytes(bodyHead, bodyTail);//拼接消息体
        byte[] enBody = new byte[0];//加密消息体
        try {
            enBody = AESEncryptUtils.encryption(body, key);
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        //LogUtils.d("---"+enBody.length);
//        LogUtils.d("---加密后消息体---" + ToolUtils.bytes2HexString(enBody));
        byte[] bytes = addBytes(addBytes(STRUCT_HEAD, msgHead), enBody);
//        LogUtils.d("---表示头+消息头+加密后消息体---" + ToolUtils.bytes2HexString(bytes));
        byte[] checkBytes = getCheckCode(addBytes(msgHead, enBody));
//        LogUtils.d("---校验码---" + ToolUtils.bytes2HexString(checkBytes));
        byte[] resBytes = addBytes(addBytes(bytes, checkBytes), STRUCT_TAIL);
        LogUtils.d("---完整体---" + ConvertUtils.bytes2HexString(resBytes));
        //LogUtils.d("---"+ToolUtils.bytes2HexString(resBytes));
//        LogUtils.d("--------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-" + resBytes.length);
        return resBytes;
    }

    /**
     * 获取消息体----body
     *
     * @param data 完整通信协议20字节
     * @return body
     */
    public static byte[] getBody(byte[] data) {
        if (data.length != 20) {
            throw new IllegalArgumentException("数据长度不等于20字节");
        }
        byte[] resBytes = new byte[16];
        int j = 0;
        for (int i = 2; i < 18; i++) {
            resBytes[j] = data[i];
            j++;
        }
        return resBytes;
    }

    /**
     * 获取消息体前8个字节----bodyHead
     *
     * @param data 完整消息体16字节
     * @return bodyHead
     */
    public static byte[] getBodyHead(byte[] data) {
        if (data.length != 16) {
            throw new IllegalArgumentException("数据长度不等于16字节");
        }
        byte[] resBytes = new byte[8];
        int j = 0;
        for (int i = 0; i < 8; i++) {
            resBytes[j] = data[i];
            j++;
        }
        return resBytes;
    }

    /**
     * 获取消息体后8个字节----bodyTail
     *
     * @param data 完整消息体16字节
     * @return bodyTail
     */
    public static byte[] getBodyTail(byte[] data) {
        if (data.length != 16) {
            throw new IllegalArgumentException("数据长度不等于16字节");
        }
        byte[] resBytes = new byte[8];
        int j = 0;
        for (int i = 8; i < 16; i++) {
            resBytes[j] = data[i];
            j++;
        }
        return resBytes;
    }

    /**
     * 生成校验码
     *
     * @param bytes
     * @return
     */
    public static byte[] getCheckCode(byte[] bytes) {
        byte res = 0x00;
        for (byte b : bytes) {
            res = (byte) (b ^ res);
        }
        byte[] resBytes = {res};
        return resBytes;
    }

    /**
     * 拼接两个byte数组
     *
     * @param data1
     * @param data2
     * @return data1 与 data2拼接的结果
     */
    public static byte[] addBytes(byte[] data1, byte[] data2) {
        byte[] data3 = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, data3, 0, data1.length);
        System.arraycopy(data2, 0, data3, data1.length, data2.length);
        return data3;
    }

    /**
     * 比较两个字节数组
     *
     * @param data1
     * @param data2
     * @return
     */
    public static boolean compareBytes(byte[] data1, byte[] data2) {
        if (data1.length != data2.length) {
            return false;
        }
        int i = 0;
        while (i < data1.length) {
            if (data1[i] != data2[i]) {
                return false;
            }
            i++;
        }
        return true;
    }

    /**
     * 生成8字节  16进制随机byte数组
     *
     * @return
     */
    public static byte[] getRandom() {
        return ConvertUtils.hexString2Bytes(getRandomValue());
    }

    /*产生8位16进制的数*/
    public static String getRandomValue() {
        String str = "";
        for (int i = 0; i < 16; i++) {
            char temp = 0;
            int key = (int) (Math.random() * 2);
            switch (key) {
                case 0:
                    temp = (char) (Math.random() * 10 + 48);//产生随机数字
                    break;
                case 1:
                    temp = (char) (Math.random() * 6 + 'a');//产生a-f
                    break;
                default:
                    break;
            }
            str = str + temp;
        }
        return str;
    }
}