#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) {
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];
int key_index = 0;
for (int i = 5; i < 37; ++i) {
if (i & 1) {
if (i % 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;
}
}
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);
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;
}