Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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

I want to get some key like windows key from client side.

like image 654
lovin Avatar asked Jun 07 '12 06:06

lovin


People also ask

Where is my Windows product key on C drive?

The best way to recover a lost product key for Windows or an Office program is to use a third-party program, like Nirsoft's Produkey or ShowKeyPlus. You may also have a sticker attached to your PC or disk box with a product key written on it.

Where is product key in CD?

If you are looking for your program CD key, it's found on the CD sleeve, jewel box, or the printed documentation included with the disc.

How many digits is a Windows XP product key?

All keys -- even volume license keys -- are 25 characters long. Since you don't even have a volume license, you should forget about trying to find a key for one.


1 Answers

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.

like image 185
mrpeardotnet Avatar answered Sep 30 '22 02:09

mrpeardotnet