public void EncryptSerial(byte[] info, byte[] firmware) { //Calculate the checksum and the CRC16 of the info block before the encryption. //Fill the empty info arrary with random values. for (int i = 32; i < info.Length - 2; i++) { info[i] = (byte)Utils.Generator.Next(0, 255); } //compute the 8Bit checksum of devcode and serial string and store it in the position 34. byte cs = 0; for (int i = 5; i < 34; i++) { cs += info[i]; } info[34] = cs; //compute the CRC16 of the info array and store it in the last two position in little endian order. ushort crc; CRC16 crc16 = new CRC16(); crc = crc16.GetCRC16(info, info.Length - 2, 0); info[info.Length - 1] = (byte)((crc >> 8)); info[info.Length - 2] = (byte)(crc & 0xFF); //Now we encrypt the info block, ready to be inserted in the firmware. /*Step1 * Data scrambling. We swap the first byte with the last, the fourth from the beginning with the fourth from the end and so on. * So we have the following 10 swaps:(0-79),(4-75),(8-71),(12-67),(16-63),(20-59),(24-55),(28-51),(32-47),(36-43). */ for (int i = 0; i < info.Length / 2; i += 4) { byte t = info[i]; info[i] = info[info.Length - i - 1]; info[info.Length - i - 1] = t; } //Step 2, Left rotate the whole array by 3 bits. byte m = (byte)(info[0] >> 5); for (int i = 0; i < info.Length - 1; i++) { info[i] = (byte)(info[i] << 3 & 0xF8 | info[i + 1] >> 5); } info[info.Length - 1] = (byte)(info[info.Length - 1] << 3 & 0xF8 | m); //step3 xoring each info table value with a random number from xortable. The start index in this table is 0x0A. Index is incremented modulo 256 byte index = 0x0A; for (int i = 0; i < info.Length; i++) { info[i] = (byte)(info[i] ^ firmware[XOR_TABLE_OFFSET + index++]); } }
private ushort GetKeyCRC(byte[] data) { CRC16 crc16 = new CRC16(); return(crc16.GetCRC16(data, 0)); }
ushort get_dev_crc() { CRC16 crc16 = new CRC16(); return(crc16.GetCRC16(Encoding.ASCII.GetBytes(TxtDevcode.Text), TxtDevcode.Text.Length, 0)); }