编辑代码


#include <stdio.h>


#define uint8_t unsigned char  
#define uint16_t unsigned short 
#define uint32_t unsigned long long  

const  uint8_t Table[64] = {
0x21,0x10,0xFE,0x11,0x50,0x7E,0x15,0x88,
0x49,0x32,0x01,0x46,0x20,0x3C,0x15,0x16,
0x24,0x23,0xFD,0x19,0x10,0x09,0xCF,0x4F,
0x18,0x61,0x01,0x21,0x11,0xA6,0xAB,0xF7,
0x13,0x96,0xFB,0x98,0x12,0x3C,0x15,0x15,
0x91,0x12,0x01,0x20,0x17,0x88,0x09,0xCF,
0x22,0x24,0xEF,0x92,0x32,0x15,0x16,0x28,
0x17,0x14,0x01,0x71,0x41,0x2B,0x7E,0x15
};


//A42C56FA
//BC4EA2CA
//uint32_t glv_DeviceID[2] = {0xFA562CA4,0xCA3D4EBC};
//uint32_t glv_DeviceID[2] = {0xB2ACCED8, 0xBE72390A};
void Encrypt_ID_Prg(void){
	uint8_t i;
    uint8_t Encrypt_ID[8] = {0};

    //获取到的加密ID
    //uint8_t Device_ID[8] =  {0xDA,0x83,0x5F,0x93,0xD0,0x16,0x08,0xCB};
    //器件的真实ID
    //uint8_t Device_ID[8] =  {0xFA,0x56,0x2C,0xA4,0xCA,0x3D,0x4E,0xBC};

    //获取到的加密ID
    //uint8_t Device_ID[8] =  {0xD8,0xCE,0xAC,0xB2,0x0A,0x39,0x72,0xBE};
    //器件的真实ID
    uint8_t Device_ID[8] =  {0xD8,0xDE,0xDD,0x0A,0x08,0x08,0x2E,0x44};

    printf("\nDevice ID:\n");
	for(i = 0; i < 8; i++){
        if(Device_ID[i] >= 16)
            printf("0x%2X, ", Device_ID[i]);
        else
            printf("0x0%1X, ", Device_ID[i]);
    }  

    printf("\n\nEncrypt Device ID:\n");

	//ID数据运算
	for(i = 0; i < 8; i++){
		switch(i){
			case 0:
				Device_ID[i] ^= Table[0];
				Device_ID[i] += Table[1];
				Device_ID[i] &= Table[2];
				Device_ID[i] *= Table[3];
				Device_ID[i] ^= Table[4];

                Encrypt_ID[i] = Device_ID[i];
                if(Encrypt_ID[i] >= 16)
                    printf("0x%2X, ", Encrypt_ID[i]);
                else
                    printf("0x0%1X, ", Encrypt_ID[i]);
			break;
			case 1:
				Device_ID[i] += Table[8];
				Device_ID[i] -= Table[9];
				Device_ID[i] = ((Device_ID[i] << Table[10]) & 0xFF);
                Device_ID[i] |= Table[11];
				Device_ID[i] ^= Table[12];

                Encrypt_ID[i] = Device_ID[i];
                if(Encrypt_ID[i] >= 16)
                    printf("0x%2X, ", Encrypt_ID[i]);
                else
                    printf("0x0%1X, ", Encrypt_ID[i]);
			break;
			case 2:
				Device_ID[i] ^= Table[16];
				Device_ID[i] += Table[17];
				Device_ID[i] &= Table[18];
				Device_ID[i] *= Table[19];
				Device_ID[i] ^= Table[20];

                Encrypt_ID[i] = Device_ID[i];
                if(Encrypt_ID[i] >= 16)
                    printf("0x%2X, ", Encrypt_ID[i]);
                else
                    printf("0x0%1X, ", Encrypt_ID[i]);
			break;
			case 3:
				Device_ID[i] += Table[24];
				Device_ID[i] -= Table[25];
				Device_ID[i] = ((Device_ID[i] << Table[26]) & 0xFF);
				Device_ID[i] |= Table[27];
				Device_ID[i] ^= Table[28];

                Encrypt_ID[i] = Device_ID[i];
                if(Encrypt_ID[i] >= 16)
                    printf("0x%2X, ", Encrypt_ID[i]);
                else
                    printf("0x0%1X, ", Encrypt_ID[i]);
			break;
			case 4:
				Device_ID[i] ^= Table[32];
				Device_ID[i] += Table[33];
				Device_ID[i] &= Table[34];
				Device_ID[i] *= Table[35];
				Device_ID[i] ^= Table[36];

                Encrypt_ID[i] = Device_ID[i];
                if(Encrypt_ID[i] >= 16)
                    printf("0x%2X, ", Encrypt_ID[i]);
                else
                    printf("0x0%1X, ", Encrypt_ID[i]);
			break;
			case 5:
				Device_ID[i] += Table[40];
				Device_ID[i] -= Table[41];
				Device_ID[i] = ((Device_ID[i] << Table[42]) & 0xFF);
				Device_ID[i] |= Table[43];
				Device_ID[i] ^= Table[44];

                Encrypt_ID[i] = Device_ID[i];
                if(Encrypt_ID[i] >= 16)
                    printf("0x%2X, ", Encrypt_ID[i]);
                else
                    printf("0x0%1X, ", Encrypt_ID[i]);
			break;
			case 6:
				Device_ID[i] ^= Table[48];
				Device_ID[i] += Table[49];
				Device_ID[i] &= Table[50];
				Device_ID[i] *= Table[51];
				Device_ID[i] ^= Table[52];

                Encrypt_ID[i] = Device_ID[i];
                if(Encrypt_ID[i] >= 16)
                    printf("0x%2X, ", Encrypt_ID[i]);
                else
                    printf("0x0%1X, ", Encrypt_ID[i]);
			break;
			case 7:
				Device_ID[i] += Table[56];
				Device_ID[i] -= Table[57];
				Device_ID[i] = ((Device_ID[i] << Table[58]) & 0xFF);
				Device_ID[i] |= Table[59];
				Device_ID[i] ^= Table[60];

                Encrypt_ID[i] = Device_ID[i];
                if(Encrypt_ID[i] >= 16)
                    printf("0x%2X, ", Encrypt_ID[i]);
                else
                    printf("0x0%1X, ", Encrypt_ID[i]);
			break;
			default:
			break;
		}
	}
}


// 穷举,有多个结果
void Decrypt_ID_Prg(void){
	uint8_t i;
    uint16_t j;
    uint8_t Decrypt_ID[8] = {0x00};    //缓存解密后的ID

	//已经加密的ID
    uint8_t Encrypt_ID[8] = {0xD8,0xCE,0xAC,0xB2,0x0A,0x39,0x72,0xBE};

    printf("\n\nEncrypt Device ID:\n");
	for(i = 0; i < 8; i++){
        if(Encrypt_ID[i] >= 16)
            printf("0x%2X, ", Encrypt_ID[i]);
        else
            printf("0x0%1X, ", Encrypt_ID[i]);
    }  

    printf("\n\n枚举出的 Device ID:\n");

	for(i = 0; i < 8; i++){
		switch(i){
			case 0:
            for(j = 0; j < 255; j++){
                Decrypt_ID[i] = j;

				Decrypt_ID[i] ^= Table[0];
				Decrypt_ID[i] += Table[1];
				Decrypt_ID[i] &= Table[2];
				Decrypt_ID[i] *= Table[3];
				Decrypt_ID[i] ^= Table[4];

                if(Decrypt_ID[i] == Encrypt_ID[i]){         //穷举出的数据是否等于加密后的数据
                    if(j >= 16)
                        printf("0x%2X, ", j);
                    else
                        printf("0x0%1X, ", j);
                    break;                                                 
                }
            }
			break;
			case 1:
            for(j = 0; j < 255; j++){
                Decrypt_ID[i] = j;

				Decrypt_ID[i] += Table[8];
				Decrypt_ID[i] -= Table[9];
				Decrypt_ID[i] = ((Decrypt_ID[i] << Table[10]) & 0xFF);
                Decrypt_ID[i] |= Table[11];
				Decrypt_ID[i] ^= Table[12];

                if(Decrypt_ID[i] == Encrypt_ID[i]){
                    if(j >= 16)
                        printf("0x%2X, ", j);
                    else
                        printf("0x0%1X, ", j);
                    break;  
                }
            }
			break;
			case 2:
            for(j = 0; j < 255; j++){
                Decrypt_ID[i] = j;

				Decrypt_ID[i] ^= Table[16];
				Decrypt_ID[i] += Table[17];
				Decrypt_ID[i] &= Table[18];
				Decrypt_ID[i] *= Table[19];
				Decrypt_ID[i] ^= Table[20];

                if(Decrypt_ID[i] == Encrypt_ID[i]){
                    if(j >= 16)
                        printf("0x%2X, ", j);
                    else
                        printf("0x0%1X, ", j);
                    break;  
                }
            }
			break;
			case 3:
            for(j = 0; j < 255; j++){
                Decrypt_ID[i] = j;

				Decrypt_ID[i] += Table[24];
				Decrypt_ID[i] -= Table[25];
				Decrypt_ID[i] = ((Decrypt_ID[i] << Table[26]) & 0xFF);
				Decrypt_ID[i] |= Table[27];
				Decrypt_ID[i] ^= Table[28];

                if(Decrypt_ID[i] == Encrypt_ID[i]){
                    if(j >= 16)
                        printf("0x%2X, ", j);
                    else
                        printf("0x0%1X, ", j);
                    break;  
                }
            }
			break;
			case 4:
            for(j = 0; j < 255; j++){
                Decrypt_ID[i] = j;

				Decrypt_ID[i] ^= Table[32];
				Decrypt_ID[i] += Table[33];
				Decrypt_ID[i] &= Table[34];
				Decrypt_ID[i] *= Table[35];
				Decrypt_ID[i] ^= Table[36];

                if(Decrypt_ID[i] == Encrypt_ID[i]){
                    if(j >= 16)
                        printf("0x%2X, ", j);
                    else
                        printf("0x0%1X, ", j);
                    break;  
                }
            }
			break;
			case 5:
            for(j = 0; j < 255; j++){
                Decrypt_ID[i] = j;

				Decrypt_ID[i] += Table[40];
				Decrypt_ID[i] -= Table[41];
				Decrypt_ID[i] = ((Decrypt_ID[i] << Table[42]) & 0xFF);
				Decrypt_ID[i] |= Table[43];
				Decrypt_ID[i] ^= Table[44];

                if(Decrypt_ID[i] == Encrypt_ID[i]){
                    if(j >= 16)
                        printf("0x%2X, ", j);
                    else
                        printf("0x0%1X, ", j);
                    break;  
                }
            }
			break;
			case 6:
            for(j = 0; j < 255; j++){
                Decrypt_ID[i] = j;

				Decrypt_ID[i] ^= Table[48];
				Decrypt_ID[i] += Table[49];
				Decrypt_ID[i] &= Table[50];
				Decrypt_ID[i] *= Table[51];
				Decrypt_ID[i] ^= Table[52];

                if(Decrypt_ID[i] == Encrypt_ID[i]){
                    if(j >= 16)
                        printf("0x%2X, ", j);
                    else
                        printf("0x0%1X, ", j);
                    break;  
                }
            }
			break;
			case 7:
            for(j = 0; j < 255; j++){
                Decrypt_ID[i] = j;

				Decrypt_ID[i] += Table[56];
				Decrypt_ID[i] -= Table[57];
				Decrypt_ID[i] = ((Decrypt_ID[i] << Table[58]) & 0xFF);
				Decrypt_ID[i] |= Table[59];
				Decrypt_ID[i] ^= Table[60];

                if(Decrypt_ID[i] == Encrypt_ID[i]){
                    if(j >= 16)
                        printf("0x%2X, ", j);
                    else
                        printf("0x0%1X, ", j);
                    break;  
                }
            }
			break;
			default:
			break;
		}
	}
}

int main () {

    Encrypt_ID_Prg();

    Decrypt_ID_Prg();

    return 0;
}