Exemplo n.º 1
0
    /// <summary>
    /// 解析加密后的数据包得到原始数据包
    /// </summary>
    /// <param name="dataCache"></param>
    public static byte[] DeMakeData(byte[] buffer)
    {
        // 拆包得到数据包
        byte[] bufferNew     = new byte[buffer.Length - 3];
        bool   IsComPressLen = false;
        ushort crc           = 0;

        using (MMO_MemoryStream ms = new MMO_MemoryStream(buffer))
        {
            //是否压缩
            IsComPressLen = ms.ReadBool();
            crc           = ms.ReadUShort();
            ms.Read(bufferNew, 0, bufferNew.Length);
        }
        //1、crc校验
        ushort newCrc = CRC16.CalculateCrc16(bufferNew);

        if (newCrc == crc)
        {
            //2、异或后的原始数据包
            bufferNew = SecurityUtil.Xor(bufferNew);
            if (IsComPressLen)
            {
                //3、解压缩 bufferNew是真实数据
                bufferNew = ZlibHelper.deCompressBytes(bufferNew);
            }
        }
        return(bufferNew);
    }