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

lov*_*vin 5 c# c#-4.0

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

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

mrp*_*net 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;
    }
Run Code Online (Sandbox Code Playgroud)

要检查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;
    }
Run Code Online (Sandbox Code Playgroud)

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

注意:对我来说,原始的vbs脚本返回错误的Win7键,尽管原始的博客文章声明代码适用于Win7及以上.因此,我总是回归到Win7及更低版本的众所周知的旧方法.

希望能帮助到你.


Eri*_* J. 3

查看 Windows 产品密钥查找器。

http://wpkf.codeplex.com/

来源可用。

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