如何在C#中获取Windows产品密钥?

时间:2012-06-07 06:20:56

标签: c# c#-4.0

如何在C#中获取Windows产品密钥?

我想从客户端获得一些像Windows密钥的密钥。

6 个答案:

答案 0 :(得分:6)

Windows产品密钥查找器和Erij J.及其他人提到的其他解决方案仅适用于Windows XP和Windows 7。自Windows 8以来,Microsoft已经更改了密钥加密算法。

我在这里找到了适用于Windows 8及其博客的解决方案: http://winaero.com/blog/how-to-view-your-product-key-in-windows-10-windows-8-and-windows-7/

然而,它是用VBS编写的,所以我把它重写为C#。

您可以在GitHub上查看完整项目:https://github.com/mrpeardotnet/WinProdKeyFinder

以下是如何在Windows 8及更高版本中解码产品密钥的代码:

    public static string DecodeProductKeyWin8AndUp(byte[] digitalProductId)
    {
        var key = String.Empty;
        const int keyOffset = 52;
        var isWin8 = (byte)((digitalProductId[66] / 6) & 1);
        digitalProductId[66] = (byte)((digitalProductId[66] & 0xf7) | (isWin8 & 2) * 4);

        // Possible alpha-numeric characters in product key.
        const string digits = "BCDFGHJKMPQRTVWXY2346789";
        int last = 0;
        for (var i = 24; i >= 0; i--)
        {
            var current = 0;
            for (var j = 14; j >= 0; j--)
            {
                current = current*256;
                current = digitalProductId[j + keyOffset] + current;
                digitalProductId[j + keyOffset] = (byte)(current/24);
                current = current%24;
                last = current;
            }
            key = digits[current] + key;
        }
        var keypart1 = key.Substring(1, last);
        const string insert = "N";
        key = key.Substring(1).Replace(keypart1, keypart1 + insert);
        if (last == 0)
            key = insert + key;
        for (var i = 5; i < key.Length; i += 6)
        {
            key = key.Insert(i, "-");
        }
        return key;
    }

要检查Windows版本并获取digitalProductId,请使用以下包装方法:

    public static string GetWindowsProductKey()
    {
            var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                                          RegistryView.Default);
            const string keyPath = @"Software\Microsoft\Windows NT\CurrentVersion";
            var digitalProductId = (byte[])key.OpenSubKey(keyPath).GetValue("DigitalProductId");

        var isWin8OrUp =
            (Environment.OSVersion.Version.Major == 6 && System.Environment.OSVersion.Version.Minor >= 2)
            ||
            (Environment.OSVersion.Version.Major > 6);

        var productKey = isWin8OrUp ? DecodeProductKeyWin8AndUp(digitalProductId) : DecodeProductKey(digitalProductId);
        return productKey;
    }

我在几台机器上测试了它,它给了我正确的结果。即使在Windows 10上,我也可以获得通用的Windows 10代码(在升级的系统上)。

注意:对我来说,原始的vbs脚本返回了错误的Win7密钥,尽管原始的blogpost声明代码适用于Win7及以上。因此,我总是回归到Win7及其下游的着名旧方法。

希望它有所帮助。

答案 1 :(得分:3)

WMI有一个名为Win32_OperatingSystem的类,其中包含一个名为 SerialNumber 的属性:

  

操作系统产品序列号。

     

示例:“10497-OEM-0031416-71674”

System.Management命名空间包含允许.NET代码与WMI交互的类。

答案 2 :(得分:2)

查看Windows产品密钥查找器。

http://wpkf.codeplex.com/

来源可用。

密钥存储在注册表中,需要使用源中提供的算法进行解码。

答案 3 :(得分:1)

答案 4 :(得分:1)

使用 mrpeardotnet的回答...

我遇到错误,RegistryView.Default无法转换为字符串。 我改变了这个:

var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);

到此:

var key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, Environment.MachineName);

它似乎有效。

答案 5 :(得分:-1)

使用此代码,您可以找到Microsoft产品的产品密钥。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
namespace MSKeyFinder
{
    public class KeyDecoder
    {
        public enum Key { XP, Office10, Office11 };
        public static byte[] GetRegistryDigitalProductId(Key key)
        {
            byte[] digitalProductId = null;
            RegistryKey registry = null;
            switch (key)
            {
                // Open the XP subkey readonly.
                case Key.XP:
                    registry =
                      Registry.LocalMachine.
                        OpenSubKey(
                          @"SOFTWARE\Microsoft\Windows NT\CurrentVersion",
                            false);
                    break;
                // Open the Office 10 subkey readonly.
                case Key.Office10:
                    registry =
                      Registry.LocalMachine.
                        OpenSubKey(
                          @"SOFTWARE\Microsoft\Office\10.0\Registration\" + 
                          @"{90280409-6000-11D3-8CFE-0050048383C9}",
                          false);
                    // TODO: Open the registry key.
                    break;
                // Open the Office 11 subkey readonly.
                case Key.Office11:
                    // TODO: Open the registry key.
                    break;
            }
            if (registry != null)
            {
                // TODO: For other products, key name maybe different.
                digitalProductId = registry.GetValue("DigitalProductId")
                  as byte[];
                registry.Close();
            }
            return 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);
        }
    }
}

参考:http://www.codeproject.com/Articles/23334/Microsoft-Product-Key-Finder

相关问题