java解密ble宣传的有效载荷数据

问题描述

我有一个ble设备,可以保持对加密数据包的广告,并且我正在做的是设计一个Android移动应用来解密数据包并提取数据。该设备来自第三方,他们向我介绍了如何加密信息。我没有他们的代码

基本上,在每个广告包中,随机生成一个2字节的加密公钥。低10位是查找表的初始偏移量,该表已提供给我。高6位是确定下一个字节的步幅。

解密信息的伪代码为:

    offset = public_key & 0x03FF

    stride = ((public_key >> 10) & 0x003F) + 1 

    decrypted_data_byte[0] = lookup_table[offset]

    for i from 1 to n:
       offset  = (offset + stride) & 0x03FF
       decrypted_data_byte[i] = lookup_table[offset]

数据包的结构如下: 加密密钥2个字节,签名1个字节,版本1个字节。 大于1个字节的字段为低端字节序。

我的Java代码用于测试,因此只能获取版本信息。

    private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes();
    public static String bytesToHex(byte[] bytes) {
        byte[] hexChars = new byte[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars,StandardCharsets.UTF_8);
    }

    public static byte[] getSliceOfArray(byte[] arr,int start,int end)
    {
        // Get the slice of the Array
        byte[] slice = new byte[end - start];
        // copy elements of arr to slice
        for (int i = 0; i < slice.length; i++) {
            slice[i] = arr[start + i];
        }
        // return the slice
        return slice;
    }

   /* copy complete advertised data*/
   byte[] advertisedData = getSliceOfArray(Arrays.copyOf(scanRecord,scanRecord.length),5,31);
   /* get private key for decryption,switch bytes for little endian*/
   byte[] key1 = new byte[2];
   key1[0] = advertisedData[0];
   key1[1] = advertisedData[1];
   /* turn hex string to Integer*/
   int publicKey1 = Integer.parseInt(bytesToHex(key1),16);
   /* offset and stride on lookup table*/
   int offset1 = (publicKey1 & 0x03FF);
   int stride1 = ((publicKey1 >>> 10) & 0x003F) + 1;
   /* signature */
   byte version = advertisedData[3];
   for(int i=1;i<=3;i++){offset1 = (offset1+stride1) & 0x03FF;}
   byte[] realVersion = {(byte) (version ^ privateKey[offset1])};
   Log.e(TAG,"onLeScan version 1: " + bytesToHex(realVersion));

问题是我没有获得正确的解密信息。我对Java很陌生,因此不知道是由于我的代码还是其他原因。

如果有人可以给我一些建议,我将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)