DigitalProductId解密算法的一个例子

public static string DecodeProductKey(byte[] digitalProductId)

{

// Offset of first byte of encoded product key in

// 'DigitalProductIdxxx" REG_BINARY value. Offset = 34H.

const int keyStartIndex = 52;

// Offset of last byte of encoded product key in

// 'DigitalProductIdxxx" REG_BINARY value. Offset = 43H.

const int keyEndIndex = keyStartIndex + 15;

// Possible alpha-numeric characters in product key.

char[] digits = new char[]

{

'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R',

'T', 'V', 'W', 'X', 'Y', '2', '3', '4', '6', '7', '8', '9',

};

// Length of decoded product key

const int decodeLength = 29;

// Length of decoded product key in byte-form.

// Each byte represents 2 chars.

const int decodeStringLength = 15;

// Array of containing the decoded product key.

char[] decodedChars = new char[decodeLength];

// Extract byte 52 to 67 inclusive.

ArrayList hexPid = new ArrayList();

for (int i = keyStartIndex; i <= keyEndIndex; i++)

{

hexPid.Add(digitalProductId[i]);

}

for (int i = decodeLength - 1; i >= 0; i--)

{

// Every sixth char is a separator.

if ((i + 1) % 6 == 0)

{

decodedChars[i] = '-';

}

else

{

// Do the actual decoding.

int digitMapIndex = 0;

for (int j = decodeStringLength - 1; j >= 0; j--)

{

int byteValue = (digitMapIndex << 8) | (byte)hexPid[j];

hexPid[j] = (byte)(byteValue / 24);

digitMapIndex = byteValue % 24;

}

decodedChars[i] = digits[digitMapIndex];

}

}

return new string(decodedChars);

}

算法大概意思:

注册表里面DigitalProductId的第0x34到0x43位15个十六进制的加密字符串。

第一部分:

先取余,左移8位加上下一个,

然后在取余,左移8位加上下一个。

15个hexPid[]数组循环完成后,得到最终索引值,查找digits[]数组得到一个字符。

第二部分:

除上24,取整得到下一次循环的hexPid[]数组的一个值。

循环完成得到完整的hexPid[]数组,继续进行下一次循环。

digitMapIndex = byteValue % 24;

第一个索引是0x02对应的字符是D,正确。

第二次循环hexpid[]数字变为:

第二个索引是0x08对应的字符是M,正确。