简体   繁体   English

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

[英]How can I get windows XP product key in C#?

How can I get windows XP product key in C# ?如何在 C# 中获取 Windows XP 产品密钥?

I want to get some key like windows key from client side.我想从客户端获取一些像 Windows 键这样的键。

Windows Product Key Finder and other solutions mentioned here by Erij J. and others are working for Windows XP and Windows 7 only. Windows Product Key Finder 和 Erij J. 和其他人在此提到的其他解决方案仅适用于 Windows XP 和 Windows 7。 Microsoft has changed key encryption algorithm since Windows 8.自 Windows 8 起,Microsoft 更改了密钥加密算法。

I've found a solution for Windows 8 and up on blogpost here: http://winaero.com/blog/how-to-view-your-product-key-in-windows-10-windows-8-and-windows-7/我在这里的博客文章中找到了适用于 Windows 8 及更高版本的解决方案: http ://winaero.com/blog/how-to-view-your-product-key-in-windows-10-windows-8-and-windows -7/

However it is written in VBS, so I've rewritten it to C#.但是它是用 VBS 编写的,所以我将它重写为 C#。

You can check full project on GitHub: https://github.com/mrpeardotnet/WinProdKeyFinder你可以在 GitHub 上查看完整的项目: https : //github.com/mrpeardotnet/WinProdKeyFinder

Here is the code how to decode product key in Windows 8 and up:以下是如何在 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;
    }

To check Windows version and get digitalProductId use wrapper method like this:要检查 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;
    }

I've tested it on several machines and it was giving me correct results.我已经在多台机器上测试过它,它给了我正确的结果。 Even on Windows 10 I was able to get generic Windows 10 code (on upgraded system).即使在 Windows 10 上,我也能够获得通用的 Windows 10 代码(在升级的系统上)。

Note: For me the original vbs script was returning wrong Win7 keys despite the fact that original blogpost states the code works for Win7 and up.注意:对我来说,尽管原始博客文章指出代码适用于 Win7 及更高版本,但原始 vbs 脚本返回了错误的 Win7 键。 So I always fallback to the well known old method for Win7 and lower.所以我总是回退到 Win7 及更低版本的众所周知的旧方法。

Hope it helps.希望能帮助到你。

Check out Windows Product Key Finder.查看 Windows 产品密钥查找器。

http://wpkf.codeplex.com/ http://wpkf.codeplex.com/

Source is available.来源可用。

The key is stored in the registry and needs to be decoded using an algorithm available in the source.密钥存储在注册表中,需要使用源中可用的算法进行解码。

WMI has a class, called Win32_OperatingSystem that contains a property called SerialNumber : WMI 有一个名为Win32_OperatingSystem的类,其中包含一个名为SerialNumber的属性:

Operating system product serial identification number.操作系统产品序列号。

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

The System.Management namespace contains classes that allow .NET code to interact with WMI. System.Management命名空间包含允许 .NET 代码与 WMI 交互的类。

Going with mrpeardotnet's answer...使用mrpeardotnet的答案......

I was getting errors where RegistryView.Default would not convert to string.我遇到了RegistryView.Default无法转换为字符串的错误。 I changed this:我改变了这个:

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

to this:对此:

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

and it seems to work.它似乎有效。

Using this code, you can find the product key of your Microsoft products.使用此代码,您可以找到 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);
        }
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 或免费的 XP 工具卸载或删除 Windows XP Sp3 游戏? - How can I uninstall or delete Windows XP Sp3 Games using C# or free XP tools? 如何为我的 C# 应用程序创建产品密钥? - How can I create a product key for my C# application? 如何在c#中禁用Windows键? - how can I disable windows key in c#? 在Windows XP上如何在C#中使用语音识别? +我需要训练每台运行该应用程序的PC吗? - How can I use speech recognition with C# on Windows XP? + do I need to train every pc that runs the app? 在Windows XP上,如何枚举系统显示的所有窗口(C#) - On Windows XP, how do I enumerate all the windows displayed by the system (C#) C#如何获取主键的最大值 - C# How can i get max value of primary key 如何在用C#编写的Windows服务中获取ServiceName? - How can I get the ServiceName in a Windows Service written in C#? 如何使用 C# 以编程方式禁用 XP 中的 Windows 更新 - How can you programatically disable Windows Update in XP using C# 如何从Windows XP Embedded中的C#访问性能计数器? - How do I access performance counters from C# in Windows XP Embedded? 如何使用C#在Windows XP / 2K上连接/断开连接 - How to connect / disconnection connection on windows XP / 2K with c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM