Willem's...

{rue if I mellow}

  Home  |   Contact  |   Syndication    |   Login
  24 Posts | 0 Stories | 93 Comments | 78 Trackbacks

News

Archives

Post Categories

Businessware Architects

Ever needed to reinstall your PC and could not recover your product key from that 'safe storage' you were supposed to use? It is no secret that if your system is still running you can recover the key from the system registry where it is encoded in a REG_BINARY value.

You can download a free utility that recovers the XP product key (and the MS Office product key if its installed):

You can even recover it online here:

but if you're paranoid, maybe these applications are actually stealing your product key, so you want to do this yourself with your own code, right?  The only bit of code I could find that can recover product keys is here:

which provides a working Delphi source-code listing. I only required to find the XP product key, so after hacking the code, I produced a working C# version (works with .NET 1.1 and 2.0) shown below. I allowed place-holders for other product keys but the code may have to be further modified to work correctly.

Windows XP KeyFinder Listing
using System;
using System.Collections;
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:
          // 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);
    }
  }
}

Once you realize how the product keys are encoded, a search through the registry for the values starting with DigitalProductId indicates that many more product keys may be encoded this way.

posted on Sunday, April 23, 2006 9:37 AM

Feedback

# re: Recover your XP product key from the registry using C# 5/19/2006 6:20 PM Anonymous
There's this one too:

http://www.snapfiles.com/get/rockxp.html

# re: Recover your XP product key from the registry using C# 10/28/2006 11:18 PM Mike Cobain
how do I call the function to get the final String to be showed at a txt box ?



# re: Recover your XP product key from the registry using C# 11/2/2006 1:16 PM slide
Any ideas on reversing the process? I have a bunch of machines at work that I need to rekey Office for (the same decode algorithm works for Office keys)

# re: Recover your XP product key from the registry using C# 11/6/2006 1:23 PM Willem
I guess you could hack the registry directly but considering the dire warnings MS gives about this, I would suggest using some VB scripts that change the product key using WMI. Check out the following link:

http://support.microsoft.com/kb/328874

It should be possible to also do this using C# by utilizing the classes of the System.Management namespace. Check on Google for the key-words C# and WMI...


# re: Recover your XP product key from the registry using C# 4/20/2007 7:56 AM Derek Flournoy
Wow, Very impressive... i had to add a Main method (obviously) but it definitely worked,

Thank you sir!

# re: Recover your XP product key from the registry using C# 8/6/2007 6:14 PM sysguru
for those of you who have not figured out how to actually make this work....

byte[] results =KeyDecoder.GetRegistryDigitalProductId(KeyDecoder.Key.XP);

textBox1.Text = KeyDecoder.DecodeProductKey(results);




# re: Recover your XP product key from the registry using C# 8/9/2007 2:22 AM Socrates
Does not work with 64-bit editions of Windows due to Registry reflection/mirroring

# re: Recover your XP product key from the registry using C# 8/13/2007 7:44 PM CU-420
Did not work for me.
I have an older version of XP Home, it came with only SP1.

Worked on Vista though

# re: Recover your XP product key from the registry using C# 11/9/2007 5:51 AM Thanks
I can use it to "偷" ProductKey

# re: Recover your XP product key from the registry using C# 11/23/2007 3:44 PM Paul
I have used this software on OEM and retail versions of Windows Pro x64 (not Itanium) and it works fine. Socrates may have tried it on an Itanium OS or a mass produced OEM version like Dell would distribute. Anyway it works fine.

# re: Recover your XP product key from the registry using C# 2/1/2008 11:04 PM Goody
use WinGuggle

You can use this application to get your Windows Vista Product key easily.

It Provides Complete Way to Brand your PC/Laptop:
like add information to Windows experience index page, system properties page and owners information info. WOW! Great Tool…

Download Here: WinGuggle
http://unlockforus.blogspot.com/2008/01/winguggle-get-your-windows-vista.html



# re: Recover your XP product key from the registry using C# 2/1/2008 11:06 PM Goody
Download Here:


# re: Recover your XP product key from the registry using C# 3/14/2008 12:19 PM Big Mike
So how about a method to encode a human readable product key into a digitalproductID?

# re: Recover your XP product key from the registry using C# 7/10/2008 10:13 PM brian
This code is great and you've been kind enough to put it online for all to see! However (if you're in the USA), you have not specified a copyright for it, so as a result you have automatic rights and no one else is allowed to use it without your permission. If you read the codinghorror blog post here http://www.codinghorror.com/blog/archives/000833.html you can see what problems this can cause.

It would be great if you could specify a copyright so the many who have received some benefit from it can use it. Even though I've seen it reposted on many other sites by now, I'm pretty sure yours is the original. Based on your comments above, it seems like Public Domain might be the best license for your intentions, but take a look at that Coding Horror post and maybe something else will jump out at you.

# re: Recover your XP product key from the registry using C# 10/1/2008 10:00 AM stuart
Excellent code works great but have experienced problems with Dell PC's. Got this from a technet post:

---------------------------------------
What Dell does is:
Create an install with a single KEY that is preactivated. The Key is provided in the winnt.sif file in the sysprep directory so the end user is not even prompted for it.

The end user never sees a prompt to enter a CD Key. Nor does the user need to activate.

The Dell XP CD's have the following characteristics:
1. No CD Key is needed to install
2. They do a BIOS Check so that they can only be installed on a DELL Computer.
3. If you reinstall from the CD, you will need to activate. If you try, it will fail and prompt you to re-enter a valid Key. Simply enter the Key from the COA sticker and you can activate.

The implications are profound. This means that as long as you do not reinstall windows on your original Dell hardware, your OEM key will activate on any new motherboard you might install.

---------------------------------------

Therefore all the keys are the same and will not match the sticker on the Dell case. Shame, would have been nice to use this to audit all PC's.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 5 and 3 and type the answer here: