Skip to content

Instantly share code, notes, and snippets.

@PAMinerva
Last active February 2, 2025 09:00
Show Gist options
  • Save PAMinerva/40cb862bc1fff16f261e2b1902063018 to your computer and use it in GitHub Desktop.
Save PAMinerva/40cb862bc1fff16f261e2b1902063018 to your computer and use it in GitHub Desktop.
Get Windows Product Key
#include <windows.h>
#include <iostream>
#include <vector>
#include <string>
static constexpr const int start = 52;
static const std::string digits = "BCDFGHJKMPQRTVWXY2346789";
// The Microsoft Windows product key is hidden in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion,
// which is base24-encoded & encrypted in a binary string.
// Decoding routines are provided by the WinProdKeyFinder project (github.com/mrpeardotnet/WinProdKeyFinder),
// ported to C++11.
static std::string DecodeKeyFor7AndLower(const ::BYTE *bytes)
{
if (!bytes)
return std::string();
static constexpr const int end = start + 15;
static constexpr const int decodeLength = 29;
static constexpr int decodeStringLength = 15;
std::string chars;
chars.resize(decodeLength + 1);
::byte hexPid[end - start + 1];
for (int i = start; i <= end; ++i)
hexPid[i - start] = bytes[i];
for (int i = decodeLength - 1; i >= 0; i--)
{
if ((i + 1) % 6 == 0)
chars[i] = '-';
else
{
int digitMapIndex = 0;
for (int j = decodeStringLength - 1; j >= 0; j--)
{
int byteValue = (digitMapIndex << 8) | hexPid[j];
hexPid[j] = (::byte) (byteValue / 24);
digitMapIndex = byteValue % 24;
chars[i] = digits[digitMapIndex];
}
}
}
return chars;
}
static std::string DecodeKeyFor8AndHigher(/* const */ ::BYTE *bytes)
{
if (!bytes)
return std::string();
std::string chars = {};
bytes[66] = (byte)((bytes[66] & 0xf7) | (1 & 2) * 4);
int last = 0;
for (int i = 24; i >= 0; i--)
{
int current = 0;
for (int j = 14; j >= 0; j--)
{
current = current*256;
current = bytes[j + start] + current;
bytes[j + start] = (byte)(current/24);
current = current%24;
last = current;
}
chars = digits[current] + chars;
}
chars = chars.substr(1, last) + "N" + chars.substr(last + 1, chars.size() - (last + 1));
for (auto i = 5; i < chars.size(); i += 6)
chars.insert(i, "-");
return chars;
}
// Recupera la chiave di Windows dalla Registry Key "DigitalProductId"
std::string GetWindowsProductKey()
{
// Legge la chiave dal registro
HKEY hKey;
const char* subKey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ | KEY_WOW64_64KEY, &hKey) != ERROR_SUCCESS)
{
return "";
}
DWORD sizeNeeded = 0;
if (RegQueryValueExA(hKey, "DigitalProductId", nullptr, nullptr, nullptr, &sizeNeeded) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return "";
}
std::vector<BYTE> digitalProductId(sizeNeeded);
if (RegQueryValueExA(hKey, "DigitalProductId", nullptr, nullptr, digitalProductId.data(), &sizeNeeded) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return "";
}
RegCloseKey(hKey);
// Verifica versione Windows
bool isWin8AndHigher = (digitalProductId.data()[66] >> 3) & 1;
// Esegue la decodifica in base alla versione
if (isWin8AndHigher)
{
return DecodeKeyFor8AndHigher(digitalProductId.data());
}
else
{
return DecodeKeyFor7AndLower(digitalProductId.data());
}
}
int main()
{
std::string productKey = GetWindowsProductKey();
if (!productKey.empty()) {
std::cout << "Product Key: " << productKey << std::endl;
} else {
std::cout << "Impossibile recuperare il product key." << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment