PoshCode Archive  Artifact Content

Artifact 7f0c4f695541d2acac03ca07845d752f5327412d07a1017f8b7a4ab55dc7bbea:

  • File product-key-on-c.ps1 — part of check-in [d36f194d3e] at 2018-06-10 13:47:18 on branch trunk — I found it [here](https://github.com/gregzakharov/X/blob/master/ProductKey/source.cpp) and it works. (user: bO_obleak size: 1844)

# encoding: ascii
# api: powershell
# title: product key on c++
# description: I found it [here](https://github.com/gregzakharov/X/blob/master/ProductKey/source.cpp) and it works.
# version: 0.1
# author: bO_obleak
# license: CC0
# x-poshcode-id: 4875
# x-archived: 2014-02-07T23:47:41
# x-published: 2014-02-04T05:51:00
#
#
#include <windows.h>
#include <iostream>

using namespace std;

#pragma comment (lib, "advapi32.lib")

void main() {
  HKEY  Registry;
  BYTE *DigitalProductId = 0;
  DWORD RawLength;
  BYTE  raw[15];
  char  key[30];
  
  const char *key_map[] = {
    "B","C","D","F","G","H","J","K","M","P","Q","R",
    "T","V","W","X","Y","2","3","4","6","7","8","9",
  };
  
  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                   "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
                   0,
                   KEY_QUERY_VALUE,
                   &Registry) == ERROR_SUCCESS) {
    RawLength = 164;
    DigitalProductId = new byte[RawLength]; //allocate memory
    memset(DigitalProductId, 0, RawLength); //memory initialization
    
    if (RegQueryValueEx(Registry,
                        "DigitalProductId",
                        NULL,
                        NULL,
                        DigitalProductId,
                        &RawLength) == ERROR_SUCCESS) {
      memcpy(raw, DigitalProductId + 52, 15); //raw key
      if (DigitalProductId) delete []DigitalProductId; //release memory
      RegCloseKey(Registry);
    }
  }
  
  memset(key, 0, sizeof(key));
  for (int i = 24; i >= 0; i--) {
    int k = 0;
    for (int j = 14; j >= 0; j--) {
      k = (k * 256) ^ raw[j];
      raw[j] = k / 24;
      k %= 24;
    }
    
    strcat_s(key, key_map[k]);
    if (!(i % 5) && i) strcat_s(key, "-");
  }
  
  cout << _strrev(key) << endl;
}