今天無意中在CodePlex 發現一個叫Windows Product Key Finder 的項目,從名字就可以看出它的用途。通過這款軟體可以輕鬆的擷取本地Windows 的產品金鑰。當然對於找不到密鑰光碟片的人來說這當然是款實用的工具,但如果到了某些圖謀不軌的人手裡那您的產品金鑰必定要受到威脅。
閱讀了項目原始碼後,其實程式的核心也是最有價值的部分就是去解碼\\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion 中的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 值(Byte)賦給DecodeProductKey 方法即可算出Windows 產品金鑰。項目描述中提到該程式在XP、Vista、Windows 7 系統上均可運行。
RegistryKey hklm = Registry.LocalMachine;hklm = hklm.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");byte[] digitalProductId = hklm.GetValue("DigitalProductId") as byte[];textBox1.Text = DecodeProductKey(digitalProductId);
由此可見簡單幾行代碼就可以使產品金鑰落入他人之手,並且這類程式在網上還有很多。對於Vista、Windows 7 使用者來說建議使用slmgr /cpky 命令對產品金鑰進行一下處理,詳細內容請參考《Windows 7 產品金鑰是否安全》。從註冊表清除產品金鑰資訊後,再運行該程式便會出現效果,產品金鑰將全部顯示為字母“B”,這樣您就不必擔心產品金鑰的安全性了。