编辑代码

#include <stdio.h>
#include <stdint.h>
#include <string.h>

// 函数声明
uint8_t reverse_bits(uint8_t byte);
void decrypt_37bytes_head(const unsigned char *content, uint8_t *decrypted_data);
int Check(const unsigned char *pPayload);

// 按位逆序函数
uint8_t reverse_bits(uint8_t byte) {
    uint8_t reversed_byte = 0;
    for (int i = 0; i < 8; ++i) {
        reversed_byte = (reversed_byte << 1) | (byte & 1);
        byte >>= 1;
    }
    return reversed_byte;
}

// 解密函数
void decrypt_37bytes_head(const unsigned char *content, uint8_t *decrypted_data) {
    // 37 bytes head
    uint8_t head_content[37];
    memcpy(head_content, content, 37);

    // 进行逆序
    uint8_t reverse_head[37];
    for (int i = 0; i < 37; ++i) {
        reverse_head[i] = head_content[36 - i];
    }

    // 获取头部解密密钥
    uint8_t xor_key[5];
    memcpy(xor_key, reverse_head, 5);

    // 解密后的数据
    uint8_t plain_head[32]; // 因为前5个字节是密钥,所以只有32个字节需要解密
    int key_index = 0;
    for (int i = 5; i < 37; ++i) {
        if (i & 1) { // 数据偏移为奇数
            if (i % 3) { // 数据偏移不能被3整除
                // 进行异或操作
                plain_head[i - 5] = reverse_head[i] ^ xor_key[key_index];
                key_index = (key_index + 1) % 5;
            } else {
                // 进行按位逆序操作
                plain_head[i - 5] = reverse_bits(reverse_head[i]);
            }
        } else {
            // 执行非操作
            plain_head[i - 5] = (~reverse_head[i]) & 0xFF;
        }
    }

    // 构建完整的解密后的37字节数据
    memcpy(decrypted_data, xor_key, 5);
    memcpy(decrypted_data + 5, plain_head, 32);
}

// 检查函数
int Check(const unsigned char *pPayload) {
    uint8_t decrypted_data[37];
    decrypt_37bytes_head(pPayload, decrypted_data);

    // 打印解密后的37字节数据
    printf("解密后的37字节数据: ");
    for (int i = 0; i < 37; ++i) {
        printf("0x%02X ", decrypted_data[i]);
    }
    printf("\n");

    // 获取需要检查的值
    uint16_t value_06_07 = (decrypted_data[6] << 8) | decrypted_data[7];
    uint8_t value_08 = decrypted_data[8];
    uint32_t value_0b_0e = (decrypted_data[11] << 24) | (decrypted_data[12] << 16) | (decrypted_data[13] << 8) | decrypted_data[14];
    uint8_t value_13 = decrypted_data[19];
    uint8_t value_19 = decrypted_data[25];
    uint8_t value_1f = decrypted_data[31];

    // 打印每个需要检查的值
    printf("Offset 0x06-0x07: 0x%04X\n", value_06_07);
    printf("Offset 0x08: 0x%02X\n", value_08);
    printf("Offset 0x0B-0x0E: 0x%08X\n", value_0b_0e);
    printf("Offset 0x13: 0x%02X\n", value_13);
    printf("Offset 0x19: 0x%02X\n", value_19);
    printf("Offset 0x1F: 0x%02X\n", value_1f);

    // 检查条件
    if (value_06_07 == 0xfca8 &&
        value_08 >= 0x5 && value_08 <= 0xF &&
        value_0b_0e == 0x30320000 &&
        value_13 == 0x1c &&
        value_19 == 0x1c &&
        value_1f == 0xa8) {
        return 1;
    } else {
        return 0;
    }
}

int main() {
    // 原始数据
    unsigned char pPayload[] = {0xff, 0x32, 0xff, 0x00, 0xff, 0x22, 0xff, 0xf5, 0xff, 0x00, 0xff, 0x56, 0xff, 0x8a, 0xcd, 0xf4, 0xff, 0x2e, 0x48, 0xe4, 0x5f, 0x39, 0xff, 0xf5, 0xcd, 0x7a, 0x6c, 0xe5, 0xf4, 0x22, 0x03, 0xed, 0x8a, 0xf5, 0x4a, 0x8a, 0x32};
    int result = Check(pPayload);

    if (result) {
        printf("所有条件满足,返回1\n");
    } else {
        printf("条件不满足,返回0\n");
    }

    return 0;
}