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

c#

Solution

Windows Product Key Finder and other solutions mentioned here by Erij J. and others are working for Windows XP and Windows 7 only. Microsoft has changed key encryption algorithm since Windows 8.

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/

However it is written in VBS, so I've rewritten it to C#.

You can check full project on GitHub: https://github.com/mrpeardotnet/WinProdKeyFinder

Here is the code how to decode product key in Windows 8 and up:

    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:

    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).

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. So I always fallback to the well known old method for Win7 and lower.

Hope it helps.

Problem

How can I get windows XP product key in C# ? I want to get some key like windows key from client side.

Original source