How can I get a Windows product key in C #?

How can I get a Windows product key in C #?

I want to get the Windows key from the client side.

+5
source share
7 answers

Windows Key Key Finder and the other solutions mentioned here by Erij J. and others only work for Windows XP and Windows 7. Microsoft has changed the key encryption algorithm from Windows 8.

I found a solution for Windows 8 and 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 rewrote it in C #.

You can check out the full project on GitHub: https://github.com/mrpeardotnet/WinProdKeyFinder

Here is the code on how to decode a product key in Windows 8 and above:

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 the version of Windows and force digitalProductId to use the wrapper method as follows:

  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 tested it on several machines and it gave me the correct results. Even in Windows 10, I managed to get the generic Windows 10 code (on the updated system).

Note. For me, the original vbs script returned the wrong Win7 keys, despite the fact that the source blog indicated that the code works for Win7 and higher. Therefore, I always abandon the well-known old method for Win7 and below.

Hope this helps.

+6
source

WMI has a class called Win32_OperatingSystem that contains the SerialNumber property:

The operating system product serial number.

Example: "10497-OEM-0031416-71674"

The System.Management namespace contains classes that allow .NET code to interact with WMI.

+3
source

Check out the Windows Product Key Finder software.

http://wpkf.codeplex.com/

Source available.

The key is stored in the registry and must be decoded using the algorithm available in the source.

+2
source

Transition with mrpeardotnet answer ...

I was getting errors when RegistryView.Default did not convert to string. I changed this:

 var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default); 

:

 var key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, Environment.MachineName); 

and it seems to work.

+1
source

Why not try keygetter.com, it will help you instantly recover your lost product key.

0
source

Using this code, you can find the product key for your Microsoft products.

 using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; 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: registry = Registry.LocalMachine. OpenSubKey( @"SOFTWARE\Microsoft\Office\10.0\Registration\" + @"{90280409-6000-11D3-8CFE-0050048383C9}", false); // 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); } } } 

Link: http://www.codeproject.com/Articles/23334/Microsoft-Product-Key-Finder

-1
source

Source: https://habr.com/ru/post/1416514/


All Articles