Skip to content

Instantly share code, notes, and snippets.

@SebastianRedinger
Created December 14, 2021 08:48
Show Gist options
  • Save SebastianRedinger/04cd4b99449afe886843c0d8dfe55207 to your computer and use it in GitHub Desktop.
Save SebastianRedinger/04cd4b99449afe886843c0d8dfe55207 to your computer and use it in GitHub Desktop.
Windows Product Key viewer (incl. Windows 10)
#include "stdafx.h"
#include <Windows.h>
#include <intrin.h>
#include <atlstr.h>
#include <wbemidl.h>
#include <comdef.h>
#include <Strsafe.h>
#include <stdio.h>
#include <conio.h>
#include <string>
#include <iostream>
int main()
{
HKEY hRegistryKey;
BYTE *DigitalProductID = NULL;
DWORD DataLength;
int isWin10Key = 0;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"),
0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &hRegistryKey) == ERROR_SUCCESS)
{
DataLength = 164;
DigitalProductID = (BYTE *)malloc(DataLength);
memset(DigitalProductID, 0, DataLength);
if (RegQueryValueEx(hRegistryKey, TEXT("DigitalProductId"),
0, NULL, DigitalProductID, &DataLength) == ERROR_SUCCESS)
{
isWin10Key = (byte)((DigitalProductID[66] / 6) & 1);
DigitalProductID[66] = (byte)((DigitalProductID[66] & 0xF7) | ((isWin10Key & 2) * 4));
}
}
std::string pkey;
const std::string digits = "BCDFGHJKMPQRTVWXY2346789";
int last = 0;
for (int i = 24; i >= 0; i--) {
int current = 0;
for (int j = 14; j >= 0; j--) {
current = current * 256;
current = DigitalProductID[j + 52] + current;
DigitalProductID[j + 52] = (BYTE)(current / 24);
current = current % 24;
last = current;
}
pkey = digits[current] + pkey;
}
RegCloseKey(hRegistryKey);
if (DigitalProductID) free(DigitalProductID);
std::string productKey;
if (isWin10Key != 0) {
productKey = pkey;
std::string keypart1 = productKey.substr(1, last);
std::string keypart2 = productKey.substr(last + 1, productKey.length() - (last + 1));
productKey = keypart1 + "N" + keypart2;
}
else {
productKey = pkey;
}
for (unsigned int i = 5; i < productKey.length(); i += 6) {
productKey = productKey.insert(i, "-");
}
std::cout << productKey;
std::getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment