473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Retrieve your Windows product key with C#

PsychoCoder
465 Expert Mod 256MB
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\\Microsoft\\Windows NT\\CurrentVersion" (in HKEY_LOCAL_MACHINE). I then grab the value for DigitalProductId 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 14378
Frinavale
9,735 Expert Mod 8TB
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 100+
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
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...
0
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
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...
2
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. ...
0
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
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....
1
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
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...
11
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....
1
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.