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: -
//first byte offset
-
const int start = 52;
-
//last byte offset
-
const int end = start + 15;
-
//decoded key length
-
const int length = 29;
-
//decoded key in byte form
-
const int decoded = 15;
-
-
//char[] for holding the decoded product key
-
var decodedKey = new char[length];
-
-
//List<byte> to hold the key bytes
-
var keyHex = new List<byte>();
-
-
//list to hold possible alpha-numeric characters
-
//that may be in the product key
-
var charsInKey = new List<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'
-
};
-
Next step is to add all the bytes to our List<byte>: -
//add all bytes to our list
-
for (var i = start; i <= end; i++)
-
keyHex.Add(id[i]);
-
Next we do the actual heavy listing (decode the byte array) to get the product key: -
//now the decoding starts
-
for (var i = length - 1; i >= 0; i--)
-
-
switch ((i + 1) % 6)
-
{
-
//if the calculation is 0 (zero) then add the seperator
-
case 0:
-
decodedKey[i] = '-';
-
break;
-
default:
-
var idx = 0;
-
-
for (var j = decoded - 1; j >= 0; j--)
-
{
-
var @value = (idx << 8) | keyHex[j];
-
keyHex[j] = (byte) (@value/24);
-
idx = @value%24;
-
decodedKey[i] = charsInKey[idx];
-
}
-
break;
-
}
-
And finally convert the byte array to a string containing the product key: -
return new string(decodedKey);
-
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: -
private void FindProductKeyClick(object sender, EventArgs e)
-
{
-
byte[] id = null;
-
var regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
-
-
if (regKey != null) id = regKey.GetValue("DigitalProductId") as byte[];
-
-
ProductKeyTextBox.Text = DecodeKeyByteArray(id);
-
}
-
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!
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
I've tried your solution, but I think I did something wrong. - //first byte offset
-
const int start = 52;
-
//last byte offset
-
const int end = start + 15;
-
//decoded key length
-
const int length = 29;
-
//decoded key in byte form
-
const int decoded = 15;
-
-
private void btnGetKey_Click(object sender, EventArgs e)
-
{
-
byte[] id = null;
-
var regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
-
-
if (regKey != null) id = regKey.GetValue("DigitalProductId") as byte[];
-
{
-
WindowsProductKey.Text = DecodeKeyByteArray(id);
-
}
-
-
//char[] for holding the decoded product key
-
var decodedKey = new char[length];
-
-
//List<byte> to hold the key bytes
-
var keyHex = new List<byte>();
-
-
//list to hold possible alpha-numeric characters
-
//that may be in the product key
-
var charsInKey = new List<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'
-
};
-
//add all bytes to our list
-
for (var i = start; i <= end; i++)
-
{
-
keyHex.Add(id[i]);
-
}
-
-
//now the decoding starts
-
for (var i = length - 1; i >= 0; i--)
-
-
switch ((i + 1) % 6)
-
{
-
//if the calculation is 0 (zero) then add the seperator
-
case 0:
-
decodedKey[i] = '-';
-
break;
-
default:
-
var idx = 0;
-
-
for (var j = decoded - 1; j >= 0; j--)
-
{
-
var @value = (idx << 8) | keyHex[j];
-
keyHex[j] = (byte)(@value / 24);
-
idx = @value % 24;
-
decodedKey[i] = charsInKey[idx];
-
}
-
break;
-
}
-
return new string(decodedKey);
-
}
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: -
private void btnGetKey_Click(object sender, EventArgs e)
-
{
-
byte[] id = null;
-
var regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
-
-
if (regKey != null) id = regKey.GetValue("DigitalProductId") as byte[];
-
{
-
WindowsProductKey.Text = DecodeKeyByteArray(id);
-
}
-
}
- class getKey
-
{
-
//first byte offset
-
const int start = 52;
-
//last byte offset
-
const int end = start + 15;
-
//decoded key length
-
const int length = 29;
-
//decoded key in byte form
-
const int decoded = 15;
-
-
public WindowsKey()
-
{
-
//char[] for holding the decoded product key
-
var decodedKey = new char[length];
-
-
//List<byte> to hold the key bytes
-
var keyHex = new List<byte>();
-
-
//list to hold possible alpha-numeric characters
-
//that may be in the product key
-
var charsInKey = new List<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'
-
};
-
//add all bytes to our list
-
for (var i = start; i <= end; i++)
-
{
-
keyHex.Add(id[i]);
-
}
-
-
//now the decoding starts
-
for (var i = length - 1; i >= 0; i--)
-
{
-
-
switch ((i + 1) % 6)
-
{
-
//if the calculation is 0 (zero) then add the seperator
-
case 0:
-
decodedKey[i] = '-';
-
break;
-
default:
-
var idx = 0;
-
-
for (var j = decoded - 1; j >= 0; j--)
-
{
-
var @value = (idx << 8) | keyHex[j];
-
keyHex[j] = (byte)(@value / 24);
-
idx = @value % 24;
-
decodedKey[i] = charsInKey[idx];
-
}
-
break;
-
}
-
}
-
return new string(decodedKey);
-
}
-
}
-
}
it also says that I don't have a return method. Any idea what I need to add, to make it working?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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
|
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.
|
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
| |
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
|
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?
|
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...
|
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?
|
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
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |