473,594 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieve your Windows product key with C#

PsychoCoder
465 Recognized Expert Moderator Contributor
NOTE: Posted this on my blog this morning but also felt the bytes community could benefit from it.

I've seen this question asked many times on different forums and thought I'd develop a solution for retrieving your Windows product key. It's actually a little mnore indepth than I initially thought due to the fact that the key is encoded as a byte array in the registry, so we will have to decode it to get the actual key.

First thing we need to do is to declare all local variables needed to decode and hold the decoded product key:

Expand|Select|Wrap|Line Numbers
  1. //first byte offset
  2. const int start = 52;
  3. //last byte offset
  4. const int end = start + 15;
  5. //decoded key length
  6. const int length = 29;
  7. //decoded key in byte form
  8. const int decoded = 15;
  9.  
  10. //char[] for holding the decoded product key
  11. var decodedKey = new char[length];
  12.  
  13. //List<byte> to hold the key bytes
  14. var keyHex = new List<byte>();
  15.  
  16. //list to hold possible alpha-numeric characters
  17. //that may be in the product key
  18. var charsInKey = new List<char>()
  19.                     {
  20.                         'B', 'C', 'D', 'F', 'G', 'H', 
  21.                         'J', 'K', 'M', 'P', 'Q', 'R', 
  22.                         'T', 'V', 'W', 'X', 'Y', '2', 
  23.                         '3', '4', '6', '7', '8', '9'
  24.                     };
  25.  
Next step is to add all the bytes to our List<byte>:

Expand|Select|Wrap|Line Numbers
  1. //add all bytes to our list
  2. for (var i = start; i <= end; i++)
  3.     keyHex.Add(id[i]);
  4.  
Next we do the actual heavy listing (decode the byte array) to get the product key:

Expand|Select|Wrap|Line Numbers
  1. //now the decoding starts
  2. for (var i = length - 1; i >= 0; i--)
  3.  
  4.     switch ((i + 1) % 6)
  5.     {
  6.             //if the calculation is 0 (zero) then add the seperator
  7.         case 0:
  8.             decodedKey[i] = '-';
  9.             break;
  10.         default:
  11.             var idx = 0;
  12.  
  13.             for (var j = decoded - 1; j >= 0; j--)
  14.             {
  15.                 var @value = (idx << 8) | keyHex[j];
  16.                 keyHex[j] = (byte) (@value/24);
  17.                 idx = @value%24;
  18.                 decodedKey[i] = charsInKey[idx];
  19.             }
  20.             break;
  21.     }
  22.  
And finally convert the byte array to a string containing the product key:

Expand|Select|Wrap|Line Numbers
  1. return new string(decodedKey);
  2.  
NOTE: We use the new constructor of the string class to convert a char array to a regular string.

Ok, so now we have a method for decoding the product key, now how to use it. I created a simple WinForm with a TextBox and a Button. Here the first thing I do is to open the following key "SOFTWARE\\Micro soft\\Windows NT\\CurrentVers ion" (in HKEY_LOCAL_MACH INE). I then grab the value for DigitalProductI d and pass the byte array to the decoding function.

The button click event looks like so:

Expand|Select|Wrap|Line Numbers
  1. private void FindProductKeyClick(object sender, EventArgs e)
  2. {
  3.     byte[] id = null;
  4.     var regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
  5.  
  6.     if (regKey != null) id = regKey.GetValue("DigitalProductId") as byte[];
  7.  
  8.     ProductKeyTextBox.Text = DecodeKeyByteArray(id);
  9. }
  10.  
Now run your application and retrieve the Windows product key. See that wasnt as hard as you thought it would be now is it. I hope you find this helpful and thanks for reading.

Happy Coding!
Jul 4 '12 #1
2 14462
Frinavale
9,735 Recognized Expert Moderator Expert
Cool :)

I've never had a need to do this but I'm glad you've posted a solution if I ever need to retrieve the Windows product key :)

-Frinny
Aug 2 '12 #2
michaeldebruin
134 New Member
I've tried your solution, but I think I did something wrong.

Expand|Select|Wrap|Line Numbers
  1. //first byte offset
  2.         const int start = 52;
  3.         //last byte offset
  4.         const int end = start + 15;
  5.         //decoded key length
  6.         const int length = 29;
  7.         //decoded key in byte form
  8.         const int decoded = 15;
  9.  
  10.         private void btnGetKey_Click(object sender, EventArgs e)
  11.         {
  12.             byte[] id = null;
  13.             var regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
  14.  
  15.             if (regKey != null) id = regKey.GetValue("DigitalProductId") as byte[];
  16.             {
  17.                 WindowsProductKey.Text = DecodeKeyByteArray(id);
  18.             }
  19.  
  20.             //char[] for holding the decoded product key
  21.             var decodedKey = new char[length];
  22.  
  23.             //List<byte> to hold the key bytes
  24.             var keyHex = new List<byte>();
  25.  
  26.             //list to hold possible alpha-numeric characters
  27.             //that may be in the product key
  28.             var charsInKey = new List<char>()
  29.                     {
  30.                         'B', 'C', 'D', 'F', 'G', 'H', 
  31.                         'J', 'K', 'M', 'P', 'Q', 'R', 
  32.                         'T', 'V', 'W', 'X', 'Y', '2', 
  33.                         '3', '4', '6', '7', '8', '9'
  34.                     };
  35.             //add all bytes to our list
  36.             for (var i = start; i <= end; i++)
  37.             {
  38.                 keyHex.Add(id[i]);
  39.             }
  40.  
  41.             //now the decoding starts
  42.             for (var i = length - 1; i >= 0; i--)
  43.  
  44.                 switch ((i + 1) % 6)
  45.                 {
  46.                     //if the calculation is 0 (zero) then add the seperator
  47.                     case 0:
  48.                         decodedKey[i] = '-';
  49.                         break;
  50.                     default:
  51.                         var idx = 0;
  52.  
  53.                         for (var j = decoded - 1; j >= 0; j--)
  54.                         {
  55.                             var @value = (idx << 8) | keyHex[j];
  56.                             keyHex[j] = (byte)(@value / 24);
  57.                             idx = @value % 24;
  58.                             decodedKey[i] = charsInKey[idx];
  59.                         }
  60.                         break;
  61.                 }
  62.             return new string(decodedKey);
  63.         }
it keep saying that I shouldn't use the return in the end. Which is kinda obvious because my button is a void. But when I try to separate it into a different class like this:
Expand|Select|Wrap|Line Numbers
  1.         private void btnGetKey_Click(object sender, EventArgs e)
  2.         {
  3.             byte[] id = null;
  4.             var regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
  5.  
  6.             if (regKey != null) id = regKey.GetValue("DigitalProductId") as byte[];
  7.             {
  8.                 WindowsProductKey.Text = DecodeKeyByteArray(id);
  9.             }
  10.         }
Expand|Select|Wrap|Line Numbers
  1. class getKey
  2.     {
  3.         //first byte offset
  4.         const int start = 52;
  5.         //last byte offset
  6.         const int end = start + 15;
  7.         //decoded key length
  8.         const int length = 29;
  9.         //decoded key in byte form
  10.         const int decoded = 15;
  11.  
  12.         public WindowsKey()
  13.         {
  14.             //char[] for holding the decoded product key
  15.             var decodedKey = new char[length];
  16.  
  17.             //List<byte> to hold the key bytes
  18.             var keyHex = new List<byte>();
  19.  
  20.             //list to hold possible alpha-numeric characters
  21.             //that may be in the product key
  22.             var charsInKey = new List<char>()
  23.                     {
  24.                         'B', 'C', 'D', 'F', 'G', 'H', 
  25.                         'J', 'K', 'M', 'P', 'Q', 'R', 
  26.                         'T', 'V', 'W', 'X', 'Y', '2', 
  27.                         '3', '4', '6', '7', '8', '9'
  28.                     };
  29.             //add all bytes to our list
  30.             for (var i = start; i <= end; i++)
  31.             {
  32.                 keyHex.Add(id[i]);
  33.             }
  34.  
  35.             //now the decoding starts
  36.             for (var i = length - 1; i >= 0; i--)
  37.             {
  38.  
  39.                 switch ((i + 1) % 6)
  40.                 {
  41.                     //if the calculation is 0 (zero) then add the seperator
  42.                     case 0:
  43.                         decodedKey[i] = '-';
  44.                         break;
  45.                     default:
  46.                         var idx = 0;
  47.  
  48.                         for (var j = decoded - 1; j >= 0; j--)
  49.                         {
  50.                             var @value = (idx << 8) | keyHex[j];
  51.                             keyHex[j] = (byte)(@value / 24);
  52.                             idx = @value % 24;
  53.                             decodedKey[i] = charsInKey[idx];
  54.                         }
  55.                         break;
  56.                 }
  57.             }
  58.             return new string(decodedKey);
  59.         }
  60.     }
  61. }
it also says that I don't have a return method. Any idea what I need to add, to make it working?
Jan 25 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
67684
by: Johan den Boer | last post by:
Hi, I am new to php. Is it possible to find out the windows user name in php ? Logon in windows domain with 'user1'. I want to get the username in php in my example : user1. How do you that ? thanks
0
4879
by: Aytan | last post by:
Does anyone know how can I get Windows (98 and higher) serial number on client's machine through my .NET application? Thanks
79
14029
by: Klaus Bonadt | last post by:
In order to protect software from being copied without licence, I would like to use something like a key, which fits only to the current system. The serial number of the CPU or the current operating system would be appropriate. However, I do not know how to retrieve the information. Could you help? Klaus
2
2564
by: bob | last post by:
hi, I want to redirect my user to another page. With ASP (not .Net) and IIS 5.0, under windows 2000 server. We have installed the component WinHTTP that is used to manipulate http requests. We want to automatically identify the user through his Windows login/pwd (NTML) He does not need to type it.
0
1937
by: vvenk | last post by:
Hello: I would like to know how I can programmtically retrieve the windows login id? This is a asp.net application. Thanks. venki
3
5490
by: vbnetdev | last post by:
I have been assigned an application that is suppose to take an inventory of machines including the product keys for the systems. System operating systems included are Windows 2003, XP, 2000 and NT. Any ideas on how to get this information on these computers in a vb.net Winforms app? Thanks, Kelly
1
5065
by: mlohr | last post by:
For my intranet Rails app I need to get the windows logon from the user. I know it must be possible, but how?
4
2123
kestrel
by: kestrel | last post by:
I had a laptop (sony vaio fs920) with Windows XP Home Edition. Right from the start i was having problems; start up took 5min+ and shut down took about the same. everything about it was just slow. So i downloaded Ubuntu 7.04 and did a complete install and deleted windows all together. I've been happy with ubuntu since i installed it, but now im feeling the pain of not being able to run windows programs. I still have my windows product key. So...
11
9288
by: dmorand | last post by:
I'm trying to retrieve the username of the user logged into a machine when a person visits my page on our intranet. I've looked over cfntauthenticate but that's not going to do what I need it to do. Is this even possible with coldfusion or should I be looking to asp?
1
1535
by: engteng | last post by:
How can I retrieve Windows System Information ? example : System Name, System Model, Processor, Total Physical Memory and etc. Regards, Tee ET
0
7877
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8253
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8009
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8240
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5411
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.